|
1 | 1 | import {convert} from 'unist-util-is'
|
2 | 2 |
|
| 3 | +/** |
| 4 | + * @typedef {import('unist').Node} Node |
| 5 | + * @typedef {import('unist').Parent} Parent |
| 6 | + * |
| 7 | + * @typedef {import('unist-util-is').Type} Type |
| 8 | + * @typedef {import('unist-util-is').Props} Props |
| 9 | + * @typedef {import('unist-util-is').TestFunctionAnything} TestFunctionAnything |
| 10 | + */ |
| 11 | + |
| 12 | +/** |
| 13 | + * Options for unist util filter |
| 14 | + * |
| 15 | + * @typedef {Object} FilterOptions |
| 16 | + * @property {boolean} [cascade=true] Whether to drop parent nodes if they had children, but all their children were filtered out. |
| 17 | + */ |
| 18 | + |
3 | 19 | var own = {}.hasOwnProperty
|
4 | 20 |
|
5 |
| -export function filter(tree, options, test) { |
6 |
| - var is = convert(test || options) |
7 |
| - var cascade = |
8 |
| - options.cascade === undefined || options.cascade === null |
9 |
| - ? true |
10 |
| - : options.cascade |
| 21 | +export const filter = |
| 22 | + /** |
| 23 | + * @type {( |
| 24 | + * (<T extends Node>(node: Node, options: FilterOptions, test: T['type']|Partial<T>|import('unist-util-is').TestFunctionPredicate<T>|Array.<T['type']|Partial<T>|import('unist-util-is').TestFunctionPredicate<T>>) => T|null) & |
| 25 | + * (<T extends Node>(node: Node, test: T['type']|Partial<T>|import('unist-util-is').TestFunctionPredicate<T>|Array.<T['type']|Partial<T>|import('unist-util-is').TestFunctionPredicate<T>>) => T|null) & |
| 26 | + * ((node: Node, options: FilterOptions, test?: null|undefined|Type|Props|TestFunctionAnything|Array<Type|Props|TestFunctionAnything>) => Node|null) & |
| 27 | + * ((node: Node, test?: null|undefined|Type|Props|TestFunctionAnything|Array<Type|Props|TestFunctionAnything>) => Node|null) |
| 28 | + * )} |
| 29 | + */ |
| 30 | + ( |
| 31 | + /** |
| 32 | + * Create a new tree consisting of copies of all nodes that pass test. |
| 33 | + * The tree is walked in preorder (NLR), visiting the node itself, then its head, etc. |
| 34 | + * |
| 35 | + * @param {Node} tree Tree to filter |
| 36 | + * @param {FilterOptions} options |
| 37 | + * @param {null|undefined|Type|Props|TestFunctionAnything|Array<Type|Props|TestFunctionAnything>} test is-compatible test (such as a type) |
| 38 | + * @returns {Node|null} |
| 39 | + */ |
| 40 | + function (tree, options, test) { |
| 41 | + var is = convert(test || options) |
| 42 | + var cascade = |
| 43 | + options.cascade === undefined || options.cascade === null |
| 44 | + ? true |
| 45 | + : options.cascade |
11 | 46 |
|
12 |
| - return preorder(tree, null, null) |
| 47 | + return preorder(tree) |
13 | 48 |
|
14 |
| - function preorder(node, index, parent) { |
15 |
| - var children |
16 |
| - var childIndex |
17 |
| - var result |
18 |
| - var next |
19 |
| - var key |
| 49 | + /** |
| 50 | + * @param {Node} node |
| 51 | + * @param {number|undefined} [index] |
| 52 | + * @param {Parent|undefined} [parent] |
| 53 | + * @returns {Node|null} |
| 54 | + */ |
| 55 | + function preorder(node, index, parent) { |
| 56 | + /** @type {Array.<Node>} */ |
| 57 | + var children = [] |
| 58 | + /** @type {number} */ |
| 59 | + var childIndex |
| 60 | + /** @type {Node} */ |
| 61 | + var result |
| 62 | + /** @type {typeof node} */ |
| 63 | + var next |
| 64 | + /** @type {string} */ |
| 65 | + var key |
20 | 66 |
|
21 |
| - if (!is(node, index, parent)) return null |
| 67 | + if (!is(node, index, parent)) return null |
22 | 68 |
|
23 |
| - if (node.children) { |
24 |
| - children = [] |
25 |
| - childIndex = -1 |
| 69 | + if (node.children) { |
| 70 | + childIndex = -1 |
26 | 71 |
|
27 |
| - while (++childIndex < node.children.length) { |
28 |
| - result = preorder(node.children[childIndex], childIndex, node) |
| 72 | + // @ts-ignore Looks like a parent. |
| 73 | + while (++childIndex < node.children.length) { |
| 74 | + // @ts-ignore Looks like a parent. |
| 75 | + result = preorder(node.children[childIndex], childIndex, node) |
29 | 76 |
|
30 |
| - if (result) { |
31 |
| - children.push(result) |
| 77 | + if (result) { |
| 78 | + children.push(result) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // @ts-ignore Looks like a parent. |
| 83 | + if (cascade && node.children.length && !children.length) return null |
32 | 84 | }
|
33 |
| - } |
34 | 85 |
|
35 |
| - if (cascade && node.children.length && !children.length) return null |
36 |
| - } |
| 86 | + // Create a shallow clone, using the new children. |
| 87 | + // @ts-ignore all the fields will be copied over. |
| 88 | + next = {} |
37 | 89 |
|
38 |
| - // Create a shallow clone, using the new children. |
39 |
| - next = {} |
| 90 | + for (key in node) { |
| 91 | + /* istanbul ignore else - Prototype injection. */ |
| 92 | + if (own.call(node, key)) { |
| 93 | + next[key] = key === 'children' ? children : node[key] |
| 94 | + } |
| 95 | + } |
40 | 96 |
|
41 |
| - for (key in node) { |
42 |
| - /* istanbul ignore else - Prototype injection. */ |
43 |
| - if (own.call(node, key)) { |
44 |
| - next[key] = key === 'children' ? children : node[key] |
| 97 | + return next |
45 | 98 | }
|
46 | 99 | }
|
47 |
| - |
48 |
| - return next |
49 |
| - } |
50 |
| -} |
| 100 | + ) |
0 commit comments