sculp-js 1.1.0 → 1.2.0
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 +0 -1
- package/lib/cjs/array.js +1 -178
- package/lib/cjs/async.js +1 -1
- package/lib/cjs/clipboard.js +1 -1
- package/lib/cjs/cookie.js +1 -1
- package/lib/cjs/date.js +1 -1
- package/lib/cjs/dom.js +1 -1
- package/lib/cjs/download.js +1 -1
- package/lib/cjs/easing.js +1 -1
- package/lib/cjs/file.js +1 -1
- package/lib/cjs/func.js +1 -1
- package/lib/cjs/index.js +6 -4
- package/lib/cjs/number.js +1 -1
- package/lib/cjs/object.js +1 -1
- package/lib/cjs/path.js +1 -1
- package/lib/cjs/qs.js +1 -1
- package/lib/cjs/random.js +1 -1
- package/lib/cjs/string.js +1 -1
- package/lib/cjs/tooltip.js +1 -1
- package/lib/cjs/tree.js +212 -0
- package/lib/cjs/type.js +1 -1
- package/lib/cjs/unique.js +1 -1
- package/lib/cjs/url.js +1 -1
- package/lib/cjs/watermark.js +1 -1
- package/lib/es/array.js +2 -176
- package/lib/es/async.js +1 -1
- package/lib/es/clipboard.js +1 -1
- package/lib/es/cookie.js +1 -1
- package/lib/es/date.js +1 -1
- package/lib/es/dom.js +1 -1
- package/lib/es/download.js +1 -1
- package/lib/es/easing.js +1 -1
- package/lib/es/file.js +1 -1
- package/lib/es/func.js +1 -1
- package/lib/es/index.js +3 -2
- package/lib/es/number.js +1 -1
- package/lib/es/object.js +1 -1
- package/lib/es/path.js +1 -1
- package/lib/es/qs.js +1 -1
- package/lib/es/random.js +1 -1
- package/lib/es/string.js +1 -1
- package/lib/es/tooltip.js +1 -1
- package/lib/es/tree.js +207 -0
- package/lib/es/type.js +1 -1
- package/lib/es/unique.js +1 -1
- package/lib/es/url.js +1 -1
- package/lib/es/watermark.js +1 -1
- package/lib/index.d.ts +77 -64
- package/lib/umd/index.js +202 -175
- package/package.json +4 -1
package/lib/es/array.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.0
|
|
2
|
+
* sculp-js v1.1.0
|
|
3
3
|
* (c) 2023-2023 chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -107,179 +107,5 @@ function arrayRemove(array, expect) {
|
|
|
107
107
|
indexes.forEach((val, idx) => array.splice(val - idx, 1));
|
|
108
108
|
return array;
|
|
109
109
|
}
|
|
110
|
-
/**
|
|
111
|
-
* 自定义深度优先遍历函数(支持continue和break操作)
|
|
112
|
-
* @param {ArrayLike<V>} tree 树形数据
|
|
113
|
-
* @param {Function} iterator 迭代函数, 返回值为true时continue, 返回值为false时break
|
|
114
|
-
* @param {string} children 定制子元素的key
|
|
115
|
-
* @param {boolean} isReverse 是否反向遍历
|
|
116
|
-
* @returns {*}
|
|
117
|
-
*/
|
|
118
|
-
function forEachDeep(tree, iterator, children = 'children', isReverse = false) {
|
|
119
|
-
let level = 0, isBreak = false;
|
|
120
|
-
const walk = (arr, parent) => {
|
|
121
|
-
if (isReverse) {
|
|
122
|
-
for (let i = arr.length - 1; i >= 0; i--) {
|
|
123
|
-
if (isBreak) {
|
|
124
|
-
break;
|
|
125
|
-
}
|
|
126
|
-
const re = iterator(arr[i], i, tree, parent, level);
|
|
127
|
-
if (re === false) {
|
|
128
|
-
isBreak = true;
|
|
129
|
-
break;
|
|
130
|
-
}
|
|
131
|
-
else if (re === true) {
|
|
132
|
-
continue;
|
|
133
|
-
}
|
|
134
|
-
// @ts-ignore
|
|
135
|
-
if (Array.isArray(arr[i][children])) {
|
|
136
|
-
++level;
|
|
137
|
-
// @ts-ignore
|
|
138
|
-
walk(arr[i][children], arr[i]);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
else {
|
|
143
|
-
for (let i = 0; i < arr.length; i++) {
|
|
144
|
-
if (isBreak) {
|
|
145
|
-
break;
|
|
146
|
-
}
|
|
147
|
-
const re = iterator(arr[i], i, tree, parent, level);
|
|
148
|
-
if (re === false) {
|
|
149
|
-
isBreak = true;
|
|
150
|
-
break;
|
|
151
|
-
}
|
|
152
|
-
else if (re === true) {
|
|
153
|
-
continue;
|
|
154
|
-
}
|
|
155
|
-
// @ts-ignore
|
|
156
|
-
if (Array.isArray(arr[i][children])) {
|
|
157
|
-
++level;
|
|
158
|
-
// @ts-ignore
|
|
159
|
-
walk(arr[i][children], arr[i]);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
};
|
|
164
|
-
walk(tree, null);
|
|
165
|
-
}
|
|
166
|
-
/**
|
|
167
|
-
* 在树中找到 id 为某个值的节点,并返回上游的所有父级节点
|
|
168
|
-
*
|
|
169
|
-
* @param {ArrayLike<T>} tree - 树形数据
|
|
170
|
-
* @param {IdLike} nodeId - 元素ID
|
|
171
|
-
* @param {ITreeConf} config - 迭代配置项
|
|
172
|
-
* @returns {[IdLike[], ITreeItem<V>[]]} - 由parentId...childId, parentObject-childObject组成的二维数组
|
|
173
|
-
*/
|
|
174
|
-
function searchTreeById(tree, nodeId, config) {
|
|
175
|
-
const { children = 'children', id = 'id' } = config || {};
|
|
176
|
-
const toFlatArray = (tree, parentId, parent) => {
|
|
177
|
-
return tree.reduce((t, _) => {
|
|
178
|
-
const child = _[children];
|
|
179
|
-
return [
|
|
180
|
-
...t,
|
|
181
|
-
parentId ? { ..._, parentId, parent } : _,
|
|
182
|
-
...(child && child.length ? toFlatArray(child, _[id], _) : [])
|
|
183
|
-
];
|
|
184
|
-
}, []);
|
|
185
|
-
};
|
|
186
|
-
const getIds = (flatArray) => {
|
|
187
|
-
let child = flatArray.find(_ => _[id] === nodeId);
|
|
188
|
-
const { parent, parentId, ...other } = child;
|
|
189
|
-
let ids = [nodeId], nodes = [other];
|
|
190
|
-
while (child && child.parentId) {
|
|
191
|
-
ids = [child.parentId, ...ids];
|
|
192
|
-
nodes = [child.parent, ...nodes];
|
|
193
|
-
child = flatArray.find(_ => _[id] === child.parentId); // eslint-disable-line
|
|
194
|
-
}
|
|
195
|
-
return [ids, nodes];
|
|
196
|
-
};
|
|
197
|
-
return getIds(toFlatArray(tree));
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* 使用迭代函数转换数组
|
|
201
|
-
* @param {T} array
|
|
202
|
-
* @param {Function} callback 迭代函数
|
|
203
|
-
* @return {Array}
|
|
204
|
-
*/
|
|
205
|
-
function flatMap(array, callback) {
|
|
206
|
-
const result = [];
|
|
207
|
-
array.forEach((value, index) => {
|
|
208
|
-
result.push(...callback(value, index, array));
|
|
209
|
-
});
|
|
210
|
-
return result;
|
|
211
|
-
}
|
|
212
|
-
/**
|
|
213
|
-
* 根据 idProp 与 parentIdProp 从对象数组中构建对应的树
|
|
214
|
-
* 当 A[parentIdProp] === B[idProp] 时,对象A会被移动到对象B的children。
|
|
215
|
-
* 当一个对象的 parentIdProp 不与其他对象的 idProp 字段相等时,该对象被作为树的顶层节点
|
|
216
|
-
* @param {string} idProp 元素ID
|
|
217
|
-
* @param {string} parentIdProp 父元素ID
|
|
218
|
-
* @param {object[]} items 一维数组
|
|
219
|
-
* @returns {WithChildren<T>[]} 树
|
|
220
|
-
* @example
|
|
221
|
-
* const array = [
|
|
222
|
-
* { id: 'node-1', parent: 'root' },
|
|
223
|
-
* { id: 'node-2', parent: 'root' },
|
|
224
|
-
* { id: 'node-3', parent: 'node-2' },
|
|
225
|
-
* { id: 'node-4', parent: 'node-2' },
|
|
226
|
-
* { id: 'node-5', parent: 'node-4' },
|
|
227
|
-
* ]
|
|
228
|
-
* const tree = buildTree('id', 'parent', array)
|
|
229
|
-
* expect(tree).toEqual([
|
|
230
|
-
* { id: 'node-1', parent: 'root' },
|
|
231
|
-
* {
|
|
232
|
-
* id: 'node-2',
|
|
233
|
-
* parent: 'root',
|
|
234
|
-
* children: [
|
|
235
|
-
* { id: 'node-3', parent: 'node-2' },
|
|
236
|
-
* {
|
|
237
|
-
* id: 'node-4',
|
|
238
|
-
* parent: 'node-2',
|
|
239
|
-
* children: [{ id: 'node-5', parent: 'node-4' }],
|
|
240
|
-
* },
|
|
241
|
-
* ],
|
|
242
|
-
* },
|
|
243
|
-
* ])
|
|
244
|
-
*/
|
|
245
|
-
function buildTree(idProp, parentIdProp, items) {
|
|
246
|
-
const wrapperMap = new Map();
|
|
247
|
-
const ensure = (id) => {
|
|
248
|
-
if (wrapperMap.has(id)) {
|
|
249
|
-
return wrapperMap.get(id);
|
|
250
|
-
}
|
|
251
|
-
//@ts-ignore
|
|
252
|
-
const wrapper = { id, parent: null, item: null, children: [] };
|
|
253
|
-
wrapperMap.set(id, wrapper);
|
|
254
|
-
return wrapper;
|
|
255
|
-
};
|
|
256
|
-
for (const item of items) {
|
|
257
|
-
const parentWrapper = ensure(item[parentIdProp]);
|
|
258
|
-
const itemWrapper = ensure(item[idProp]);
|
|
259
|
-
//@ts-ignore
|
|
260
|
-
itemWrapper.parent = parentWrapper;
|
|
261
|
-
//@ts-ignore
|
|
262
|
-
parentWrapper.children.push(itemWrapper);
|
|
263
|
-
//@ts-ignore
|
|
264
|
-
itemWrapper.item = item;
|
|
265
|
-
}
|
|
266
|
-
const topLevelWrappers = flatMap(Array.from(wrapperMap.values()).filter(wrapper => wrapper.parent === null), wrapper => wrapper.children);
|
|
267
|
-
return unwrapRecursively(topLevelWrappers);
|
|
268
|
-
function unwrapRecursively(wrapperArray) {
|
|
269
|
-
const result = [];
|
|
270
|
-
for (const wrapper of wrapperArray) {
|
|
271
|
-
if (wrapper.children.length === 0) {
|
|
272
|
-
result.push(wrapper.item);
|
|
273
|
-
}
|
|
274
|
-
else {
|
|
275
|
-
result.push({
|
|
276
|
-
...wrapper.item,
|
|
277
|
-
children: unwrapRecursively(wrapper.children)
|
|
278
|
-
});
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
return result;
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
110
|
|
|
285
|
-
export { arrayEach, arrayEachAsync, arrayInsertBefore, arrayLike, arrayRemove
|
|
111
|
+
export { arrayEach, arrayEachAsync, arrayInsertBefore, arrayLike, arrayRemove };
|
package/lib/es/async.js
CHANGED
package/lib/es/clipboard.js
CHANGED
package/lib/es/cookie.js
CHANGED
package/lib/es/date.js
CHANGED
package/lib/es/dom.js
CHANGED
package/lib/es/download.js
CHANGED
package/lib/es/easing.js
CHANGED
package/lib/es/file.js
CHANGED
package/lib/es/func.js
CHANGED
package/lib/es/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.0
|
|
2
|
+
* sculp-js v1.1.0
|
|
3
3
|
* (c) 2023-2023 chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
export { arrayEach, arrayEachAsync, arrayInsertBefore, arrayLike, arrayRemove
|
|
7
|
+
export { arrayEach, arrayEachAsync, arrayInsertBefore, arrayLike, arrayRemove } from './array.js';
|
|
8
8
|
export { copyText } from './clipboard.js';
|
|
9
9
|
export { cookieDel, cookieGet, cookieSet } from './cookie.js';
|
|
10
10
|
export { calculateDate, calculateDateTime, dateParse, dateToEnd, dateToStart, formatDate, isValidDate } from './date.js';
|
|
@@ -24,3 +24,4 @@ export { STRING_POOL, randomNumber, randomString, randomUuid } from './random.js
|
|
|
24
24
|
export { HEX_POOL, formatNumber, numberAbbr, numberToHex } from './number.js';
|
|
25
25
|
export { UNIQUE_NUMBER_SAFE_LENGTH, uniqueNumber, uniqueString } from './unique.js';
|
|
26
26
|
export { tooltipEvent } from './tooltip.js';
|
|
27
|
+
export { buildTree, forEachDeep, formatTree, searchTreeById } from './tree.js';
|
package/lib/es/number.js
CHANGED
package/lib/es/object.js
CHANGED
package/lib/es/path.js
CHANGED
package/lib/es/qs.js
CHANGED
package/lib/es/random.js
CHANGED
package/lib/es/string.js
CHANGED
package/lib/es/tooltip.js
CHANGED
package/lib/es/tree.js
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* sculp-js v1.1.0
|
|
3
|
+
* (c) 2023-2023 chandq
|
|
4
|
+
* Released under the MIT License.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const defaultFieldOptions = { keyField: 'key', childField: 'children', pidField: 'pid' };
|
|
8
|
+
/**
|
|
9
|
+
* 自定义深度优先遍历函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
|
|
10
|
+
* @param {ArrayLike<V>} tree 树形数据
|
|
11
|
+
* @param {Function} iterator 迭代函数, 返回值为true时continue, 返回值为false时break
|
|
12
|
+
* @param {string} children 定制子元素的key
|
|
13
|
+
* @param {boolean} isReverse 是否反向遍历
|
|
14
|
+
* @returns {*}
|
|
15
|
+
*/
|
|
16
|
+
function forEachDeep(tree, iterator, children = 'children', isReverse = false) {
|
|
17
|
+
let level = 0, isBreak = false;
|
|
18
|
+
const walk = (arr, parent) => {
|
|
19
|
+
if (isReverse) {
|
|
20
|
+
for (let i = arr.length - 1; i >= 0; i--) {
|
|
21
|
+
if (isBreak) {
|
|
22
|
+
break;
|
|
23
|
+
}
|
|
24
|
+
const re = iterator(arr[i], i, arr, tree, parent, level);
|
|
25
|
+
if (re === false) {
|
|
26
|
+
isBreak = true;
|
|
27
|
+
break;
|
|
28
|
+
}
|
|
29
|
+
else if (re === true) {
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
// @ts-ignore
|
|
33
|
+
if (arr[i] && Array.isArray(arr[i][children])) {
|
|
34
|
+
++level;
|
|
35
|
+
// @ts-ignore
|
|
36
|
+
walk(arr[i][children], arr[i]);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
for (let i = 0; i < arr.length; i++) {
|
|
42
|
+
if (isBreak) {
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
const re = iterator(arr[i], i, arr, tree, parent, level);
|
|
46
|
+
if (re === false) {
|
|
47
|
+
isBreak = true;
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
else if (re === true) {
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
// @ts-ignore
|
|
54
|
+
if (arr[i] && Array.isArray(arr[i][children])) {
|
|
55
|
+
++level;
|
|
56
|
+
// @ts-ignore
|
|
57
|
+
walk(arr[i][children], arr[i]);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
walk(tree, null);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* 在树中找到 id 为某个值的节点,并返回上游的所有父级节点
|
|
66
|
+
*
|
|
67
|
+
* @param {ArrayLike<T>} tree - 树形数据
|
|
68
|
+
* @param {IdLike} nodeId - 元素ID
|
|
69
|
+
* @param {ITreeConf} config - 迭代配置项
|
|
70
|
+
* @returns {[IdLike[], ITreeItem<V>[]]} - 由parentId...childId, parentObject-childObject组成的二维数组
|
|
71
|
+
*/
|
|
72
|
+
function searchTreeById(tree, nodeId, config) {
|
|
73
|
+
const { children = 'children', id = 'id' } = config || {};
|
|
74
|
+
const toFlatArray = (tree, parentId, parent) => {
|
|
75
|
+
return tree.reduce((t, _) => {
|
|
76
|
+
const child = _[children];
|
|
77
|
+
return [
|
|
78
|
+
...t,
|
|
79
|
+
parentId ? { ..._, parentId, parent } : _,
|
|
80
|
+
...(child && child.length ? toFlatArray(child, _[id], _) : [])
|
|
81
|
+
];
|
|
82
|
+
}, []);
|
|
83
|
+
};
|
|
84
|
+
const getIds = (flatArray) => {
|
|
85
|
+
let child = flatArray.find(_ => _[id] === nodeId);
|
|
86
|
+
const { parent, parentId, ...other } = child;
|
|
87
|
+
let ids = [nodeId], nodes = [other];
|
|
88
|
+
while (child && child.parentId) {
|
|
89
|
+
ids = [child.parentId, ...ids];
|
|
90
|
+
nodes = [child.parent, ...nodes];
|
|
91
|
+
child = flatArray.find(_ => _[id] === child.parentId); // eslint-disable-line
|
|
92
|
+
}
|
|
93
|
+
return [ids, nodes];
|
|
94
|
+
};
|
|
95
|
+
return getIds(toFlatArray(tree));
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* 使用迭代函数转换数组
|
|
99
|
+
* @param {T} array
|
|
100
|
+
* @param {Function} callback 迭代函数
|
|
101
|
+
* @returns {Array}
|
|
102
|
+
*/
|
|
103
|
+
function flatMap(array, callback) {
|
|
104
|
+
const result = [];
|
|
105
|
+
array.forEach((value, index) => {
|
|
106
|
+
result.push(...callback(value, index, array));
|
|
107
|
+
});
|
|
108
|
+
return result;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* 根据 idProp 与 parentIdProp 从对象数组中构建对应的树
|
|
112
|
+
* 当 A[parentIdProp] === B[idProp] 时,对象A会被移动到对象B的children。
|
|
113
|
+
* 当一个对象的 parentIdProp 不与其他对象的 idProp 字段相等时,该对象被作为树的顶层节点
|
|
114
|
+
* @param {string} idProp 元素ID
|
|
115
|
+
* @param {string} parentIdProp 父元素ID
|
|
116
|
+
* @param {object[]} items 一维数组
|
|
117
|
+
* @returns {WithChildren<T>[]} 树
|
|
118
|
+
* @example
|
|
119
|
+
* const array = [
|
|
120
|
+
* { id: 'node-1', parent: 'root' },
|
|
121
|
+
* { id: 'node-2', parent: 'root' },
|
|
122
|
+
* { id: 'node-3', parent: 'node-2' },
|
|
123
|
+
* { id: 'node-4', parent: 'node-2' },
|
|
124
|
+
* { id: 'node-5', parent: 'node-4' },
|
|
125
|
+
* ]
|
|
126
|
+
* const tree = buildTree('id', 'parent', array)
|
|
127
|
+
* expect(tree).toEqual([
|
|
128
|
+
* { id: 'node-1', parent: 'root' },
|
|
129
|
+
* {
|
|
130
|
+
* id: 'node-2',
|
|
131
|
+
* parent: 'root',
|
|
132
|
+
* children: [
|
|
133
|
+
* { id: 'node-3', parent: 'node-2' },
|
|
134
|
+
* {
|
|
135
|
+
* id: 'node-4',
|
|
136
|
+
* parent: 'node-2',
|
|
137
|
+
* children: [{ id: 'node-5', parent: 'node-4' }],
|
|
138
|
+
* },
|
|
139
|
+
* ],
|
|
140
|
+
* },
|
|
141
|
+
* ])
|
|
142
|
+
*/
|
|
143
|
+
function buildTree(idProp, parentIdProp, items) {
|
|
144
|
+
const wrapperMap = new Map();
|
|
145
|
+
const ensure = (id) => {
|
|
146
|
+
if (wrapperMap.has(id)) {
|
|
147
|
+
return wrapperMap.get(id);
|
|
148
|
+
}
|
|
149
|
+
//@ts-ignore
|
|
150
|
+
const wrapper = { id, parent: null, item: null, children: [] };
|
|
151
|
+
wrapperMap.set(id, wrapper);
|
|
152
|
+
return wrapper;
|
|
153
|
+
};
|
|
154
|
+
for (const item of items) {
|
|
155
|
+
const parentWrapper = ensure(item[parentIdProp]);
|
|
156
|
+
const itemWrapper = ensure(item[idProp]);
|
|
157
|
+
//@ts-ignore
|
|
158
|
+
itemWrapper.parent = parentWrapper;
|
|
159
|
+
//@ts-ignore
|
|
160
|
+
parentWrapper.children.push(itemWrapper);
|
|
161
|
+
//@ts-ignore
|
|
162
|
+
itemWrapper.item = item;
|
|
163
|
+
}
|
|
164
|
+
const topLevelWrappers = flatMap(Array.from(wrapperMap.values()).filter(wrapper => wrapper.parent === null), wrapper => wrapper.children);
|
|
165
|
+
return unwrapRecursively(topLevelWrappers);
|
|
166
|
+
function unwrapRecursively(wrapperArray) {
|
|
167
|
+
const result = [];
|
|
168
|
+
for (const wrapper of wrapperArray) {
|
|
169
|
+
if (wrapper.children.length === 0) {
|
|
170
|
+
result.push(wrapper.item);
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
result.push({
|
|
174
|
+
...wrapper.item,
|
|
175
|
+
children: unwrapRecursively(wrapper.children)
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return result;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* 扁平化数组转换成树(效率高于buildTree)
|
|
184
|
+
* @param {any[]} list
|
|
185
|
+
* @param {IFieldOptions} options
|
|
186
|
+
* @returns {any[]}
|
|
187
|
+
*/
|
|
188
|
+
function formatTree(list, options = defaultFieldOptions) {
|
|
189
|
+
const { keyField, childField, pidField } = options;
|
|
190
|
+
const treeArr = [];
|
|
191
|
+
const sourceMap = {};
|
|
192
|
+
list.forEach(item => {
|
|
193
|
+
sourceMap[item[keyField]] = item;
|
|
194
|
+
});
|
|
195
|
+
list.forEach(item => {
|
|
196
|
+
const parent = sourceMap[item[pidField]];
|
|
197
|
+
if (parent) {
|
|
198
|
+
(parent[childField] || (parent[childField] = [])).push(item);
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
treeArr.push(item);
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
return treeArr;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export { buildTree, forEachDeep, formatTree, searchTreeById };
|
package/lib/es/type.js
CHANGED
package/lib/es/unique.js
CHANGED
package/lib/es/url.js
CHANGED
package/lib/es/watermark.js
CHANGED