This repository was archived by the owner on Jun 14, 2024. It is now read-only.
generated from kernel-addons/PluginTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreesearcher.js
115 lines (84 loc) · 3.09 KB
/
treesearcher.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
export default class TreeSearcher {
constructor(target, type) {
this._current = target;
this._break = false;
switch (type) {
case "react": {
this.defaultWalkable = ["props", "children"];
} break;
case "react-vdom": {
this.defaultWalkable = ["child", "return", "alternate"];
} break;
default: {
this.defaultWalkable = [];
};
}
}
type() {return typeof this._current;}
isNull() {return this._current == null;}
isArray() {return this._break = !Array.isArray(this._current), this;}
isNumber() {return this._break = this.type() !== "number", this;}
isFunction() {return this._break = this.type() !== "function", this;}
isObject() {return this._break = !(this.type() === "object" && this._current !== null), this;}
where(condition) {return this._break = !condition.call(this, this.value(), this), this;}
walk(...path) {
if (this._break) return this;
for (let i = 0; i < path.length; i++) {
if (!this._current) break;
this._current = this._current?.[path[i]];
}
if (!this._current) this._break = true;
return this;
}
find(filter, {ignore = [], walkable = this.defaultWalkable, maxProperties = 100} = {}) {
if (this._break) return this;
const stack = [this._current];
while (stack.length && maxProperties) {
const node = stack.shift();
if (filter(node)) {
this._current = node;
return this;
}
if (Array.isArray(node)) stack.push(...node);
else if (typeof node === "object" && node !== null) {
for (const key in node) {
const value = node[key];
if (
(walkable.length && (~walkable.indexOf(key) && !~ignore.indexOf(key))) ||
node && ~ignore.indexOf(key)
) {
stack.push(value);
}
}
}
maxProperties--;
}
this._break = true;
this._current = null;
return this;
}
put(factory) {
if (this._break) return this;
const value = this._current = factory.call(this, this.value(), this);
if (value == null) this._break = true;
return this;
}
call(_this, ...args) {
if (this._break) return this;
const value = this._current = this._current.call(_this, ...args);
if (value == null) this._break = true;
return this;
}
break() {return this._break = true, this;}
value() {return this._current;}
toString() {return String(this._current);}
then(onSuccess, onError) {
return Promise.resolve(this._current)
.then(
value => (onSuccess.call(this, value), this),
onError
? (error) => (onError(error), this)
: void 0
);
}
}