Skip to content

Commit 3343854

Browse files
committed
Refactor to move implementation to lib/
1 parent ca3e386 commit 3343854

File tree

3 files changed

+76
-74
lines changed

3 files changed

+76
-74
lines changed

Diff for: index.js

+1-74
Original file line numberDiff line numberDiff line change
@@ -1,74 +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 the first node in `parent` before another `node` or before an index,
14-
* that passes `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 findAllBefore =
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-
let offset = -1
44-
45-
if (!parent || !parent.type || !parent.children) {
46-
throw new Error('Expected parent node')
47-
}
48-
49-
if (typeof index === 'number') {
50-
if (index < 0 || index === Number.POSITIVE_INFINITY) {
51-
throw new Error('Expected positive finite number as index')
52-
}
53-
} else {
54-
index = parent.children.indexOf(index)
55-
56-
if (index < 0) {
57-
throw new Error('Expected child node or index')
58-
}
59-
}
60-
61-
// Performance.
62-
if (index > parent.children.length) {
63-
index = parent.children.length
64-
}
65-
66-
while (++offset < index) {
67-
if (is(parent.children[offset], offset, parent)) {
68-
results.push(parent.children[offset])
69-
}
70-
}
71-
72-
return results
73-
}
74-
)
1+
export {findAllBefore} from './lib/index.js'

Diff for: lib/index.js

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 the first node in `parent` before another `node` or before an index,
14+
* that passes `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 findAllBefore =
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+
let offset = -1
44+
45+
if (!parent || !parent.type || !parent.children) {
46+
throw new Error('Expected parent node')
47+
}
48+
49+
if (typeof index === 'number') {
50+
if (index < 0 || index === Number.POSITIVE_INFINITY) {
51+
throw new Error('Expected positive finite number as index')
52+
}
53+
} else {
54+
index = parent.children.indexOf(index)
55+
56+
if (index < 0) {
57+
throw new Error('Expected child node or index')
58+
}
59+
}
60+
61+
// Performance.
62+
if (index > parent.children.length) {
63+
index = parent.children.length
64+
}
65+
66+
while (++offset < index) {
67+
if (is(parent.children[offset], offset, parent)) {
68+
results.push(parent.children[offset])
69+
}
70+
}
71+
72+
return results
73+
}
74+
)

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"main": "index.js",
2828
"types": "index.d.ts",
2929
"files": [
30+
"lib/",
3031
"index.d.ts",
3132
"index.js"
3233
],

0 commit comments

Comments
 (0)