-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
93 lines (74 loc) · 1.85 KB
/
utils.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
const logProxy = (sourceName, initialState) => {
return new Proxy(initialState, {
get(target, prop) {
console.log(`${sourceName}.${prop} = ${target[prop]}`);
return target[prop];
},
set(target, prop, value) {
console.log(`${sourceName}.${prop} = ${value}`);
target[prop] = value;
return true;
},
});
};
const isStringNodeByTypeof = (type) => {
if (type === "string" || type === "number" || type === "boolean") {
return true;
}
return false;
};
const cn = (...args) => {
if (typeof args[0] === "function") {
return react(() => {
const result = args[0]();
return cn(result);
});
}
return args.flat().filter(Boolean).join(" ");
};
const idGenerator = () => {
let id = 1;
return () => id++;
};
const swipeItemsOnArray = (array, from, to) => {
const temp = array[from];
array[from] = array[to];
array[to] = temp;
};
function hash(obj) {
const jsonString = JSON.stringify(obj);
let hash = 0;
for (let i = 0; i < jsonString.length; i++) {
const char = jsonString.charCodeAt(i);
hash = (hash << 5) - hash + char;
hash |= 0; // Convert to 32-bit integer
}
return hash;
}
const hasDuplicates = (arr) => new Set(arr).size !== arr.length;
function findDuplicates(arr) {
const seen = new Set();
const duplicates = new Set();
for (const item of arr) {
if (seen.has(item)) {
duplicates.add(item);
} else {
seen.add(item);
}
}
return Array.from(duplicates);
}
function populateNodesDOM(node) {
for (let i = 0; i < node.children.length; i++) {
const component = node.children[i];
if (node.tag === "fragment") {
component.parent = node.parent;
} else {
component.parent = node;
}
node.appendChild(component, false);
if (component.children.length > 0) {
populateNodesDOM(component);
}
}
}