|
1 | 1 | 'use strict'
|
2 | 2 |
|
3 |
| -var flatmap = require('flatmap') |
4 | 3 | var convert = require('unist-util-is/convert')
|
5 | 4 |
|
6 | 5 | module.exports = filter
|
7 | 6 |
|
8 |
| -function filter(tree, options, test) { |
9 |
| - var is |
10 |
| - var cascade |
11 |
| - |
12 |
| - if (!test) { |
13 |
| - test = options |
14 |
| - options = {} |
15 |
| - } |
| 7 | +var own = {}.hasOwnProperty |
16 | 8 |
|
17 |
| - cascade = options.cascade |
18 |
| - cascade = cascade === null || cascade === undefined ? true : cascade |
19 |
| - is = convert(test) |
| 9 | +function filter(tree, options, test) { |
| 10 | + var is = convert(test || options) |
| 11 | + var cascade = options.cascade == null ? true : options.cascade |
20 | 12 |
|
21 | 13 | return preorder(tree, null, null)
|
22 | 14 |
|
23 | 15 | function preorder(node, index, parent) {
|
| 16 | + var children |
| 17 | + var childIndex |
| 18 | + var result |
24 | 19 | var next
|
| 20 | + var key |
25 | 21 |
|
26 |
| - if (!is(node, index, parent)) { |
27 |
| - return null |
28 |
| - } |
29 |
| - |
30 |
| - next = Object.keys(node).reduce(reduce, {}) |
| 22 | + if (!is(node, index, parent)) return null |
31 | 23 |
|
32 | 24 | if (node.children) {
|
33 |
| - next.children = flatmap(node.children, map) |
| 25 | + children = [] |
| 26 | + childIndex = -1 |
34 | 27 |
|
35 |
| - if (cascade && node.children.length > 0 && next.children.length === 0) { |
36 |
| - return null |
| 28 | + while (++childIndex < node.children.length) { |
| 29 | + result = preorder(node.children[childIndex], childIndex, node) |
| 30 | + |
| 31 | + if (result) { |
| 32 | + children.push(result) |
| 33 | + } |
37 | 34 | }
|
| 35 | + |
| 36 | + if (cascade && node.children.length && !children.length) return null |
38 | 37 | }
|
39 | 38 |
|
40 |
| - return next |
| 39 | + // Create a shallow clone, using the new children. |
| 40 | + next = {} |
41 | 41 |
|
42 |
| - function reduce(acc, key) { |
43 |
| - if (key !== 'children') { |
44 |
| - acc[key] = node[key] |
| 42 | + for (key in node) { |
| 43 | + /* istanbul ignore else - Prototype injection. */ |
| 44 | + if (own.call(node, key)) { |
| 45 | + next[key] = key === 'children' ? children : node[key] |
45 | 46 | }
|
46 |
| - |
47 |
| - return acc |
48 | 47 | }
|
49 | 48 |
|
50 |
| - function map(child, index) { |
51 |
| - return preorder(child, index, node) |
52 |
| - } |
| 49 | + return next |
53 | 50 | }
|
54 | 51 | }
|
0 commit comments