Skip to content

Commit 3240fc3

Browse files
committed
Refactor to move implementation to lib/
1 parent 35a59c3 commit 3240fc3

File tree

3 files changed

+70
-68
lines changed

3 files changed

+70
-68
lines changed

index.js

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1 @@
1-
/**
2-
* @typedef {import('unist').Node} Node
3-
* @typedef {import('unist').Parent} Parent
4-
*
5-
* @typedef {string} Type
6-
* @typedef {Record<string, unknown>} Props
7-
* @typedef {import('unist-util-is').TestFunctionAnything} TestFunctionAnything
8-
*/
9-
10-
import {convert} from 'unist-util-is'
11-
12-
/**
13-
* Find nodes in `parent` after another `node` or after an index, that pass
14-
* `test`.
15-
*
16-
* @param parent
17-
* Parent node.
18-
* @param index
19-
* Child of `parent` or it’s index.
20-
* @param [test]
21-
* `unist-util-is`-compatible test.
22-
* @returns
23-
* Children of `parent` that pass `test`.
24-
*/
25-
export const findAllAfter =
26-
/**
27-
* @type {(
28-
* (<T extends Node>(node: Parent, index: Node|number, test: T['type']|Partial<T>|import('unist-util-is').TestFunctionPredicate<T>|Array<T['type']|Partial<T>|import('unist-util-is').TestFunctionPredicate<T>>) => Array<T>) &
29-
* ((node: Parent, index: Node|number, test?: null|undefined|Type|Props|TestFunctionAnything|Array<Type|Props|TestFunctionAnything>) => Array<Node>)
30-
* )}
31-
*/
32-
(
33-
/**
34-
* @param {Parent} parent
35-
* @param {Node|number} index
36-
* @param {null|undefined|Type|Props|TestFunctionAnything|Array<Type|Props|TestFunctionAnything>} [test]
37-
* @returns {Array<Node>}
38-
*/
39-
function (parent, index, test) {
40-
const is = convert(test)
41-
/** @type {Array<Node>} */
42-
const results = []
43-
44-
if (!parent || !parent.type || !parent.children) {
45-
throw new Error('Expected parent node')
46-
}
47-
48-
if (typeof index === 'number') {
49-
if (index < 0 || index === Number.POSITIVE_INFINITY) {
50-
throw new Error('Expected positive finite number as index')
51-
}
52-
} else {
53-
index = parent.children.indexOf(index)
54-
55-
if (index < 0) {
56-
throw new Error('Expected child node or index')
57-
}
58-
}
59-
60-
while (++index < parent.children.length) {
61-
if (is(parent.children[index], index, parent)) {
62-
results.push(parent.children[index])
63-
}
64-
}
65-
66-
return results
67-
}
68-
)
1+
export {findAllAfter} from './lib/index.js'

lib/index.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* @typedef {import('unist').Node} Node
3+
* @typedef {import('unist').Parent} Parent
4+
*
5+
* @typedef {string} Type
6+
* @typedef {Record<string, unknown>} Props
7+
* @typedef {import('unist-util-is').TestFunctionAnything} TestFunctionAnything
8+
*/
9+
10+
import {convert} from 'unist-util-is'
11+
12+
/**
13+
* Find nodes in `parent` after another `node` or after an index, that pass
14+
* `test`.
15+
*
16+
* @param parent
17+
* Parent node.
18+
* @param index
19+
* Child of `parent` or it’s index.
20+
* @param [test]
21+
* `unist-util-is`-compatible test.
22+
* @returns
23+
* Children of `parent` that pass `test`.
24+
*/
25+
export const findAllAfter =
26+
/**
27+
* @type {(
28+
* (<T extends Node>(node: Parent, index: Node|number, test: T['type']|Partial<T>|import('unist-util-is').TestFunctionPredicate<T>|Array<T['type']|Partial<T>|import('unist-util-is').TestFunctionPredicate<T>>) => Array<T>) &
29+
* ((node: Parent, index: Node|number, test?: null|undefined|Type|Props|TestFunctionAnything|Array<Type|Props|TestFunctionAnything>) => Array<Node>)
30+
* )}
31+
*/
32+
(
33+
/**
34+
* @param {Parent} parent
35+
* @param {Node|number} index
36+
* @param {null|undefined|Type|Props|TestFunctionAnything|Array<Type|Props|TestFunctionAnything>} [test]
37+
* @returns {Array<Node>}
38+
*/
39+
function (parent, index, test) {
40+
const is = convert(test)
41+
/** @type {Array<Node>} */
42+
const results = []
43+
44+
if (!parent || !parent.type || !parent.children) {
45+
throw new Error('Expected parent node')
46+
}
47+
48+
if (typeof index === 'number') {
49+
if (index < 0 || index === Number.POSITIVE_INFINITY) {
50+
throw new Error('Expected positive finite number as index')
51+
}
52+
} else {
53+
index = parent.children.indexOf(index)
54+
55+
if (index < 0) {
56+
throw new Error('Expected child node or index')
57+
}
58+
}
59+
60+
while (++index < parent.children.length) {
61+
if (is(parent.children[index], index, parent)) {
62+
results.push(parent.children[index])
63+
}
64+
}
65+
66+
return results
67+
}
68+
)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"main": "index.js",
3030
"types": "index.d.ts",
3131
"files": [
32+
"lib/",
3233
"index.d.ts",
3334
"index.js"
3435
],

0 commit comments

Comments
 (0)