Skip to content

Commit 6560841

Browse files
committed
Refactor to move implementation to lib/
1 parent da65bb6 commit 6560841

File tree

3 files changed

+52
-50
lines changed

3 files changed

+52
-50
lines changed

index.js

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +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-
/** @type {Array<Node>} */
13-
let empty
14-
15-
/**
16-
* Calculate the number of nodes in `node`.
17-
*
18-
* @param {Node} node
19-
* Tree to traverse.
20-
* @param {null|undefined|Type|Props|TestFunctionAnything|Array<Type|Props|TestFunctionAnything>} [test]
21-
* `unist-util-is`-compatible test (such as a node type).
22-
* @returns {number}
23-
* Exclusive descendants of `node` that pass `test`.
24-
*/
25-
export function size(node, test) {
26-
const is = convert(test)
27-
28-
return fastSize(node)
29-
30-
/**
31-
* @param {Node} node
32-
*/
33-
function fastSize(node) {
34-
/** @type {Array<Node>} */
35-
// @ts-expect-error Looks like a parent.
36-
const children = (node && node.children) || empty
37-
let count = 0
38-
let index = -1
39-
40-
if (children && children.length > 0) {
41-
while (++index < children.length) {
42-
// @ts-expect-error Looks like a parent.
43-
if (is(children[index], index, node)) count++
44-
count += fastSize(children[index])
45-
}
46-
}
47-
48-
return count
49-
}
50-
}
1+
export {size} from './lib/index.js'

lib/index.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
/** @type {Array<Node>} */
13+
let empty
14+
15+
/**
16+
* Calculate the number of nodes in `node`.
17+
*
18+
* @param {Node} node
19+
* Tree to traverse.
20+
* @param {null|undefined|Type|Props|TestFunctionAnything|Array<Type|Props|TestFunctionAnything>} [test]
21+
* `unist-util-is`-compatible test (such as a node type).
22+
* @returns {number}
23+
* Exclusive descendants of `node` that pass `test`.
24+
*/
25+
export function size(node, test) {
26+
const is = convert(test)
27+
28+
return fastSize(node)
29+
30+
/**
31+
* @param {Node} node
32+
*/
33+
function fastSize(node) {
34+
/** @type {Array<Node>} */
35+
// @ts-expect-error Looks like a parent.
36+
const children = (node && node.children) || empty
37+
let count = 0
38+
let index = -1
39+
40+
if (children && children.length > 0) {
41+
while (++index < children.length) {
42+
// @ts-expect-error Looks like a parent.
43+
if (is(children[index], index, node)) count++
44+
count += fastSize(children[index])
45+
}
46+
}
47+
48+
return count
49+
}
50+
}

package.json

Lines changed: 1 addition & 0 deletions
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)