11
11
12
12
/* eslint-env commonjs */
13
13
14
+ /*
15
+ * Dependencies.
16
+ */
17
+
18
+ var is = require ( 'unist-util-is' ) ;
19
+
20
+ /**
21
+ * Find a node after `index` in `parent` which passes
22
+ * `test`.
23
+ *
24
+ * @param {Node } parent - Parent to search in.
25
+ * @param {number|Node } index - (Position of) node to
26
+ * search after.
27
+ * @param {* } test - See `wooorm/unist-util-is`.
28
+ * @return {Node? } - A child node of `parent` which passes
29
+ * `test`.
30
+ */
31
+ function findAfter ( parent , index , test ) {
32
+ var children ;
33
+ var child ;
34
+ var length ;
35
+
36
+ if ( ! parent || ! parent . type || ! parent . children ) {
37
+ throw new Error ( 'Expected parent node' ) ;
38
+ }
39
+
40
+ children = parent . children ;
41
+ length = children . length ;
42
+
43
+ if ( index && index . type ) {
44
+ index = children . indexOf ( index ) ;
45
+ }
46
+
47
+ if ( isNaN ( index ) || index < 0 || index === Infinity ) {
48
+ throw new Error ( 'Expected positive finite index or child node' ) ;
49
+ }
50
+
51
+ while ( ++ index < length ) {
52
+ child = children [ index ] ;
53
+
54
+ if ( is ( test , child , index , parent ) ) {
55
+ return child ;
56
+ }
57
+ }
58
+
59
+ return null ;
60
+ }
61
+
62
+ /*
63
+ * Expose.
64
+ */
65
+
66
+ module . exports = findAfter ;
67
+
68
+ } , { "unist-util-is" :2 } ] , 2 :[ function ( require , module , exports ) {
69
+ /**
70
+ * @author Titus Wormer
71
+ * @copyright 2015 Titus Wormer
72
+ * @license MIT
73
+ * @module unist:util:is
74
+ * @fileoverview Utility to check if a node passes a test.
75
+ */
76
+
77
+ 'use strict' ;
78
+
79
+ /* eslint-env commonjs */
80
+
14
81
/**
15
82
* Test.
16
83
*
17
- * @typedef {Function } findAfter ~test
84
+ * @typedef {Function } is ~test
18
85
* @param {Node } node - Node to test.
19
86
* @param {number } index - Position of `node` in `parent`.
20
87
* @param {Node } parent - Parent of `node`.
21
88
* @return {boolean? } - Whether this iteration passes.
22
89
*/
23
90
24
91
/**
25
- * Utility to return true for the first node .
92
+ * Utility to return true.
26
93
*
27
- * @type {findAfter ~test }
94
+ * @type {is ~test }
28
95
*/
29
96
function first ( ) {
30
97
return true ;
@@ -35,7 +102,7 @@ function first() {
35
102
* a given node’s type for said string.
36
103
*
37
104
* @param {string } test - Node type to test.
38
- * @return {findAfter ~test } - Tester.
105
+ * @return {is ~test } - Tester.
39
106
*/
40
107
function typeFactory ( test ) {
41
108
return function ( node ) {
@@ -48,44 +115,58 @@ function typeFactory(test) {
48
115
* a given node for strict equality.
49
116
*
50
117
* @param {Node } test - Node to test.
51
- * @return {findAfter ~test } - Tester.
118
+ * @return {is ~test } - Tester.
52
119
*/
53
120
function nodeFactory ( test ) {
54
121
return function ( node ) {
55
- return Boolean ( node && node === test ) ;
122
+ return node === test ;
56
123
}
57
124
}
58
125
59
126
/**
60
- * Find a node after `index` in `parent` which passes
61
- * `test`.
127
+ * Assert if `test` passes for `node`.
128
+ * When a `parent` node is known the `index` of node
62
129
*
63
- * @param {Node } parent - Parent to search in.
64
- * @param {number|Node } index - (Position of) node to
65
- * search after.
66
- * @param {string|Node|findAfter~test } test - Tester.
67
- * @return {Node? } - A child node of `parent` which passes
68
- * `test`.
130
+ * @example
131
+ * is(null, {type: 'strong'}); // true
132
+ *
133
+ * @example
134
+ * is('strong', {type: 'strong'}); // true
135
+ * is('emphasis', {type: 'strong'}); // false
136
+ *
137
+ * @example
138
+ * var node = {type: 'strong'};
139
+ * is(node, node) // true
140
+ * is(node, {type: 'strong'}) // false
141
+ *
142
+ * @example
143
+ * var node = {type: 'strong'};
144
+ * var parent = {type: 'paragraph', children: [node]};
145
+ * function test(node, n) {return n === 5};
146
+ * is(test, {type: 'strong'}); // false
147
+ * is(test, {type: 'strong'}, 4, parent); // false
148
+ * is(test, {type: 'strong'}, 5, parent); // true
149
+ *
150
+ * @example
151
+ * var node = {type: 'strong'};
152
+ * var parent = {type: 'paragraph', children: [node]};
153
+ * is('strong'); // throws
154
+ * is('strong', node, 0) // throws
155
+ * is('strong', node, null, parent) // throws
156
+ * is('strong', node, 0, {type: 'paragraph'}) // throws
157
+ * is('strong', node, -1, parent) // throws
158
+ * is('strong', node, Infinity, parent) // throws
159
+ *
160
+ * @param {(string|Node|is~test)? } test - Tester.
161
+ * @param {Node } node - Node to test.
162
+ * @param {number? } [index] - Position of `node` in `parent`.
163
+ * @param {Node? } [parent] - Parent of `node`.
164
+ * @param {* } [context] - Context to invoke `test` with.
165
+ * @return {boolean } - Whether `test` passes.
69
166
*/
70
- function findAfter ( parent , index , test ) {
71
- var children ;
72
- var child ;
73
- var length ;
74
-
75
- if ( ! parent || ! parent . type || ! parent . children ) {
76
- throw new Error ( 'Expected parent node' ) ;
77
- }
78
-
79
- children = parent . children ;
80
- length = children . length ;
81
-
82
- if ( index && index . type ) {
83
- index = children . indexOf ( index ) ;
84
- }
85
-
86
- if ( isNaN ( index ) || index < 0 || index === Infinity ) {
87
- throw new Error ( 'Expected positive finite index or child node' ) ;
88
- }
167
+ function is ( test , node , index , parent , context ) {
168
+ var hasParent = parent !== null && parent !== undefined ;
169
+ var hasIndex = index !== null && index !== undefined ;
89
170
90
171
if ( typeof test === 'string' ) {
91
172
test = typeFactory ( test ) ;
@@ -97,22 +178,33 @@ function findAfter(parent, index, test) {
97
178
throw new Error ( 'Expected function, string, or node as test' ) ;
98
179
}
99
180
100
- while ( ++ index < length ) {
101
- child = children [ index ] ;
181
+ if ( ! node || ! node . type ) {
182
+ throw new Error ( 'Expected node' ) ;
183
+ }
102
184
103
- if ( test ( child , index , parent ) ) {
104
- return child ;
105
- }
185
+ if (
186
+ hasIndex &&
187
+ ( typeof index !== 'number' || index < 0 || index === Infinity )
188
+ ) {
189
+ throw new Error ( 'Expected positive finite index or child node' ) ;
106
190
}
107
191
108
- return null ;
192
+ if ( hasParent && ( ! parent || ! parent . type || ! parent . children ) ) {
193
+ throw new Error ( 'Expected parent node' ) ;
194
+ }
195
+
196
+ if ( hasParent !== hasIndex ) {
197
+ throw new Error ( 'Expected both parent and index' ) ;
198
+ }
199
+
200
+ return Boolean ( test . call ( context , node , index , parent ) ) ;
109
201
}
110
202
111
203
/*
112
204
* Expose.
113
205
*/
114
206
115
- module . exports = findAfter ;
207
+ module . exports = is ;
116
208
117
209
} , { } ] } , { } , [ 1 ] ) ( 1 )
118
210
} ) ;
0 commit comments