-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
169 lines (151 loc) · 3.71 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
'use strict';
const assert = require('assert');
/**
* Initialize a new `Stack`
*/
module.exports = class Stack extends Array {
/**
* Get the first element in the stack.
*
* ```js
* const Stack = require('snapdragon-stack');
* stack.push('a');
* stack.push('b');
* stack.push('c');
* console.log(stack.first()); //=> 'a'
* ```
* @name .first
* @return {any}
* @api public
*/
first() {
return this[0];
}
/**
* Get the `n`th element from the end of the stack.
*
* ```js
* const stack = new Stack();
* stack.push('aaa');
* stack.push('bbb');
* stack.push('ccc');
* stack.push('ddd');
* console.log(stack.lookbehind(1)); //=> 'ddd'
* console.log(stack.lookbehind(2)); //=> 'ccc'
* console.log(stack.lookbehind(3)); //=> 'bbb'
* ```
* @name .lookbehind
* @param {Number} `n`
* @return {Object}
* @api public
*/
lookbehind(n) {
assert.equal(n && typeof n, 'number', 'expected a number');
return this[this.length - Math.abs(n)];
}
/**
* Get the last element in the stack.
*
* ```js
* const Stack = require('snapdragon-stack');
* stack.push('a');
* stack.push('b');
* stack.push('c');
* console.log(stack.last()); //=> 'c'
* ```
* @name .last
* @return {any}
* @api public
*/
last() {
return this.lookbehind(1);
}
/**
* Semantic alias for `stack.last()`.
*
* ```js
* const Stack = require('snapdragon-stack');
* stack.push({ type: 'root' });
* console.log(stack.current()); //=> { type: 'root' }
* ```
* @name .current
* @return {any}
* @api public
*/
current() {
return this.lookbehind(1);
}
/**
* Get the second-to-last item in the stack.
*
* ```js
* const Stack = require('snapdragon-stack');
* stack.push('a');
* stack.push('b');
* stack.push('c');
* console.log(stack.prev()); //=> 'b'
* ```
* @name .prev
* @return {any}
* @api public
*/
prev() {
return this.lookbehind(2);
}
/**
* If the [.first](#first) element in the stack is an object with a `.nodes`
* array, the first item from `stack.first().nodes` is returned.
*
* ```js
* const Stack = require('snapdragon-stack');
* const Node = require('snapdragon-node');
*
* const node = new Node({ type: 'brace' });
* node.push(new Node({ type: 'brace.open', value: '{' }));
* node.push(new Node({ type: 'text', value: 'a,b,c' }));
* node.push(new Node({ type: 'brace.close', value: '}' }));
*
* stack.push(node);
* console.log(stack.firstChild()); //=> Node { type: 'brace.open', value: '{' }
* ```
* @name .firstChild
* @return {any}
* @api public
*/
firstChild() {
const ele = this.first();
if (isObject(ele) && Array.isArray(ele.nodes)) {
return ele.nodes[0];
}
}
/**
* If the [.last](#last) element in the stack is an object with a `.nodes`
* array, the last item from `last.nodes` is returned.
*
* ```js
* const Stack = require('snapdragon-stack');
* const Node = require('snapdragon-node');
*
* const node = new Node({ type: 'brace' });
* node.push(new Node({ type: 'brace.open', value: '{' }));
* node.push(new Node({ type: 'text', value: 'a,b,c' }));
* node.push(new Node({ type: 'brace.close', value: '}' }));
*
* stack.push(node);
* console.log(stack.lastChild()); //=> Node { type: 'brace.close', value: '}' }
* ```
* @name .lastChild
* @return {any}
* @api public
*/
lastChild() {
let ele = this.last();
while (isObject(ele) && Array.isArray(ele.nodes) && ele.nodes.length) {
ele = ele.nodes[ele.nodes.length - 1];
}
return ele;
}
};
function isObject(val) {
return val && typeof val === 'object';
}