blob: 3d78a4442f72be8167c2ecd3ed2690349716b7ce [file] [log] [blame]
Mathias Bynens79e2cf02020-05-29 14:46:171'use strict'
2
3var own = {}.hasOwnProperty
4
5module.exports = stringify
6
7function stringify(value) {
8 // Nothing.
9 if (!value || typeof value !== 'object') {
10 return ''
11 }
12
13 // Node.
14 if (own.call(value, 'position') || own.call(value, 'type')) {
15 return position(value.position)
16 }
17
18 // Position.
19 if (own.call(value, 'start') || own.call(value, 'end')) {
20 return position(value)
21 }
22
23 // Point.
24 if (own.call(value, 'line') || own.call(value, 'column')) {
25 return point(value)
26 }
27
28 // ?
29 return ''
30}
31
32function point(point) {
33 if (!point || typeof point !== 'object') {
34 point = {}
35 }
36
37 return index(point.line) + ':' + index(point.column)
38}
39
40function position(pos) {
41 if (!pos || typeof pos !== 'object') {
42 pos = {}
43 }
44
45 return point(pos.start) + '-' + point(pos.end)
46}
47
48function index(value) {
49 return value && typeof value === 'number' ? value : 1
50}