@pawover/kit 0.0.0-alpha.9 → 0.0.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/enums.d.ts +25 -0
- package/dist/enums.d.ts.map +1 -0
- package/dist/enums.js +25 -0
- package/dist/enums.js.map +1 -0
- package/dist/hooks-alova.d.ts +23 -0
- package/dist/hooks-alova.d.ts.map +1 -0
- package/dist/hooks-alova.js +39 -0
- package/dist/hooks-alova.js.map +1 -0
- package/dist/hooks-react.d.ts +89 -0
- package/dist/hooks-react.d.ts.map +1 -0
- package/dist/hooks-react.js +340 -0
- package/dist/hooks-react.js.map +1 -0
- package/dist/index.d.ts +2445 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1430 -1
- package/dist/index.js.map +1 -0
- package/dist/patches-fetchEventSource.d.ts +806 -0
- package/dist/patches-fetchEventSource.d.ts.map +1 -0
- package/dist/patches-fetchEventSource.js +315 -0
- package/dist/patches-fetchEventSource.js.map +1 -0
- package/dist/vite.d.ts +13 -0
- package/dist/vite.d.ts.map +1 -0
- package/dist/vite.js +23 -0
- package/dist/vite.js.map +1 -0
- package/dist/zod.d.ts +105 -0
- package/dist/zod.d.ts.map +1 -0
- package/dist/zod.js +138 -0
- package/dist/zod.js.map +1 -0
- package/metadata.json +160 -0
- package/package.json +51 -35
- package/dist/enums/index.js +0 -20
- package/dist/hooks/react/index.js +0 -5
- package/dist/hooks/react/useCreation.js +0 -19
- package/dist/hooks/react/useLatest.js +0 -13
- package/dist/hooks/react/useMount.js +0 -29
- package/dist/hooks/react/useResponsive.js +0 -59
- package/dist/hooks/react/useUnmount.js +0 -18
- package/dist/types/enums/index.d.ts +0 -20
- package/dist/types/hooks/react/index.d.ts +0 -5
- package/dist/types/hooks/react/useCreation.d.ts +0 -10
- package/dist/types/hooks/react/useLatest.d.ts +0 -8
- package/dist/types/hooks/react/useMount.d.ts +0 -11
- package/dist/types/hooks/react/useResponsive.d.ts +0 -16
- package/dist/types/hooks/react/useUnmount.d.ts +0 -7
- package/dist/types/index.d.ts +0 -1
- package/dist/types/utils/array.d.ts +0 -76
- package/dist/types/utils/clone.d.ts +0 -13
- package/dist/types/utils/index.d.ts +0 -7
- package/dist/types/utils/object.d.ts +0 -54
- package/dist/types/utils/string.d.ts +0 -15
- package/dist/types/utils/to.d.ts +0 -5
- package/dist/types/utils/tree/index.d.ts +0 -6
- package/dist/types/utils/tree/rowsToTree.d.ts +0 -10
- package/dist/types/utils/tree/treeFilter.d.ts +0 -6
- package/dist/types/utils/tree/treeFind.d.ts +0 -8
- package/dist/types/utils/tree/treeForEach.d.ts +0 -5
- package/dist/types/utils/tree/treeMap.d.ts +0 -6
- package/dist/types/utils/tree/treeToRows.d.ts +0 -9
- package/dist/types/utils/tree/types.d.ts +0 -24
- package/dist/types/utils/typeof.d.ts +0 -37
- package/dist/types/zod/index.d.ts +0 -3
- package/dist/types/zod/primitive.d.ts +0 -32
- package/dist/types/zod/validator/input.d.ts +0 -27
- package/dist/types/zod/validator/societal.d.ts +0 -39
- package/dist/utils/array.js +0 -196
- package/dist/utils/clone.js +0 -75
- package/dist/utils/index.js +0 -7
- package/dist/utils/object.js +0 -149
- package/dist/utils/string.js +0 -73
- package/dist/utils/to.js +0 -16
- package/dist/utils/tree/index.js +0 -6
- package/dist/utils/tree/rowsToTree.js +0 -35
- package/dist/utils/tree/treeFilter.js +0 -92
- package/dist/utils/tree/treeFind.js +0 -82
- package/dist/utils/tree/treeForEach.js +0 -60
- package/dist/utils/tree/treeMap.js +0 -79
- package/dist/utils/tree/treeToRows.js +0 -13
- package/dist/utils/tree/types.js +0 -10
- package/dist/utils/typeof.js +0 -149
- package/dist/zod/index.js +0 -3
- package/dist/zod/primitive.js +0 -32
- package/dist/zod/validator/input.js +0 -39
- package/dist/zod/validator/societal.js +0 -57
package/dist/utils/clone.js
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { isArray, isMap, isObject, isSet } from "./typeof";
|
|
2
|
-
export const DefaultCloningStrategy = {
|
|
3
|
-
cloneMap(input, track, clone) {
|
|
4
|
-
const output = track(new Map());
|
|
5
|
-
for (const [key, value] of input) {
|
|
6
|
-
output.set(key, clone(value));
|
|
7
|
-
}
|
|
8
|
-
return output;
|
|
9
|
-
},
|
|
10
|
-
cloneSet(input, track, clone) {
|
|
11
|
-
const output = track(new Set());
|
|
12
|
-
for (const value of input) {
|
|
13
|
-
output.add(clone(value));
|
|
14
|
-
}
|
|
15
|
-
return output;
|
|
16
|
-
},
|
|
17
|
-
cloneArray(input, track, clone) {
|
|
18
|
-
// Use .forEach for correct handling of sparse arrays
|
|
19
|
-
const output = track(new Array(input.length));
|
|
20
|
-
input.forEach((value, index) => {
|
|
21
|
-
output[index] = clone(value);
|
|
22
|
-
});
|
|
23
|
-
return output;
|
|
24
|
-
},
|
|
25
|
-
cloneObject(input, track, clone) {
|
|
26
|
-
const output = track(Object.create(Object.getPrototypeOf(input)));
|
|
27
|
-
for (const key of Reflect.ownKeys(input)) {
|
|
28
|
-
// By copying the property descriptors, we preserve computed
|
|
29
|
-
// properties and non-enumerable properties.
|
|
30
|
-
const descriptor = Object.getOwnPropertyDescriptor(input, key);
|
|
31
|
-
if ("value" in descriptor) {
|
|
32
|
-
descriptor.value = clone(descriptor.value);
|
|
33
|
-
}
|
|
34
|
-
Object.defineProperty(output, key, descriptor);
|
|
35
|
-
}
|
|
36
|
-
return output;
|
|
37
|
-
},
|
|
38
|
-
cloneOther(input, track) {
|
|
39
|
-
return track(input);
|
|
40
|
-
},
|
|
41
|
-
};
|
|
42
|
-
/**
|
|
43
|
-
* cloneDeep
|
|
44
|
-
* @reference https://github.com/radashi-org/radashi/blob/main/src/object/cloneDeep.ts
|
|
45
|
-
*/
|
|
46
|
-
export function cloneDeep(root, customStrategy) {
|
|
47
|
-
const strategy = { ...DefaultCloningStrategy, ...customStrategy };
|
|
48
|
-
const tracked = new Map();
|
|
49
|
-
const track = (parent, newParent) => {
|
|
50
|
-
tracked.set(parent, newParent);
|
|
51
|
-
return newParent;
|
|
52
|
-
};
|
|
53
|
-
const clone = (value) => {
|
|
54
|
-
return value && typeof value === "object" ? (tracked.get(value) ?? cloneDeep(value, strategy)) : value;
|
|
55
|
-
};
|
|
56
|
-
const cloneDeep = (parent, strategy) => {
|
|
57
|
-
const cloneParent = (isObject(parent)
|
|
58
|
-
? strategy.cloneObject
|
|
59
|
-
: isArray(parent)
|
|
60
|
-
? strategy.cloneArray
|
|
61
|
-
: isMap(parent)
|
|
62
|
-
? strategy.cloneMap
|
|
63
|
-
: isSet(parent)
|
|
64
|
-
? strategy.cloneSet
|
|
65
|
-
: strategy.cloneOther);
|
|
66
|
-
const newParent = cloneParent(parent, track.bind(null, parent), clone);
|
|
67
|
-
if (!newParent) {
|
|
68
|
-
// Use the default strategy if null is returned.
|
|
69
|
-
return cloneDeep(parent, DefaultCloningStrategy);
|
|
70
|
-
}
|
|
71
|
-
tracked.set(parent, newParent);
|
|
72
|
-
return newParent;
|
|
73
|
-
};
|
|
74
|
-
return cloneDeep(root, strategy);
|
|
75
|
-
}
|
package/dist/utils/index.js
DELETED
package/dist/utils/object.js
DELETED
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import { isArray, isObject } from "./typeof";
|
|
2
|
-
/**
|
|
3
|
-
* 返回对象的可枚举属性和方法的名称
|
|
4
|
-
* - `Object.keys` 始终返回 `string[]` 类型,此函数可以返回具体类型
|
|
5
|
-
*
|
|
6
|
-
* @param obj 对象
|
|
7
|
-
* @returns 对象所有可枚举的属性的键名
|
|
8
|
-
*/
|
|
9
|
-
export function objectKeys(obj) {
|
|
10
|
-
return Object.keys(obj);
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* 返回对象的可枚举属性的值数组
|
|
14
|
-
*
|
|
15
|
-
* @param obj 对象
|
|
16
|
-
*/
|
|
17
|
-
export function objectValues(obj) {
|
|
18
|
-
return Object.values(obj);
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* 返回对象的可枚举属性的键/值数组
|
|
22
|
-
*
|
|
23
|
-
* @param obj 对象
|
|
24
|
-
*/
|
|
25
|
-
export function objectEntries(obj) {
|
|
26
|
-
return Object.entries(obj);
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* 对象反转
|
|
30
|
-
* - 返回交换了对象的可枚举属性的值/键对象
|
|
31
|
-
*
|
|
32
|
-
* @param obj 对象
|
|
33
|
-
*/
|
|
34
|
-
export function objectSwitch(obj) {
|
|
35
|
-
const result = {};
|
|
36
|
-
if (!isObject(obj)) {
|
|
37
|
-
return result;
|
|
38
|
-
}
|
|
39
|
-
for (const [k, v] of objectEntries(obj)) {
|
|
40
|
-
result[v] = k;
|
|
41
|
-
}
|
|
42
|
-
return result;
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* 对象合并
|
|
46
|
-
* - 将两个对象递归合并为一个新对象,从右到左依次使用数值
|
|
47
|
-
* - 递归只适用于子对象属性
|
|
48
|
-
*/
|
|
49
|
-
export function objectAssign(obj, overrideObj) {
|
|
50
|
-
const result = {};
|
|
51
|
-
if (!isObject(obj) && !isObject(overrideObj)) {
|
|
52
|
-
return result;
|
|
53
|
-
}
|
|
54
|
-
if (!isObject(obj) || !isObject(overrideObj)) {
|
|
55
|
-
return obj ?? overrideObj ?? result;
|
|
56
|
-
}
|
|
57
|
-
return objectEntries({ ...obj, ...overrideObj }).reduce((acc, [key, value]) => {
|
|
58
|
-
return {
|
|
59
|
-
...acc,
|
|
60
|
-
[key]: (() => {
|
|
61
|
-
if (isObject(obj[key])) {
|
|
62
|
-
return objectAssign(obj[key], value);
|
|
63
|
-
}
|
|
64
|
-
return value;
|
|
65
|
-
})(),
|
|
66
|
-
};
|
|
67
|
-
}, result);
|
|
68
|
-
}
|
|
69
|
-
export function objectPick(obj, keys) {
|
|
70
|
-
const result = {};
|
|
71
|
-
if (!isObject(obj)) {
|
|
72
|
-
return result;
|
|
73
|
-
}
|
|
74
|
-
if (!isArray(keys)) {
|
|
75
|
-
return obj;
|
|
76
|
-
}
|
|
77
|
-
return keys.reduce((acc, curr) => {
|
|
78
|
-
if (curr in obj) {
|
|
79
|
-
acc[curr] = obj[curr];
|
|
80
|
-
}
|
|
81
|
-
return acc;
|
|
82
|
-
}, result);
|
|
83
|
-
}
|
|
84
|
-
function enumType(enumeration) {
|
|
85
|
-
if (!isObject(enumeration)) {
|
|
86
|
-
throw Error(`function enumKeys expected parameter is a enum, but got ${typeof enumeration}`);
|
|
87
|
-
}
|
|
88
|
-
if (!objectKeys(enumeration).length) {
|
|
89
|
-
throw Error("Enum requires at least one member");
|
|
90
|
-
}
|
|
91
|
-
return enumeration;
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* 获取枚举所有属性的键
|
|
95
|
-
*
|
|
96
|
-
* @param enumeration 枚举
|
|
97
|
-
*/
|
|
98
|
-
export function enumKeys(enumeration) {
|
|
99
|
-
const e = enumType(enumeration);
|
|
100
|
-
const keys = objectKeys(e);
|
|
101
|
-
const values = objectValues(e);
|
|
102
|
-
const isTwoWayEnum = keys.every((k) => values.some((v) => `${v}` === k));
|
|
103
|
-
if (isTwoWayEnum) {
|
|
104
|
-
return keys.splice(keys.length / 2, keys.length / 2);
|
|
105
|
-
}
|
|
106
|
-
return keys;
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* 获取枚举所有属性的值
|
|
110
|
-
*
|
|
111
|
-
* @param enumeration 枚举
|
|
112
|
-
*/
|
|
113
|
-
export function enumValues(enumeration) {
|
|
114
|
-
const e = enumType(enumeration);
|
|
115
|
-
const keys = objectKeys(e);
|
|
116
|
-
const values = objectValues(e);
|
|
117
|
-
const isTwoWayEnum = keys.every((k) => values.some((v) => `${v}` === k));
|
|
118
|
-
if (isTwoWayEnum) {
|
|
119
|
-
return values.splice(keys.length / 2, keys.length / 2);
|
|
120
|
-
}
|
|
121
|
-
return values;
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* 返回枚举的属性的键/值数组
|
|
125
|
-
*
|
|
126
|
-
* @param enumeration 枚举
|
|
127
|
-
*/
|
|
128
|
-
export function enumEntries(enumeration) {
|
|
129
|
-
const e = enumType(enumeration);
|
|
130
|
-
const keys = objectKeys(e);
|
|
131
|
-
const values = objectValues(e);
|
|
132
|
-
const entries = objectEntries(e);
|
|
133
|
-
const isTwoWayEnum = keys.every((k) => values.some((v) => `${v}` === k));
|
|
134
|
-
if (isTwoWayEnum) {
|
|
135
|
-
return entries.splice(keys.length / 2, keys.length / 2);
|
|
136
|
-
}
|
|
137
|
-
return entries;
|
|
138
|
-
}
|
|
139
|
-
export function mapEntries(obj, toEntry) {
|
|
140
|
-
const defaultResult = {};
|
|
141
|
-
if (!obj) {
|
|
142
|
-
return defaultResult;
|
|
143
|
-
}
|
|
144
|
-
return objectEntries(obj).reduce((acc, [key, value]) => {
|
|
145
|
-
const [newKey, newValue] = toEntry(key, value);
|
|
146
|
-
acc[newKey] = newValue;
|
|
147
|
-
return acc;
|
|
148
|
-
}, defaultResult);
|
|
149
|
-
}
|
package/dist/utils/string.js
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { isString } from "./typeof";
|
|
2
|
-
/**
|
|
3
|
-
* 转义特殊字符
|
|
4
|
-
*
|
|
5
|
-
* @link https://github.com/sindresorhus/escape-string-regexp
|
|
6
|
-
* @param value 字符串
|
|
7
|
-
*/
|
|
8
|
-
export function escapeStringRegexp(value) {
|
|
9
|
-
return value.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&").replace(/-/g, "\\x2d");
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* 首字母大小写
|
|
13
|
-
*/
|
|
14
|
-
export function stringInitialCase(value, type) {
|
|
15
|
-
if (!isString(value) || value.length === 0) {
|
|
16
|
-
return value;
|
|
17
|
-
}
|
|
18
|
-
const m1 = /\S+/g;
|
|
19
|
-
const m2 = /[^a-zA-Z\u00C0-\u017F]/;
|
|
20
|
-
return value.replace(m1, (word) => {
|
|
21
|
-
// 单词含非字母字符(如.,'-等)→ 原样保留
|
|
22
|
-
if (m2.test(word)) {
|
|
23
|
-
return word;
|
|
24
|
-
}
|
|
25
|
-
// 纯字母且全大写 → 保留
|
|
26
|
-
if (word === word.toLocaleUpperCase()) {
|
|
27
|
-
return word;
|
|
28
|
-
}
|
|
29
|
-
// 纯字母且非全大写 → 首字母小写,其余保留
|
|
30
|
-
if (type === "lower" && word[0]) {
|
|
31
|
-
return word[0].toLocaleLowerCase() + word.slice(1);
|
|
32
|
-
}
|
|
33
|
-
// 纯字母且非全大写 → 首字母大写写,其余保留
|
|
34
|
-
if (type === "upper" && word[0]) {
|
|
35
|
-
return word[0].toLocaleUpperCase() + word.slice(1);
|
|
36
|
-
}
|
|
37
|
-
return word;
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
export function stringToJson(data, safeValue) {
|
|
41
|
-
if (isString(data) && data) {
|
|
42
|
-
try {
|
|
43
|
-
const value = JSON.parse(data);
|
|
44
|
-
return value;
|
|
45
|
-
}
|
|
46
|
-
catch (error) {
|
|
47
|
-
return safeValue;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
return safeValue;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
export function stringToValues(data, valueType = "number") {
|
|
55
|
-
if (isString(data) && data) {
|
|
56
|
-
try {
|
|
57
|
-
const values = data.split(",");
|
|
58
|
-
if (valueType === "number") {
|
|
59
|
-
return values.map((d) => Number(d));
|
|
60
|
-
}
|
|
61
|
-
return values;
|
|
62
|
-
}
|
|
63
|
-
catch (error) {
|
|
64
|
-
return [];
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
else {
|
|
68
|
-
return [];
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
export function stringReplace(input, search, replacement) {
|
|
72
|
-
return input.replace(search, replacement);
|
|
73
|
-
}
|
package/dist/utils/to.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @param promise
|
|
3
|
-
* @param errorExt - 可以传递给err对象的其他信息
|
|
4
|
-
*/
|
|
5
|
-
export function to(promise, errorExt) {
|
|
6
|
-
return promise
|
|
7
|
-
.then((data) => [null, data])
|
|
8
|
-
.catch((err) => {
|
|
9
|
-
if (errorExt) {
|
|
10
|
-
const parsedError = { ...err, ...errorExt };
|
|
11
|
-
return [parsedError, undefined];
|
|
12
|
-
}
|
|
13
|
-
const defaultError = err ? err : new Error("defaultError");
|
|
14
|
-
return [defaultError, undefined];
|
|
15
|
-
});
|
|
16
|
-
}
|
package/dist/utils/tree/index.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { isNull, isUndefined } from "../typeof";
|
|
2
|
-
/**
|
|
3
|
-
* 行结构 转 树结构
|
|
4
|
-
*/
|
|
5
|
-
export function rowsToTree(rows, options) {
|
|
6
|
-
const { parentIdKey = "parentId", rowKey = "id", childrenKey = "children" } = options || {};
|
|
7
|
-
const result = [];
|
|
8
|
-
const map = new Map();
|
|
9
|
-
for (const row of rows) {
|
|
10
|
-
const id = row[rowKey];
|
|
11
|
-
if (!map.get(id)) {
|
|
12
|
-
map.set(id, row);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
for (const row of rows) {
|
|
16
|
-
const parentId = row[parentIdKey];
|
|
17
|
-
const parent = map.get(parentId);
|
|
18
|
-
if (!parent || !parentId) {
|
|
19
|
-
result.push(row);
|
|
20
|
-
continue;
|
|
21
|
-
}
|
|
22
|
-
const siblings = parent[childrenKey];
|
|
23
|
-
if (isNull(siblings) || isUndefined(siblings)) {
|
|
24
|
-
parent[childrenKey] = [row];
|
|
25
|
-
}
|
|
26
|
-
else if (Array.isArray(siblings)) {
|
|
27
|
-
siblings.push(row);
|
|
28
|
-
}
|
|
29
|
-
else {
|
|
30
|
-
const message = `The key "${childrenKey.toString()}" in parent item is not an array.`;
|
|
31
|
-
throw new Error(message);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
return result;
|
|
35
|
-
}
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import { arrayLast } from "../array";
|
|
2
|
-
import { isArray } from "../typeof";
|
|
3
|
-
import { getFinalChildrenKey } from "./types";
|
|
4
|
-
// 前置遍历
|
|
5
|
-
function preImpl(row, callback, options) {
|
|
6
|
-
const result = callback(row, options);
|
|
7
|
-
if (!result) {
|
|
8
|
-
return undefined;
|
|
9
|
-
}
|
|
10
|
-
const finalChildrenKey = getFinalChildrenKey(row, options, options);
|
|
11
|
-
const children = row[finalChildrenKey];
|
|
12
|
-
let newChildren;
|
|
13
|
-
if (isArray(children)) {
|
|
14
|
-
const nextLevelOptions = { ...options, parents: [...options.parents, row], depth: options.depth + 1 };
|
|
15
|
-
newChildren = children.map((c) => preImpl(c, callback, nextLevelOptions)).filter((c) => !!c);
|
|
16
|
-
}
|
|
17
|
-
return { ...row, [finalChildrenKey]: newChildren };
|
|
18
|
-
}
|
|
19
|
-
// 子节点优先遍历
|
|
20
|
-
function postImpl(row, callback, options) {
|
|
21
|
-
const finalChildrenKey = getFinalChildrenKey(row, options, options);
|
|
22
|
-
const children = row[finalChildrenKey];
|
|
23
|
-
let newChildren;
|
|
24
|
-
if (isArray(children)) {
|
|
25
|
-
const nextLevelOptions = { ...options, parents: [...options.parents, row], depth: options.depth + 1 };
|
|
26
|
-
newChildren = children.map((c) => preImpl(c, callback, nextLevelOptions)).filter((c) => !!c);
|
|
27
|
-
}
|
|
28
|
-
const result = callback(row, options);
|
|
29
|
-
if (!result) {
|
|
30
|
-
return undefined;
|
|
31
|
-
}
|
|
32
|
-
return { ...row, [finalChildrenKey]: newChildren };
|
|
33
|
-
}
|
|
34
|
-
// 广度优先遍历
|
|
35
|
-
function breadthImpl(row, callback, options) {
|
|
36
|
-
const queue = [{ queueRow: row, queueOptions: options }];
|
|
37
|
-
const resultCache = new WeakMap();
|
|
38
|
-
const newNodeCache = new WeakMap();
|
|
39
|
-
const childrenKeyCache = new WeakMap();
|
|
40
|
-
let result;
|
|
41
|
-
const runQueue = () => {
|
|
42
|
-
if (queue.length === 0) {
|
|
43
|
-
return result;
|
|
44
|
-
}
|
|
45
|
-
const { queueRow, queueOptions } = queue.shift();
|
|
46
|
-
const finalChildrenKey = getFinalChildrenKey(queueRow, queueOptions, queueOptions);
|
|
47
|
-
const children = queueRow[finalChildrenKey];
|
|
48
|
-
if (isArray(children)) {
|
|
49
|
-
const nextLevelOptions = { ...queueOptions, parents: [...queueOptions.parents, queueRow], depth: queueOptions.depth + 1 };
|
|
50
|
-
const subQueueItems = children.map((queueRow) => ({ queueRow, queueOptions: nextLevelOptions }));
|
|
51
|
-
queue.push(...subQueueItems);
|
|
52
|
-
}
|
|
53
|
-
const parent = arrayLast(queueOptions.parents);
|
|
54
|
-
const isTopNode = queueOptions.depth === 0;
|
|
55
|
-
const parentResult = parent && resultCache.get(parent);
|
|
56
|
-
if (!isTopNode && !parentResult) {
|
|
57
|
-
return runQueue();
|
|
58
|
-
}
|
|
59
|
-
const callbackResult = callback(queueRow, queueOptions);
|
|
60
|
-
if (isTopNode && !callbackResult) {
|
|
61
|
-
return undefined;
|
|
62
|
-
}
|
|
63
|
-
const newNode = { ...queueRow, [finalChildrenKey]: undefined };
|
|
64
|
-
if (isTopNode) {
|
|
65
|
-
result = newNode;
|
|
66
|
-
}
|
|
67
|
-
resultCache.set(queueRow, callbackResult);
|
|
68
|
-
newNodeCache.set(queueRow, newNode);
|
|
69
|
-
childrenKeyCache.set(queueRow, finalChildrenKey);
|
|
70
|
-
if (callbackResult && parent) {
|
|
71
|
-
const parentNewNode = newNodeCache.get(parent);
|
|
72
|
-
const parentChildrenKey = childrenKeyCache.get(parent);
|
|
73
|
-
if (parentNewNode && parentChildrenKey) {
|
|
74
|
-
if (!parentNewNode[parentChildrenKey]) {
|
|
75
|
-
parentNewNode[parentChildrenKey] = [];
|
|
76
|
-
}
|
|
77
|
-
parentNewNode[parentChildrenKey].push(newNode);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
return runQueue();
|
|
81
|
-
};
|
|
82
|
-
return runQueue();
|
|
83
|
-
}
|
|
84
|
-
const strategies = { pre: preImpl, post: postImpl, breadth: breadthImpl };
|
|
85
|
-
export function treeFilter(tree, callback, options = {}) {
|
|
86
|
-
const { childrenKey = "children", strategy = "pre", getChildrenKey } = options;
|
|
87
|
-
const traversalMethod = strategies[strategy];
|
|
88
|
-
const innerOptions = { childrenKey: childrenKey, depth: 0, parents: [], getChildrenKey };
|
|
89
|
-
return isArray(tree)
|
|
90
|
-
? tree.map((row) => traversalMethod(row, callback, innerOptions)).filter((t) => !!t)
|
|
91
|
-
: traversalMethod(tree, callback, innerOptions) || [];
|
|
92
|
-
}
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import { isArray } from "../typeof";
|
|
2
|
-
import { getFinalChildrenKey } from "./types";
|
|
3
|
-
const strategies = { pre: preImpl, post: postImpl, breadth: breadthImpl };
|
|
4
|
-
// 前置深度优先遍历
|
|
5
|
-
function preImpl(row, callback, options) {
|
|
6
|
-
const callbackResult = callback(row, options);
|
|
7
|
-
if (callbackResult) {
|
|
8
|
-
return row;
|
|
9
|
-
}
|
|
10
|
-
const finalChildrenKey = getFinalChildrenKey(row, options, options);
|
|
11
|
-
const children = row[finalChildrenKey];
|
|
12
|
-
if (isArray(children)) {
|
|
13
|
-
for (const child of children) {
|
|
14
|
-
const nextLevelOptions = { ...options, parents: [...options.parents, row], depth: options.depth + 1 };
|
|
15
|
-
const result = preImpl(child, callback, nextLevelOptions);
|
|
16
|
-
if (result) {
|
|
17
|
-
return result;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
return undefined;
|
|
22
|
-
}
|
|
23
|
-
// 后置深度优先遍历
|
|
24
|
-
function postImpl(row, callback, options) {
|
|
25
|
-
const finalChildrenKey = getFinalChildrenKey(row, options, options);
|
|
26
|
-
const children = row[finalChildrenKey];
|
|
27
|
-
if (isArray(children)) {
|
|
28
|
-
for (const child of children) {
|
|
29
|
-
const nextLevelOptions = { ...options, parents: [...options.parents, row], depth: options.depth + 1 };
|
|
30
|
-
const result = postImpl(child, callback, nextLevelOptions);
|
|
31
|
-
if (result) {
|
|
32
|
-
return result;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
const callbackResult = callback(row, options);
|
|
37
|
-
if (callbackResult) {
|
|
38
|
-
return row;
|
|
39
|
-
}
|
|
40
|
-
return undefined;
|
|
41
|
-
}
|
|
42
|
-
// 广度优先遍历
|
|
43
|
-
function breadthImpl(row, callback, options) {
|
|
44
|
-
const queue = [{ queueRow: row, queueOptions: options }];
|
|
45
|
-
const runQueue = () => {
|
|
46
|
-
if (queue.length === 0) {
|
|
47
|
-
return undefined;
|
|
48
|
-
}
|
|
49
|
-
const { queueRow, queueOptions } = queue.shift();
|
|
50
|
-
const finalChildrenKey = getFinalChildrenKey(queueRow, queueOptions, queueOptions);
|
|
51
|
-
const children = queueRow[finalChildrenKey];
|
|
52
|
-
if (isArray(children)) {
|
|
53
|
-
const nextLevelOptions = { ...queueOptions, parents: [...queueOptions.parents, queueRow], depth: queueOptions.depth + 1 };
|
|
54
|
-
const subQueueItems = children.map((queueRow) => ({ queueRow, queueOptions: nextLevelOptions }));
|
|
55
|
-
queue.push(...subQueueItems);
|
|
56
|
-
}
|
|
57
|
-
const callbackResult = callback(queueRow, queueOptions);
|
|
58
|
-
if (callbackResult) {
|
|
59
|
-
return queueRow;
|
|
60
|
-
}
|
|
61
|
-
return runQueue();
|
|
62
|
-
};
|
|
63
|
-
return runQueue();
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* 查找树节点,找到第一个返回非空值的节点
|
|
67
|
-
*/
|
|
68
|
-
export function treeFind(tree, callback, options = {}) {
|
|
69
|
-
const { childrenKey = "children", strategy = "pre", getChildrenKey } = options;
|
|
70
|
-
const traversalMethod = strategies[strategy];
|
|
71
|
-
const innerOptions = { childrenKey: childrenKey, depth: 0, parents: [], getChildrenKey };
|
|
72
|
-
if (isArray(tree)) {
|
|
73
|
-
for (const row of tree) {
|
|
74
|
-
const result = traversalMethod(row, callback, innerOptions);
|
|
75
|
-
if (result) {
|
|
76
|
-
return result;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
return undefined;
|
|
80
|
-
}
|
|
81
|
-
return traversalMethod(tree, callback, innerOptions);
|
|
82
|
-
}
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { isArray } from "../typeof";
|
|
2
|
-
import { getFinalChildrenKey } from "./types";
|
|
3
|
-
const strategies = { pre: preImpl, post: postImpl, breadth: breadthImpl };
|
|
4
|
-
// 前置遍历
|
|
5
|
-
function preImpl(row, callback, options) {
|
|
6
|
-
callback(row, options);
|
|
7
|
-
const finalChildrenKey = getFinalChildrenKey(row, options, options);
|
|
8
|
-
const children = row[finalChildrenKey];
|
|
9
|
-
if (isArray(children)) {
|
|
10
|
-
const nextLevelOptions = { ...options, parents: [...options.parents, row], depth: options.depth + 1 };
|
|
11
|
-
for (const child of children) {
|
|
12
|
-
preImpl(child, callback, nextLevelOptions);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
// 后置遍历
|
|
17
|
-
function postImpl(row, callback, options) {
|
|
18
|
-
const finalChildrenKey = getFinalChildrenKey(row, options, options);
|
|
19
|
-
const children = row[finalChildrenKey];
|
|
20
|
-
if (isArray(children)) {
|
|
21
|
-
const nextLevelOptions = { ...options, parents: [...options.parents, row], depth: options.depth + 1 };
|
|
22
|
-
for (const child of children) {
|
|
23
|
-
postImpl(child, callback, nextLevelOptions);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
callback(row, options);
|
|
27
|
-
}
|
|
28
|
-
// 广度优先遍历
|
|
29
|
-
function breadthImpl(row, callback, options) {
|
|
30
|
-
const queue = [{ queueRow: row, queueOptions: options }];
|
|
31
|
-
const runQueue = () => {
|
|
32
|
-
if (queue.length === 0) {
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
const { queueRow, queueOptions } = queue.shift();
|
|
36
|
-
const finalChildrenKey = getFinalChildrenKey(queueRow, queueOptions, queueOptions);
|
|
37
|
-
const children = queueRow[finalChildrenKey];
|
|
38
|
-
if (isArray(children)) {
|
|
39
|
-
const nextLevelOptions = { ...queueOptions, parents: [...queueOptions.parents, queueRow], depth: queueOptions.depth + 1 };
|
|
40
|
-
const subQueueItems = children.map((queueRow) => ({ queueRow, queueOptions: nextLevelOptions }));
|
|
41
|
-
queue.push(...subQueueItems);
|
|
42
|
-
}
|
|
43
|
-
callback(queueRow, queueOptions);
|
|
44
|
-
runQueue();
|
|
45
|
-
};
|
|
46
|
-
runQueue();
|
|
47
|
-
}
|
|
48
|
-
export function treeForEach(tree, callback, options = {}) {
|
|
49
|
-
const { childrenKey = "children", strategy = "pre", getChildrenKey } = options;
|
|
50
|
-
const traversalMethod = strategies[strategy];
|
|
51
|
-
const innerOptions = { childrenKey: childrenKey, depth: 0, parents: [], getChildrenKey };
|
|
52
|
-
if (isArray(tree)) {
|
|
53
|
-
for (const row of tree) {
|
|
54
|
-
traversalMethod(row, callback, innerOptions);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
else {
|
|
58
|
-
traversalMethod(tree, callback, innerOptions);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import { arrayLast } from "../array";
|
|
2
|
-
import { isArray } from "../typeof";
|
|
3
|
-
import { getFinalChildrenKey } from "./types";
|
|
4
|
-
const strategies = { pre: preImpl, post: postImpl, breadth: breadthImpl };
|
|
5
|
-
// 前置遍历
|
|
6
|
-
function preImpl(row, callback, options) {
|
|
7
|
-
const finalChildrenKey = getFinalChildrenKey(row, options, options);
|
|
8
|
-
const result = callback(row, options);
|
|
9
|
-
const children = row[finalChildrenKey];
|
|
10
|
-
let newChildren;
|
|
11
|
-
if (isArray(children)) {
|
|
12
|
-
const nextLevelOptions = { ...options, parents: [...options.parents, row], depth: options.depth + 1 };
|
|
13
|
-
newChildren = children.map((c) => preImpl(c, callback, nextLevelOptions));
|
|
14
|
-
}
|
|
15
|
-
return { ...result, [finalChildrenKey]: newChildren };
|
|
16
|
-
}
|
|
17
|
-
// 子节点优先遍历
|
|
18
|
-
function postImpl(row, callback, options) {
|
|
19
|
-
const finalChildrenKey = getFinalChildrenKey(row, options, options);
|
|
20
|
-
const children = row[finalChildrenKey];
|
|
21
|
-
let newChildren;
|
|
22
|
-
if (isArray(children)) {
|
|
23
|
-
const nextLevelOptions = { ...options, parents: [...options.parents, row], depth: options.depth + 1 };
|
|
24
|
-
newChildren = children.map((c) => postImpl(c, callback, nextLevelOptions));
|
|
25
|
-
}
|
|
26
|
-
const result = callback(row, options);
|
|
27
|
-
return { ...result, [finalChildrenKey]: newChildren };
|
|
28
|
-
}
|
|
29
|
-
// 广度优先遍历
|
|
30
|
-
function breadthImpl(row, callback, options) {
|
|
31
|
-
const queue = [{ queueRow: row, queueOptions: options }];
|
|
32
|
-
const cache = new WeakMap();
|
|
33
|
-
const childrenKeyCache = new WeakMap();
|
|
34
|
-
let result;
|
|
35
|
-
const runQueue = () => {
|
|
36
|
-
if (queue.length === 0) {
|
|
37
|
-
return result;
|
|
38
|
-
}
|
|
39
|
-
const { queueRow, queueOptions } = queue.shift();
|
|
40
|
-
const finalChildrenKey = getFinalChildrenKey(queueRow, queueOptions, queueOptions);
|
|
41
|
-
const children = queueRow[finalChildrenKey];
|
|
42
|
-
if (isArray(children)) {
|
|
43
|
-
const nextLevelOptions = { ...queueOptions, parents: [...queueOptions.parents, queueRow], depth: queueOptions.depth + 1 };
|
|
44
|
-
const subQueueItems = children.map((queueRow) => ({ queueRow, queueOptions: nextLevelOptions }));
|
|
45
|
-
queue.push(...subQueueItems);
|
|
46
|
-
}
|
|
47
|
-
const res = callback(queueRow, queueOptions);
|
|
48
|
-
cache.set(queueRow, res);
|
|
49
|
-
childrenKeyCache.set(queueRow, finalChildrenKey);
|
|
50
|
-
// breadth 模式的子节点一定晚于父节点执行,所以可以在cache中找到父节点的生成物
|
|
51
|
-
const parent = arrayLast(queueOptions.parents);
|
|
52
|
-
if (parent) {
|
|
53
|
-
const newParent = cache.get(parent);
|
|
54
|
-
const parentChildrenKey = childrenKeyCache.get(parent);
|
|
55
|
-
if (newParent && parentChildrenKey) {
|
|
56
|
-
if (newParent[parentChildrenKey]) {
|
|
57
|
-
newParent[parentChildrenKey].push(res);
|
|
58
|
-
}
|
|
59
|
-
else {
|
|
60
|
-
newParent[parentChildrenKey] = [res];
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
// 这棵树的顶点
|
|
65
|
-
if (queueOptions.depth === 0) {
|
|
66
|
-
result = res;
|
|
67
|
-
}
|
|
68
|
-
return runQueue();
|
|
69
|
-
};
|
|
70
|
-
return runQueue();
|
|
71
|
-
}
|
|
72
|
-
export function treeMap(tree, callback, options = {}) {
|
|
73
|
-
const { childrenKey = "children", strategy = "pre", getChildrenKey } = options;
|
|
74
|
-
const traversalMethod = strategies[strategy];
|
|
75
|
-
const innerOptions = { childrenKey: childrenKey, depth: 0, parents: [], getChildrenKey };
|
|
76
|
-
return isArray(tree)
|
|
77
|
-
? tree.map((row) => traversalMethod(row, callback, innerOptions))
|
|
78
|
-
: traversalMethod(tree, callback, innerOptions);
|
|
79
|
-
}
|