-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
102 lines (92 loc) · 2.15 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* @author Titus Wormer
* @copyright 2015 Titus Wormer. All rights reserved.
* @module mdast:util:position
* @fileoverview Utility to get either the starting or the
* ending position of a node, and if its generated or not.
*/
'use strict';
/**
* Factory to get a position at `type`.
*
* @example
* positionFactory('start'); // Function
*
* positionFactory('end'); // Function
*
* @param {string} type - Either `'start'` or `'end'`.
* @return {function(Node): Object}
*/
function positionFactory(type) {
/**
* Get a position in `node` at a bound `type`.
*
* @example
* // When bound to `start`.
* start({
* start: {
* line: 1,
* column: 1
* }
* }); // {line: 1, column: 1}
*
* // When bound to `end`.
* end({
* end: {
* line: 1,
* column: 2
* }
* }); // {line: 1, column: 2}
*
* @param {Node} node - Node to check.
* @return {Object} - Position at `type` in `node`, or
* an empty object.
*/
return function (node) {
var pos = (node && node.position && node.position[type]) || {};
return {
'line': pos.line || null,
'column': pos.column || null,
'indent': pos.indent || null,
'offset': isNaN(pos.offset) ? null : pos.offset
};
};
}
/*
* Getters.
*/
var position = {
'start': positionFactory('start'),
'end': positionFactory('end')
};
/**
* Detect if a node was available in the original document.
*
* @example
* generated(); // true
*
* generated({
* start: {
* line: 1,
* column: 1
* },
* end: {
* line: 1,
* column: 2
* }
* }); // false
*
* @param {Node} node - Node to test.
* @return {boolean} - Whether or not `node` is generated.
*/
function generated(node) {
var initial = position.start(node);
var final = position.end(node);
return initial.line === null || initial.column === null ||
final.line === null || final.column === null;
}
position.generated = generated;
/*
* Expose.
*/
module.exports = position;