@zcrkey/js-utils 0.0.5 → 0.0.7
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/README.md +9 -0
- package/dist/cjs/color.d.ts +195 -0
- package/dist/cjs/color.js +353 -0
- package/{src/eventCenter.ts → dist/cjs/eventCenter.js} +120 -112
- package/dist/cjs/index.d.ts +9 -0
- package/dist/cjs/index.js +54 -0
- package/dist/cjs/obj.js +37 -0
- package/{src/storage.ts → dist/cjs/storage.js} +118 -101
- package/dist/cjs/tree.js +145 -0
- package/dist/{util.d.ts → cjs/util.d.ts} +3 -3
- package/{src/util.ts → dist/cjs/util.js} +173 -257
- package/dist/esm/color.d.ts +195 -0
- package/dist/esm/color.js +500 -0
- package/dist/esm/eventCenter.d.ts +59 -0
- package/dist/esm/index.d.ts +9 -0
- package/dist/esm/index.js +6 -0
- package/dist/esm/obj.d.ts +12 -0
- package/dist/{objUtil.js → esm/obj.js} +6 -6
- package/dist/esm/storage.d.ts +44 -0
- package/dist/esm/tree.d.ts +48 -0
- package/dist/esm/util.d.ts +209 -0
- package/dist/{util.js → esm/util.js} +129 -129
- package/dist/{index.umd.js → umd/index.umd.js} +1 -1
- package/package.json +15 -4
- package/.dumi/global.less +0 -1396
- package/.dumirc.ts +0 -36
- package/.fatherrc.ts +0 -14
- package/.husky/commit-msg +0 -4
- package/.husky/pre-commit +0 -4
- package/.prettierignore +0 -2
- package/.stylelintrc +0 -10
- package/dist/index.d.ts +0 -8
- package/dist/index.js +0 -5
- package/docs/api/eventCenter/index.md +0 -34
- package/docs/api/index.md +0 -5
- package/docs/api/storage/index.md +0 -9
- package/docs/api/storage/local.tsx +0 -91
- package/docs/api/storage/session.tsx +0 -85
- package/docs/api/treeUtil/index.md +0 -5
- package/docs/api/treeUtil/index.tsx +0 -266
- package/docs/api/util/index.md +0 -6
- package/docs/api/util/index.tsx +0 -405
- package/docs/api/util/is.tsx +0 -196
- package/docs/guide.md +0 -24
- package/src/index.ts +0 -8
- package/src/objUtil.ts +0 -20
- package/src/treeUtil.ts +0 -164
- package/tsconfig.json +0 -18
- /package/dist/{eventCenter.d.ts → cjs/eventCenter.d.ts} +0 -0
- /package/dist/{objUtil.d.ts → cjs/obj.d.ts} +0 -0
- /package/dist/{storage.d.ts → cjs/storage.d.ts} +0 -0
- /package/dist/{treeUtil.d.ts → cjs/tree.d.ts} +0 -0
- /package/dist/{eventCenter.js → esm/eventCenter.js} +0 -0
- /package/dist/{storage.js → esm/storage.js} +0 -0
- /package/dist/{treeUtil.js → esm/tree.js} +0 -0
package/src/treeUtil.ts
DELETED
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
cloneDeep as _cloneDeep,
|
|
3
|
-
find as _find,
|
|
4
|
-
get as _get,
|
|
5
|
-
keyBy as _keyBy,
|
|
6
|
-
} from 'lodash';
|
|
7
|
-
|
|
8
|
-
export default class CrTreeUtil {
|
|
9
|
-
/**
|
|
10
|
-
* 列表数据转树形数据
|
|
11
|
-
* @param list
|
|
12
|
-
* @param options
|
|
13
|
-
* @param options.idField 默认值 id
|
|
14
|
-
* @param options.parentIdField 默认值 parentId
|
|
15
|
-
* @param options.childrenField 默认值 children
|
|
16
|
-
* @param options.isCloneDeep 是否需要深拷贝, 默认值为 true
|
|
17
|
-
* @param options.getData 处理数据
|
|
18
|
-
* @returns
|
|
19
|
-
*/
|
|
20
|
-
static listToTree<T extends Record<string, any>>(
|
|
21
|
-
list: T[],
|
|
22
|
-
options?: {
|
|
23
|
-
idField?: string;
|
|
24
|
-
parentIdField?: string;
|
|
25
|
-
childrenField?: string;
|
|
26
|
-
isCloneDeep?: boolean;
|
|
27
|
-
getData?: (item: T) => any;
|
|
28
|
-
},
|
|
29
|
-
): any[] {
|
|
30
|
-
const {
|
|
31
|
-
idField = 'id',
|
|
32
|
-
parentIdField = 'parentId',
|
|
33
|
-
childrenField = 'children',
|
|
34
|
-
isCloneDeep = true,
|
|
35
|
-
getData = (item: T) => item,
|
|
36
|
-
} = options || {};
|
|
37
|
-
|
|
38
|
-
const listData = isCloneDeep ? _cloneDeep(list) : list;
|
|
39
|
-
const treeData: any[] = [];
|
|
40
|
-
const nodeMap = new Map<string | number, any>();
|
|
41
|
-
|
|
42
|
-
for (const item of listData) {
|
|
43
|
-
const id = item[idField];
|
|
44
|
-
const node = {
|
|
45
|
-
...getData(item),
|
|
46
|
-
__origin_id: id,
|
|
47
|
-
__origin_pid: item[parentIdField],
|
|
48
|
-
};
|
|
49
|
-
nodeMap.set(id, node);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
for (const node of nodeMap.values()) {
|
|
53
|
-
const parentId = node.__origin_pid;
|
|
54
|
-
if (
|
|
55
|
-
parentId === undefined ||
|
|
56
|
-
parentId === null ||
|
|
57
|
-
parentId === 0 ||
|
|
58
|
-
!nodeMap.has(parentId)
|
|
59
|
-
) {
|
|
60
|
-
treeData.push(node);
|
|
61
|
-
} else {
|
|
62
|
-
const parentNode = nodeMap.get(parentId);
|
|
63
|
-
if (!parentNode[childrenField]) {
|
|
64
|
-
parentNode[childrenField] = [];
|
|
65
|
-
}
|
|
66
|
-
parentNode[childrenField].push(node);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const clean = (nodes: any[]) => {
|
|
71
|
-
for (const node of nodes) {
|
|
72
|
-
delete node.__origin_id;
|
|
73
|
-
delete node.__origin_pid;
|
|
74
|
-
if (node[childrenField]) {
|
|
75
|
-
clean(node[childrenField]);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
clean(treeData);
|
|
80
|
-
|
|
81
|
-
return treeData;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* 树形数据转列表数据
|
|
86
|
-
* @param tree
|
|
87
|
-
* @param options
|
|
88
|
-
* @param options.childrenField 默认值 children
|
|
89
|
-
* @param options.isCloneDeep 是否需要深拷贝, 默认值为 true
|
|
90
|
-
* @returns
|
|
91
|
-
*/
|
|
92
|
-
static treeToList<T extends Record<string, any>>(
|
|
93
|
-
tree: T[],
|
|
94
|
-
options?: {
|
|
95
|
-
childrenField?: string;
|
|
96
|
-
isCloneDeep?: boolean;
|
|
97
|
-
},
|
|
98
|
-
) {
|
|
99
|
-
const { childrenField = 'children', isCloneDeep = true } = options || {};
|
|
100
|
-
const result: T[] = [];
|
|
101
|
-
const queue: T[] = [...tree];
|
|
102
|
-
while (queue.length) {
|
|
103
|
-
const node = queue.shift()!;
|
|
104
|
-
const children = node[childrenField];
|
|
105
|
-
const { [childrenField]: _, ...rest } = node;
|
|
106
|
-
result.push(rest as T);
|
|
107
|
-
if (children && children.length) {
|
|
108
|
-
queue.push(...children);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
return isCloneDeep ? _cloneDeep(result) : result;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* 获取扁平化父数据(包含自身)
|
|
117
|
-
* @param listData
|
|
118
|
-
* @param value
|
|
119
|
-
* @param settings
|
|
120
|
-
* @param settings.valueField 默认值 value
|
|
121
|
-
* @param settings.idField 默认值 id
|
|
122
|
-
* @param settings.parentIdField 默认值 parentId
|
|
123
|
-
* @param settings.getData 过滤数据
|
|
124
|
-
*/
|
|
125
|
-
static getFlatParentDatas<T = any, R extends Record<string, any> = any>(
|
|
126
|
-
listData: R[],
|
|
127
|
-
value: number | string,
|
|
128
|
-
settings?: {
|
|
129
|
-
valueField?: string;
|
|
130
|
-
idField?: string;
|
|
131
|
-
parentIdField?: string;
|
|
132
|
-
getData?: (item: R) => T;
|
|
133
|
-
},
|
|
134
|
-
): T[] {
|
|
135
|
-
if (!Array.isArray(listData) || listData.length === 0 || value == null) {
|
|
136
|
-
return [];
|
|
137
|
-
}
|
|
138
|
-
const options = Object.assign(
|
|
139
|
-
{
|
|
140
|
-
valueField: 'value',
|
|
141
|
-
idField: 'id',
|
|
142
|
-
parentIdField: 'parentId',
|
|
143
|
-
getData: (item: R): T => {
|
|
144
|
-
return item as unknown as T;
|
|
145
|
-
},
|
|
146
|
-
},
|
|
147
|
-
settings,
|
|
148
|
-
);
|
|
149
|
-
const nodeMap = _keyBy(listData, options.valueField);
|
|
150
|
-
const result: T[] = [];
|
|
151
|
-
const visited = new Set();
|
|
152
|
-
let current: R | undefined = nodeMap[value];
|
|
153
|
-
while (current && !visited.has(_get(current, options.idField))) {
|
|
154
|
-
visited.add(_get(current, options.idField));
|
|
155
|
-
result.unshift(options.getData(current));
|
|
156
|
-
const parentId: string | number | undefined | null = _get(
|
|
157
|
-
current,
|
|
158
|
-
options.parentIdField,
|
|
159
|
-
);
|
|
160
|
-
current = _find(listData, [options.valueField, parentId]);
|
|
161
|
-
}
|
|
162
|
-
return result;
|
|
163
|
-
}
|
|
164
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "es2015",
|
|
4
|
-
"downlevelIteration": true,
|
|
5
|
-
"strict": true,
|
|
6
|
-
"declaration": true,
|
|
7
|
-
"skipLibCheck": true,
|
|
8
|
-
"esModuleInterop": true,
|
|
9
|
-
"jsx": "react",
|
|
10
|
-
"baseUrl": "./",
|
|
11
|
-
"paths": {
|
|
12
|
-
"@@/*": [".dumi/tmp/*"],
|
|
13
|
-
"@zcrkey/js-utils": ["src"],
|
|
14
|
-
"@zcrkey/js-utils/*": ["src/*", "*"]
|
|
15
|
-
}
|
|
16
|
-
},
|
|
17
|
-
"include": [".dumirc.ts", "src/**/*", "docs/**/*"]
|
|
18
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|