Skip to content

Commit 206246c

Browse files
committed
Refactor to externalise assertions
1 parent 9ad1d00 commit 206246c

File tree

7 files changed

+156
-119
lines changed

7 files changed

+156
-119
lines changed

component.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
"util",
1717
"utility"
1818
],
19+
"dependencies": {
20+
"wooorm/unist-util-is": "^1.0.0"
21+
},
1922
"repository": "wooorm/unist-util-find-after",
2023
"scripts": [
2124
"index.js"

index.js

Lines changed: 5 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10,50 +10,11 @@
1010

1111
/* eslint-env commonjs */
1212

13-
/**
14-
* Test.
15-
*
16-
* @typedef {Function} findAfter~test
17-
* @param {Node} node - Node to test.
18-
* @param {number} index - Position of `node` in `parent`.
19-
* @param {Node} parent - Parent of `node`.
20-
* @return {boolean?} - Whether this iteration passes.
21-
*/
22-
23-
/**
24-
* Utility to return true for the first node.
25-
*
26-
* @type {findAfter~test}
27-
*/
28-
function first() {
29-
return true;
30-
}
31-
32-
/**
33-
* Utility to convert a string into a function which checks
34-
* a given node’s type for said string.
35-
*
36-
* @param {string} test - Node type to test.
37-
* @return {findAfter~test} - Tester.
13+
/*
14+
* Dependencies.
3815
*/
39-
function typeFactory(test) {
40-
return function (node) {
41-
return Boolean(node && node.type === test);
42-
}
43-
}
4416

45-
/**
46-
* Utility to convert a node into a function which checks
47-
* a given node for strict equality.
48-
*
49-
* @param {Node} test - Node to test.
50-
* @return {findAfter~test} - Tester.
51-
*/
52-
function nodeFactory(test) {
53-
return function (node) {
54-
return Boolean(node && node === test);
55-
}
56-
}
17+
var is = require('unist-util-is');
5718

5819
/**
5920
* Find a node after `index` in `parent` which passes
@@ -62,7 +23,7 @@ function nodeFactory(test) {
6223
* @param {Node} parent - Parent to search in.
6324
* @param {number|Node} index - (Position of) node to
6425
* search after.
65-
* @param {string|Node|findAfter~test} test - Tester.
26+
* @param {*} test - See `wooorm/unist-util-is`.
6627
* @return {Node?} - A child node of `parent` which passes
6728
* `test`.
6829
*/
@@ -86,20 +47,10 @@ function findAfter(parent, index, test) {
8647
throw new Error('Expected positive finite index or child node');
8748
}
8849

89-
if (typeof test === 'string') {
90-
test = typeFactory(test);
91-
} else if (test && test.type) {
92-
test = nodeFactory(test);
93-
} else if (test === null || test === undefined) {
94-
test = first;
95-
} else if (typeof test !== 'function') {
96-
throw new Error('Expected function, string, or node as test');
97-
}
98-
9950
while (++index < length) {
10051
child = children[index];
10152

102-
if (test(child, index, parent)) {
53+
if (is(test, child, index, parent)) {
10354
return child;
10455
}
10556
}

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
"util",
1717
"utility"
1818
],
19+
"dependencies": {
20+
"unist-util-is": "^1.0.0"
21+
},
1922
"repository": {
2023
"type": "git",
2124
"url": "https://github.com/wooorm/unist-util-find-after.git"

readme.md

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,31 +72,11 @@ given).
7272

7373
* `index` (`number`) — Position of child to search after;
7474

75-
* `test` ([`Function`](#function-testnode-index-parent), `string`, or
76-
`Node`, optional)
77-
— Invoked for each following child of `parent` after `node` or `position`.
78-
When `test` returns truthy for the first time on a `child`, that `child` is
79-
returned.
80-
81-
Passing a `string` is equal to passing
82-
`function (node) {return node.type === test}`.
83-
84-
Passing a `node` is equal to passing
85-
`function (node) {return node === test}`, useful when checking if `test`
86-
comes after `index` or `node` in `parent`.
75+
* `test` (`Function`, `string`, or `Node`; optional)
76+
— See [`is()`](https://github.com/wooorm/unist-util-is#istest-node-index-parent-context).
8777

8878
**Returns**: `node?`, when found. Child node of `parent` which passes `test`.
8979

90-
### function test(node, index, parent)
91-
92-
**Parameters**:
93-
94-
* `node` (`Node`) — Node to test;
95-
* `index` (`number`) — Position of `node` in `parent`.
96-
* `parent` (`Node`) — Parent of `node`.
97-
98-
**Returns**: `boolean?`, whether this iteration passes.
99-
10080
## License
10181

10282
[MIT](LICENSE) © [Titus Wormer](http://wooorm.com)

test.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,22 @@ describe('unist-util-find-after', function () {
8080
assert.throws(function () {
8181
findAfter({
8282
'type': 'foo',
83-
'children': []
83+
'children': [{
84+
'type': 'bar'
85+
}, {
86+
'type': 'baz'
87+
}]
8488
}, 0, false);
8589
}, /Expected function, string, or node as test/);
8690

8791
assert.throws(function () {
8892
findAfter({
8993
'type': 'foo',
90-
'children': []
94+
'children': [{
95+
'type': 'bar'
96+
}, {
97+
'type': 'baz'
98+
}]
9199
}, 0, true);
92100
}, /Expected function, string, or node as test/);
93101
});

unist-util-find-after.js

Lines changed: 132 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,87 @@
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
*/
2996
function 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
*/
40107
function 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
*/
53120
function 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
});

unist-util-find-after.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)