1111
1212/* eslint-env commonjs */
1313
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+
1481/**
1582 * Test.
1683 *
17- * @typedef {Function } findAfter ~test
84+ * @typedef {Function } is ~test
1885 * @param {Node } node - Node to test.
1986 * @param {number } index - Position of `node` in `parent`.
2087 * @param {Node } parent - Parent of `node`.
2188 * @return {boolean? } - Whether this iteration passes.
2289 */
2390
2491/**
25- * Utility to return true for the first node .
92+ * Utility to return true.
2693 *
27- * @type {findAfter ~test }
94+ * @type {is ~test }
2895 */
2996function first ( ) {
3097 return true ;
@@ -35,7 +102,7 @@ function first() {
35102 * a given node’s type for said string.
36103 *
37104 * @param {string } test - Node type to test.
38- * @return {findAfter ~test } - Tester.
105+ * @return {is ~test } - Tester.
39106 */
40107function typeFactory ( test ) {
41108 return function ( node ) {
@@ -48,44 +115,58 @@ function typeFactory(test) {
48115 * a given node for strict equality.
49116 *
50117 * @param {Node } test - Node to test.
51- * @return {findAfter ~test } - Tester.
118+ * @return {is ~test } - Tester.
52119 */
53120function nodeFactory ( test ) {
54121 return function ( node ) {
55- return Boolean ( node && node === test ) ;
122+ return node === test ;
56123 }
57124}
58125
59126/**
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
62129 *
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.
69166 */
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 ;
89170
90171 if ( typeof test === 'string' ) {
91172 test = typeFactory ( test ) ;
@@ -97,22 +178,33 @@ function findAfter(parent, index, test) {
97178 throw new Error ( 'Expected function, string, or node as test' ) ;
98179 }
99180
100- while ( ++ index < length ) {
101- child = children [ index ] ;
181+ if ( ! node || ! node . type ) {
182+ throw new Error ( 'Expected node' ) ;
183+ }
102184
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' ) ;
106190 }
107191
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 ) ) ;
109201}
110202
111203/*
112204 * Expose.
113205 */
114206
115- module . exports = findAfter ;
207+ module . exports = is ;
116208
117209} , { } ] } , { } , [ 1 ] ) ( 1 )
118210} ) ;
0 commit comments