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