/
index.js
140 lines (127 loc) · 3.83 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* Enhanced and multifunctional tree walker
* @author imcuttle
*/
const crawl = require('tree-crawl')
const get = require('lodash.get')
const set = require('lodash.set')
const unset = require('lodash.unset')
function toArray(data) {
if (!Array.isArray(data)) {
return [data]
}
return data
}
function proxy(ctx, path, getNewDescriptor) {
const old = ctx[path]
const host = ctx // ctx.hasOwnProperty(path) ? ctx : Object.getPrototypeOf(ctx)
Object.defineProperty(
host,
path,
Object.assign({}, Object.getOwnPropertyDescriptor(host, path), getNewDescriptor(old))
)
return ctx
}
const symbol = typeof Symbol === 'function' ? Symbol('override') : '__override__'
const childrenIsNotArraySymbol =
typeof Symbol === 'function' ? Symbol('childrenIsNotArraySymbol') : '__childrenIsNotArraySymbol__'
/**
*
* @public
* @param tree {T} - Type `T` should extends Object
* @param walker {(node, ctx: Context) => {}} - Iterator for each node by order
* @param opts {object}
* @param [opts.path='children'] {string} - The child's path on recursive struction
* @param [opts.order='pre'] {'pre' | 'post' | 'bfs'}
* <br/>
* `pre` means walking the node before walking it's children node by dfs <br/>
* `post` means walking the node after walking it's children node by dfs <br/>
* `bfs` means walking the node by bfs <br/>
* @param [opts.skipVisited=true] {boolean}
* Should skip the node which has been visited.
* @param [opts.uniquePath=node => node] {Function | string | null}
* The unique's path for determining the node has been visited (same node)
* @param [opts.state] {any}
* Inject in `context.state` on customized way
* @return walkedTree {T}
*/
function walk(
tree,
walker = (node, ctx) => {},
{ path = 'children', order = 'pre', skipVisited = true, uniquePath = node => node, state } = {}
) {
let getUniq
if (typeof uniquePath === 'function') {
getUniq = uniquePath
} else {
getUniq = typeof uniquePath === 'string' ? node => get(node, uniquePath) : v => v
}
crawl(
tree,
function(node, ctx) {
if (!ctx[symbol]) {
ctx.state = state
proxy(ctx, 'track', () => {
return {
configurable: true,
enumerable: false,
value: new Map()
}
})
proxy(ctx, 'remove', oldRemove => {
return {
enumerable: true,
value: function remove() {
const children = get(this.parent, path)
if (!Array.isArray(children)) {
unset(this.parent, path)
} else {
children.splice(this.index, 1)
}
return oldRemove.apply(this, arguments)
}
}
})
proxy(ctx, 'replace', oldReplace => {
return {
enumerable: true,
value: function replace(node) {
const children = get(this.parent, path)
if (!Array.isArray(children)) {
set(this.parent, path, node)
} else {
children[this.index] = node
}
return oldReplace.apply(this, arguments)
}
}
})
Object.defineProperty(ctx, symbol, {
configurable: true,
enumerable: false,
value: true
})
}
const { track } = ctx
const uniqKey = getUniq(node)
const visitedStatus = track.get(uniqKey)
if (skipVisited && visitedStatus === 'visited') {
ctx.skip()
return
}
track.set(uniqKey, 'visiting')
const rlt = walker(node, ctx)
track.set(uniqKey, 'visited')
return rlt
},
{
getChildren: node => {
const children = get(node, path)
return children != null ? toArray(children) : children
},
order
}
)
return tree
}
module.exports = walk