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