Skip to content

Commit 9ef08e0

Browse files
committed
Refactor to move implementation to lib/
1 parent 6364426 commit 9ef08e0

File tree

4 files changed

+113
-108
lines changed

4 files changed

+113
-108
lines changed

index.js

Lines changed: 2 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,7 @@
11
/**
2-
* @typedef {import('unist').Node} Node
3-
* @typedef {import('unist').Parent} Parent
4-
* @typedef {import('unist-util-is').Test} Test
5-
*
6-
* @typedef Options
7-
* Configuration (optional).
8-
* @property {boolean} [cascade=true]
9-
* Whether to drop parent nodes if they had children, but all their children
10-
* were filtered out.
11-
*
2+
* @typedef {import('./lib/index.js').Options} Options
123
* @typedef {Options} FilterOptions
134
* Deprecated, use `Options`.
145
*/
156

16-
import {convert} from 'unist-util-is'
17-
18-
const own = {}.hasOwnProperty
19-
20-
/**
21-
* Create a new `tree` of copies of all nodes that pass `test`.
22-
* The tree is walked in preorder (NLR), visiting the node itself, then its
23-
* head, etc.
24-
*
25-
* @param tree
26-
* Tree to filter.
27-
* @param options
28-
* Configuration (optional).
29-
* @param test
30-
* `unist-util-is`-compatible test (such as a type).
31-
* @returns
32-
* New filtered tree.
33-
* `null` is returned if `tree` itself didn’t pass the test, or is cascaded
34-
* away.
35-
*/
36-
export const filter =
37-
/**
38-
* @type {(
39-
* (<Tree extends Node, Check extends Test>(node: Tree, options: Options, test: Check) => import('./complex-types.js').Matches<Tree, Check>) &
40-
* (<Tree extends Node, Check extends Test>(node: Tree, test: Check) => import('./complex-types.js').Matches<Tree, Check>) &
41-
* (<Tree extends Node>(node: Tree, options?: Options) => Tree)
42-
* )}
43-
*/
44-
(
45-
/**
46-
* @param {Node} tree
47-
* @param {Options} options
48-
* @param {Test} test
49-
* @returns {Node|null}
50-
*/
51-
function (tree, options, test) {
52-
const is = convert(test || options)
53-
const cascade =
54-
options.cascade === undefined || options.cascade === null
55-
? true
56-
: options.cascade
57-
58-
return preorder(tree)
59-
60-
/**
61-
* @param {Node} node
62-
* @param {number|undefined} [index]
63-
* @param {Parent|undefined} [parent]
64-
* @returns {Node|null}
65-
*/
66-
function preorder(node, index, parent) {
67-
/** @type {Array<Node>} */
68-
const children = []
69-
/** @type {number} */
70-
let childIndex
71-
/** @type {Node} */
72-
let result
73-
/** @type {string} */
74-
let key
75-
76-
if (!is(node, index, parent)) return null
77-
78-
// @ts-expect-error: Looks like a parent.
79-
if (node.children) {
80-
childIndex = -1
81-
82-
// @ts-expect-error Looks like a parent.
83-
while (++childIndex < node.children.length) {
84-
// @ts-expect-error Looks like a parent.
85-
result = preorder(node.children[childIndex], childIndex, node)
86-
87-
if (result) {
88-
children.push(result)
89-
}
90-
}
91-
92-
// @ts-expect-error Looks like a parent.
93-
if (cascade && node.children.length > 0 && children.length === 0)
94-
return null
95-
}
96-
97-
// Create a shallow clone, using the new children.
98-
/** @type {typeof node} */
99-
// @ts-expect-error all the fields will be copied over.
100-
const next = {}
101-
102-
for (key in node) {
103-
if (own.call(node, key)) {
104-
// @ts-expect-error: Looks like a record.
105-
next[key] = key === 'children' ? children : node[key]
106-
}
107-
}
108-
109-
return next
110-
}
111-
}
112-
)
7+
export {filter} from './lib/index.js'

lib/index.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* @typedef {import('unist').Node} Node
3+
* @typedef {import('unist').Parent} Parent
4+
* @typedef {import('unist-util-is').Test} Test
5+
*
6+
* @typedef Options
7+
* Configuration (optional).
8+
* @property {boolean} [cascade=true]
9+
* Whether to drop parent nodes if they had children, but all their children
10+
* were filtered out.
11+
*/
12+
13+
import {convert} from 'unist-util-is'
14+
15+
const own = {}.hasOwnProperty
16+
17+
/**
18+
* Create a new `tree` of copies of all nodes that pass `test`.
19+
* The tree is walked in preorder (NLR), visiting the node itself, then its
20+
* head, etc.
21+
*
22+
* @param tree
23+
* Tree to filter.
24+
* @param options
25+
* Configuration (optional).
26+
* @param test
27+
* `unist-util-is`-compatible test (such as a type).
28+
* @returns
29+
* New filtered tree.
30+
* `null` is returned if `tree` itself didn’t pass the test, or is cascaded
31+
* away.
32+
*/
33+
export const filter =
34+
/**
35+
* @type {(
36+
* (<Tree extends Node, Check extends Test>(node: Tree, options: Options, test: Check) => import('../complex-types.js').Matches<Tree, Check>) &
37+
* (<Tree extends Node, Check extends Test>(node: Tree, test: Check) => import('../complex-types.js').Matches<Tree, Check>) &
38+
* (<Tree extends Node>(node: Tree, options?: Options) => Tree)
39+
* )}
40+
*/
41+
(
42+
/**
43+
* @param {Node} tree
44+
* @param {Options} options
45+
* @param {Test} test
46+
* @returns {Node|null}
47+
*/
48+
function (tree, options, test) {
49+
const is = convert(test || options)
50+
const cascade =
51+
options.cascade === undefined || options.cascade === null
52+
? true
53+
: options.cascade
54+
55+
return preorder(tree)
56+
57+
/**
58+
* @param {Node} node
59+
* @param {number|undefined} [index]
60+
* @param {Parent|undefined} [parent]
61+
* @returns {Node|null}
62+
*/
63+
function preorder(node, index, parent) {
64+
/** @type {Array<Node>} */
65+
const children = []
66+
/** @type {number} */
67+
let childIndex
68+
/** @type {Node} */
69+
let result
70+
/** @type {string} */
71+
let key
72+
73+
if (!is(node, index, parent)) return null
74+
75+
// @ts-expect-error: Looks like a parent.
76+
if (node.children) {
77+
childIndex = -1
78+
79+
// @ts-expect-error Looks like a parent.
80+
while (++childIndex < node.children.length) {
81+
// @ts-expect-error Looks like a parent.
82+
result = preorder(node.children[childIndex], childIndex, node)
83+
84+
if (result) {
85+
children.push(result)
86+
}
87+
}
88+
89+
// @ts-expect-error Looks like a parent.
90+
if (cascade && node.children.length > 0 && children.length === 0)
91+
return null
92+
}
93+
94+
// Create a shallow clone, using the new children.
95+
/** @type {typeof node} */
96+
// @ts-expect-error all the fields will be copied over.
97+
const next = {}
98+
99+
for (key in node) {
100+
if (own.call(node, key)) {
101+
// @ts-expect-error: Looks like a record.
102+
next[key] = key === 'children' ? children : node[key]
103+
}
104+
}
105+
106+
return next
107+
}
108+
}
109+
)

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
"complex-types.d.ts",
3334
"index.d.ts",
3435
"index.js"

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"include": ["**/*.js"],
2+
"include": ["**/*.js", "complex-types.d.ts"],
33
"exclude": ["coverage/", "node_modules/"],
44
"compilerOptions": {
55
"checkJs": true,

0 commit comments

Comments
 (0)