sculp-js 1.0.1 → 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 +105 -9
- package/lib/cjs/array.js +1 -92
- 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 -3
- 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 -91
- 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 -25
- package/lib/umd/index.js +203 -90
- package/package.json +10 -6
package/lib/es/array.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.
|
|
2
|
+
* sculp-js v1.1.0
|
|
3
3
|
* (c) 2023-2023 chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -107,94 +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
110
|
|
|
200
|
-
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.
|
|
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
package/lib/index.d.ts
CHANGED
|
@@ -74,29 +74,6 @@ declare function arrayInsertBefore(array: AnyArray, start: number, to: number):
|
|
|
74
74
|
* @returns {V[]}
|
|
75
75
|
*/
|
|
76
76
|
declare function arrayRemove<V>(array: V[], expect: (val: V, idx: number) => boolean): V[];
|
|
77
|
-
/**
|
|
78
|
-
* 自定义深度优先遍历函数(支持continue和break操作)
|
|
79
|
-
* @param {ArrayLike<V>} tree 树形数据
|
|
80
|
-
* @param {Function} iterator 迭代函数, 返回值为true时continue, 返回值为false时break
|
|
81
|
-
* @param {string} children 定制子元素的key
|
|
82
|
-
* @param {boolean} isReverse 是否反向遍历
|
|
83
|
-
* @returns {*}
|
|
84
|
-
*/
|
|
85
|
-
declare function forEachDeep<V>(tree: ArrayLike<V>, iterator: (val: V, i: number, arr: ArrayLike<V>, parent: V | null, level: number) => boolean | void, children?: string, isReverse?: boolean): void;
|
|
86
|
-
type IdLike = number | string;
|
|
87
|
-
interface ITreeConf {
|
|
88
|
-
id: string | number;
|
|
89
|
-
children: string;
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* 在树中找到 id 为某个值的节点,并返回上游的所有父级节点
|
|
93
|
-
*
|
|
94
|
-
* @param {ArrayLike<T>} tree - 树形数据
|
|
95
|
-
* @param {IdLike} nodeId - 元素ID
|
|
96
|
-
* @param {ITreeConf} config - 迭代配置项
|
|
97
|
-
* @returns {[IdLike[], ITreeItem<V>[]]} - 由parentId...childId, parentObject-childObject组成的二维数组
|
|
98
|
-
*/
|
|
99
|
-
declare function searchTreeById<V>(tree: ArrayLike<V>, nodeId: IdLike, config?: ITreeConf): [IdLike[], ArrayLike<V>[]];
|
|
100
77
|
|
|
101
78
|
/**
|
|
102
79
|
* 复制文本
|
|
@@ -383,7 +360,7 @@ declare function objectGet(obj: AnyObject, path: string, strict?: boolean): {
|
|
|
383
360
|
* @param {WeakMap} map
|
|
384
361
|
* @returns {AnyObject | AnyArray}
|
|
385
362
|
*/
|
|
386
|
-
declare function cloneDeep(obj: Object, map?: WeakMap<object, any>):
|
|
363
|
+
declare function cloneDeep(obj: Object, map?: WeakMap<object, any>): AnyObject | AnyArray;
|
|
387
364
|
|
|
388
365
|
/**
|
|
389
366
|
* 标准化路径
|
|
@@ -706,4 +683,79 @@ declare const tooltipEvent: {
|
|
|
706
683
|
handleMouseLeave: typeof handleMouseLeave;
|
|
707
684
|
};
|
|
708
685
|
|
|
709
|
-
|
|
686
|
+
interface IFieldOptions {
|
|
687
|
+
keyField: string;
|
|
688
|
+
childField: string;
|
|
689
|
+
pidField: string;
|
|
690
|
+
}
|
|
691
|
+
/**
|
|
692
|
+
* 自定义深度优先遍历函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
|
|
693
|
+
* @param {ArrayLike<V>} tree 树形数据
|
|
694
|
+
* @param {Function} iterator 迭代函数, 返回值为true时continue, 返回值为false时break
|
|
695
|
+
* @param {string} children 定制子元素的key
|
|
696
|
+
* @param {boolean} isReverse 是否反向遍历
|
|
697
|
+
* @returns {*}
|
|
698
|
+
*/
|
|
699
|
+
declare function forEachDeep<V>(tree: ArrayLike<V>, iterator: (val: V, i: number, currentArr: ArrayLike<V>, tree: ArrayLike<V>, parent: V | null, level: number) => boolean | void, children?: string, isReverse?: boolean): void;
|
|
700
|
+
type IdLike = number | string;
|
|
701
|
+
interface ITreeConf {
|
|
702
|
+
id: string | number;
|
|
703
|
+
children: string;
|
|
704
|
+
}
|
|
705
|
+
/**
|
|
706
|
+
* 在树中找到 id 为某个值的节点,并返回上游的所有父级节点
|
|
707
|
+
*
|
|
708
|
+
* @param {ArrayLike<T>} tree - 树形数据
|
|
709
|
+
* @param {IdLike} nodeId - 元素ID
|
|
710
|
+
* @param {ITreeConf} config - 迭代配置项
|
|
711
|
+
* @returns {[IdLike[], ITreeItem<V>[]]} - 由parentId...childId, parentObject-childObject组成的二维数组
|
|
712
|
+
*/
|
|
713
|
+
declare function searchTreeById<V>(tree: ArrayLike<V>, nodeId: IdLike, config?: ITreeConf): [IdLike[], ArrayLike<V>[]];
|
|
714
|
+
type WithChildren<T> = T & {
|
|
715
|
+
children?: WithChildren<T>[];
|
|
716
|
+
};
|
|
717
|
+
/**
|
|
718
|
+
* 根据 idProp 与 parentIdProp 从对象数组中构建对应的树
|
|
719
|
+
* 当 A[parentIdProp] === B[idProp] 时,对象A会被移动到对象B的children。
|
|
720
|
+
* 当一个对象的 parentIdProp 不与其他对象的 idProp 字段相等时,该对象被作为树的顶层节点
|
|
721
|
+
* @param {string} idProp 元素ID
|
|
722
|
+
* @param {string} parentIdProp 父元素ID
|
|
723
|
+
* @param {object[]} items 一维数组
|
|
724
|
+
* @returns {WithChildren<T>[]} 树
|
|
725
|
+
* @example
|
|
726
|
+
* const array = [
|
|
727
|
+
* { id: 'node-1', parent: 'root' },
|
|
728
|
+
* { id: 'node-2', parent: 'root' },
|
|
729
|
+
* { id: 'node-3', parent: 'node-2' },
|
|
730
|
+
* { id: 'node-4', parent: 'node-2' },
|
|
731
|
+
* { id: 'node-5', parent: 'node-4' },
|
|
732
|
+
* ]
|
|
733
|
+
* const tree = buildTree('id', 'parent', array)
|
|
734
|
+
* expect(tree).toEqual([
|
|
735
|
+
* { id: 'node-1', parent: 'root' },
|
|
736
|
+
* {
|
|
737
|
+
* id: 'node-2',
|
|
738
|
+
* parent: 'root',
|
|
739
|
+
* children: [
|
|
740
|
+
* { id: 'node-3', parent: 'node-2' },
|
|
741
|
+
* {
|
|
742
|
+
* id: 'node-4',
|
|
743
|
+
* parent: 'node-2',
|
|
744
|
+
* children: [{ id: 'node-5', parent: 'node-4' }],
|
|
745
|
+
* },
|
|
746
|
+
* ],
|
|
747
|
+
* },
|
|
748
|
+
* ])
|
|
749
|
+
*/
|
|
750
|
+
declare function buildTree<ID extends string, PID extends string, T extends {
|
|
751
|
+
[key in ID | PID]: string;
|
|
752
|
+
}>(idProp: ID, parentIdProp: PID, items: T[]): WithChildren<T>[];
|
|
753
|
+
/**
|
|
754
|
+
* 扁平化数组转换成树(效率高于buildTree)
|
|
755
|
+
* @param {any[]} list
|
|
756
|
+
* @param {IFieldOptions} options
|
|
757
|
+
* @returns {any[]}
|
|
758
|
+
*/
|
|
759
|
+
declare function formatTree(list: any[], options?: IFieldOptions): any[];
|
|
760
|
+
|
|
761
|
+
export { type AnyArray, type AnyFunc, type AnyObject, type ArrayElements, type DateObj, type DateValue, type DebounceFunc, type FileType, HEX_POOL, type ICanvasWM, type IFieldOptions, type ITreeConf, type IdLike, type LooseParamValue, type LooseParams, type ObjectAssignItem, type OnceFunc, type Params, type PartialDeep, type RandomString, type ReadyCallback, type Replacer, STRING_ARABIC_NUMERALS, STRING_LOWERCASE_ALPHA, STRING_POOL, STRING_UPPERCASE_ALPHA, type SetStyle, type SmoothScrollOptions, type Style, type ThrottleFunc, UNIQUE_NUMBER_SAFE_LENGTH, type UniqueString, type Url, type WithChildren, addClass, arrayEach, arrayEachAsync, arrayInsertBefore, arrayLike, arrayRemove, asyncMap, buildTree, calculateDate, calculateDateTime, chooseLocalFile, cloneDeep, cookieDel, cookieGet, cookieSet, copyText, dateParse, dateToEnd, dateToStart, debounce, downloadBlob, downloadData, downloadHref, downloadURL, forEachDeep, formatDate, formatNumber, formatTree, genCanvasWM, getComputedCssVal, getGlobal, getStrWidthPx, getStyle, hasClass, isArray, isBigInt, isBoolean, isDate, isDomReady, isError, isFunction, isNaN, isNull, isNumber, isObject, isPlainObject, isPrimitive, isRegExp, isString, isSymbol, isUndefined, isValidDate, numberAbbr, numberToHex, objectAssign, objectEach, objectEachAsync, objectFill, objectGet, objectHas, objectMap, objectAssign as objectMerge, objectOmit, objectPick, onDomReady, once, pathJoin, pathNormalize, qsParse, qsStringify, randomNumber, randomString, randomUuid, removeClass, searchTreeById, setGlobal, setStyle, smoothScroll, stringAssign, stringCamelCase, stringEscapeHtml, stringFill, stringFormat, stringKebabCase, throttle, tooltipEvent, typeIs, uniqueNumber, uniqueString, urlDelParams, urlParse, urlSetParams, urlStringify, wait };
|