-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
115 lines (104 loc) · 3.45 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* @typedef {import('unist').Node} Node
* @typedef {import('unist').Parent} Parent
*
* @typedef {import('unist-util-is').Type} Type
* @typedef {import('unist-util-is').Props} Props
*
* @typedef Options
* @property {boolean} [cascade=true]
* Whether to drop parent nodes if they had children, but all their children
* were filtered out.
*
* @typedef {Options} RemoveOptions
* @deprecated
* Use `Options` instead.
*/
/**
* Check if a node passes a test.
*
* @template {Node} Tree
* Node type that is checked for.
* @callback TestFunction
* @param {Tree} node
* @param {number|null|undefined} [index]
* @param {Parent|null|undefined} [parent]
* @returns {boolean|void}
*/
import {convert} from 'unist-util-is'
/** @type {Array<unknown>} */
const empty = []
/**
* Mutate the given `tree` by removing all nodes that pass `test`.
* The tree is walked in preorder (NLR), visiting the node itself, then its
* head, etc.
*
* @param tree
* Tree to change.
* @param [options]
* Configuration (optional).
* @param [test]
* `unist-util-is`-compatible test.
* @returns
* The given `tree` without nodes that pass `test`.
* `null` is returned if `tree` itself didn’t pass the test or is cascaded
* away.
*/
export const remove =
/**
* @type {(
* (<Tree extends Node>(node: Tree, options: RemoveOptions, test: Type|Props|TestFunction<import('unist-util-visit-parents/complex-types').InclusiveDescendant<Tree>>|Array<Type|Props|TestFunction<import('unist-util-visit-parents/complex-types').InclusiveDescendant<Tree>>>) => Tree|null) &
* (<Tree extends Node>(node: Tree, test: Type|Props|TestFunction<import('unist-util-visit-parents/complex-types').InclusiveDescendant<Tree>>|Array<Type|Props|TestFunction<import('unist-util-visit-parents/complex-types').InclusiveDescendant<Tree>>>) => Tree|null)
* )}
*/
(
/**
* @param {Node} tree
* @param {RemoveOptions} [options]
* @param {Type|Props|TestFunction<Node>|Array<Type|Props|TestFunction<Node>>} [test]
* @returns {Node|null}
*/
function (tree, options, test) {
const is = convert(test || options)
const cascade =
!options || options.cascade === undefined || options.cascade === null
? true
: options.cascade
return preorder(tree)
/**
* Check and remove nodes recursively in preorder.
* For each composite node, modify its children array in-place.
*
* @param {Node} node
* @param {number|undefined} [index]
* @param {Parent|undefined} [parent]
* @returns {Node|null}
*/
function preorder(node, index, parent) {
/** @type {Array<Node>} */
// @ts-expect-error looks like a parent.
const children = node.children || empty
let childIndex = -1
let position = 0
if (is(node, index, parent)) {
return null
}
if (children.length > 0) {
// Move all living children to the beginning of the children array.
while (++childIndex < children.length) {
// @ts-expect-error looks like a parent.
if (preorder(children[childIndex], childIndex, node)) {
children[position++] = children[childIndex]
}
}
// Cascade delete.
if (cascade && !position) {
return null
}
// Drop other nodes.
children.length = position
}
return node
}
}
)