Skip to content

Commit 4426b2c

Browse files
committed
feat: deep scan object cases
1 parent e162c3d commit 4426b2c

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

packages/copy/src/utils/helper.ts

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,50 +9,51 @@
99
const weakSet = new WeakSet();
1010
const pathRouter = ["root"];
1111
const regexp = //;
12+
const log = console.table;
13+
14+
const isKeywords = (key, value) => {
15+
return (
16+
value instanceof Node ||
17+
value instanceof Window ||
18+
key.includes("_react") ||
19+
key.includes("cssRule") ||
20+
key.includes("previousSibling") ||
21+
key.includes("nextSibling") ||
22+
key.includes("previousElementSibling") ||
23+
key.includes("nextElementSibling")
24+
);
25+
};
1226

1327
const deepScanObject = (origin, deep, maxDeep) => {
14-
if (deep > maxDeep) return;
15-
if (origin instanceof Map) {
16-
weakSet.add(origin);
17-
if (weakSet.has(origin)) return;
18-
origin = [...origin.entries()];
19-
}
20-
for (const item in origin) {
21-
const currentPath = pathRouter.join("/") + "/" + item;
28+
if (deep > maxDeep || weakSet.has(origin) || ArrayBuffer.isView(origin)) return;
29+
if (origin instanceof Map) origin = [...origin.entries()];
30+
for (const _item in origin) {
31+
const currentPath = pathRouter.join("/") + "/" + _item;
2232
try {
33+
const item = String(_item);
2334
const value = origin[item];
24-
if (
25-
value instanceof Node ||
26-
value instanceof Window ||
27-
item.includes("_react") ||
28-
item.includes("cssRule")
29-
) {
30-
continue;
31-
}
35+
if (isKeywords(item, value)) continue;
3236
if (value && typeof value === "object") {
33-
if (weakSet.has(value)) {
34-
regexp.test(item) && console.log(currentPath);
35-
continue;
36-
}
37-
weakSet.add(value);
37+
regexp.test(item) && log(currentPath, "===>", value);
38+
if (weakSet.has(value)) continue;
3839
pathRouter.push(item);
3940
deepScanObject(value, deep + 1, maxDeep);
41+
weakSet.add(value);
4042
pathRouter.pop();
41-
} else {
42-
if (regexp.test(item) || regexp.test(value)) {
43-
console.log(currentPath, "================", value);
44-
}
43+
} else if (regexp.test(item) || regexp.test(String(value))) {
44+
if (typeof value === "function") continue;
45+
log(currentPath, "===>", value);
4546
}
4647
} catch (e) {
47-
console.warn(e);
48+
log("warn", "--->", e);
4849
}
4950
}
5051
};
5152

5253
deepScanObject.toString = () => "";
53-
console.log("start");
54+
log("start");
5455
deepScanObject(window, 0, 10);
55-
console.log("finish");
56+
log("finish");
5657

5758
// =========================================================================== //
5859

0 commit comments

Comments
 (0)