sculp-js 1.12.0 → 1.13.1

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.
Files changed (63) hide show
  1. package/lib/cjs/array.js +1 -1
  2. package/lib/cjs/async.js +4 -4
  3. package/lib/cjs/base64.js +1 -1
  4. package/lib/cjs/clipboard.js +1 -1
  5. package/lib/cjs/cloneDeep.js +1 -1
  6. package/lib/cjs/cookie.js +1 -1
  7. package/lib/cjs/date.js +1 -1
  8. package/lib/cjs/dom.js +1 -1
  9. package/lib/cjs/download.js +1 -1
  10. package/lib/cjs/easing.js +1 -1
  11. package/lib/cjs/file.js +1 -1
  12. package/lib/cjs/func.js +1 -1
  13. package/lib/cjs/index.js +1 -1
  14. package/lib/cjs/isEqual.js +1 -1
  15. package/lib/cjs/math.js +1 -1
  16. package/lib/cjs/number.js +1 -1
  17. package/lib/cjs/object.js +1 -1
  18. package/lib/cjs/path.js +1 -1
  19. package/lib/cjs/qs.js +1 -1
  20. package/lib/cjs/random.js +1 -1
  21. package/lib/cjs/string.js +1 -1
  22. package/lib/cjs/tooltip.js +1 -1
  23. package/lib/cjs/tree.js +142 -59
  24. package/lib/cjs/type.js +1 -1
  25. package/lib/cjs/unique.js +1 -1
  26. package/lib/cjs/url.js +1 -1
  27. package/lib/cjs/validator.js +1 -1
  28. package/lib/cjs/variable.js +1 -1
  29. package/lib/cjs/watermark.js +1 -1
  30. package/lib/cjs/we-decode.js +1 -1
  31. package/lib/es/array.js +1 -1
  32. package/lib/es/async.js +4 -4
  33. package/lib/es/base64.js +1 -1
  34. package/lib/es/clipboard.js +1 -1
  35. package/lib/es/cloneDeep.js +1 -1
  36. package/lib/es/cookie.js +1 -1
  37. package/lib/es/date.js +1 -1
  38. package/lib/es/dom.js +1 -1
  39. package/lib/es/download.js +1 -1
  40. package/lib/es/easing.js +1 -1
  41. package/lib/es/file.js +1 -1
  42. package/lib/es/func.js +1 -1
  43. package/lib/es/index.js +1 -1
  44. package/lib/es/isEqual.js +1 -1
  45. package/lib/es/math.js +1 -1
  46. package/lib/es/number.js +1 -1
  47. package/lib/es/object.js +1 -1
  48. package/lib/es/path.js +1 -1
  49. package/lib/es/qs.js +1 -1
  50. package/lib/es/random.js +1 -1
  51. package/lib/es/string.js +1 -1
  52. package/lib/es/tooltip.js +1 -1
  53. package/lib/es/tree.js +143 -60
  54. package/lib/es/type.js +1 -1
  55. package/lib/es/unique.js +1 -1
  56. package/lib/es/url.js +1 -1
  57. package/lib/es/validator.js +1 -1
  58. package/lib/es/variable.js +1 -1
  59. package/lib/es/watermark.js +1 -1
  60. package/lib/es/we-decode.js +1 -1
  61. package/lib/index.d.ts +41 -24
  62. package/lib/umd/index.js +2 -2
  63. package/package.json +1 -1
package/lib/es/date.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.12.0
2
+ * sculp-js v1.13.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/dom.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.12.0
2
+ * sculp-js v1.13.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.12.0
2
+ * sculp-js v1.13.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/easing.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.12.0
2
+ * sculp-js v1.13.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/file.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.12.0
2
+ * sculp-js v1.13.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/func.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.12.0
2
+ * sculp-js v1.13.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.12.0
2
+ * sculp-js v1.13.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/isEqual.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.12.0
2
+ * sculp-js v1.13.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/math.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.12.0
2
+ * sculp-js v1.13.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/number.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.12.0
2
+ * sculp-js v1.13.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/object.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.12.0
2
+ * sculp-js v1.13.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/path.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.12.0
2
+ * sculp-js v1.13.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/qs.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.12.0
2
+ * sculp-js v1.13.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/random.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.12.0
2
+ * sculp-js v1.13.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/string.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.12.0
2
+ * sculp-js v1.13.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/tooltip.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.12.0
2
+ * sculp-js v1.13.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/tree.js CHANGED
@@ -1,11 +1,11 @@
1
1
  /*!
2
- * sculp-js v1.12.0
2
+ * sculp-js v1.13.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
6
6
 
7
7
  import { objectOmit } from './object.js';
8
- import { objectHas, isEmpty } from './type.js';
8
+ import { isObject, objectHas, isEmpty } from './type.js';
9
9
 
10
10
  const defaultFieldOptions = { keyField: 'key', childField: 'children', pidField: 'pid' };
11
11
  const defaultSearchTreeOptions = {
@@ -15,58 +15,126 @@ const defaultSearchTreeOptions = {
15
15
  ignoreCase: true
16
16
  };
17
17
  /**
18
- * 深度优先遍历函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
18
+ * 树遍历函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
19
19
  * @param {ArrayLike<V>} tree 树形数据
20
20
  * @param {Function} iterator 迭代函数, 返回值为true时continue, 返回值为false时break
21
- * @param {string} children 定制子元素的key
22
- * @param {boolean} isReverse 是否反向遍历
21
+ * @param {options} options 支持定制子元素名称、反向遍历、广度优先遍历,默认{
22
+ childField: 'children',
23
+ reverse: false,
24
+ breadthFirst: false
25
+ }
23
26
  * @returns {*}
24
27
  */
25
- function forEachDeep(tree, iterator, children = 'children', isReverse = false) {
28
+ function forEachDeep(tree, iterator, options = {
29
+ childField: 'children',
30
+ reverse: false,
31
+ breadthFirst: false
32
+ }) {
33
+ const { childField = 'children', reverse = false, breadthFirst = false } = isObject(options) ? options : {};
26
34
  let isBreak = false;
35
+ const queue = [];
27
36
  const walk = (arr, parent, level = 0) => {
28
- if (isReverse) {
29
- for (let i = arr.length - 1; i >= 0; i--) {
37
+ if (reverse) {
38
+ for (let index = arr.length - 1; index >= 0; index--) {
30
39
  if (isBreak) {
31
40
  break;
32
41
  }
33
- const re = iterator(arr[i], i, arr, tree, parent, level);
34
- if (re === false) {
35
- isBreak = true;
36
- break;
42
+ const item = arr[index];
43
+ // 广度优先
44
+ if (breadthFirst) {
45
+ queue.push({ item, index, array: arr, tree, parent, level });
37
46
  }
38
- else if (re === true) {
39
- continue;
47
+ else {
48
+ const re = iterator(item, index, arr, tree, parent, level);
49
+ if (re === false) {
50
+ isBreak = true;
51
+ break;
52
+ }
53
+ else if (re === true) {
54
+ continue;
55
+ }
56
+ // @ts-ignore
57
+ if (item && Array.isArray(item[childField])) {
58
+ // @ts-ignore
59
+ walk(item[childField], item, level + 1);
60
+ }
40
61
  }
41
- // @ts-ignore
42
- if (arr[i] && Array.isArray(arr[i][children])) {
62
+ }
63
+ if (breadthFirst) {
64
+ // Process queue
65
+ while (queue.length > 0 && !isBreak) {
66
+ const current = queue.shift();
67
+ // iterate(info);
68
+ // @ts-ignore
69
+ const { item, index, array, tree, parent, level } = current;
70
+ const re = iterator(item, index, array, tree, parent, level);
71
+ if (re === false) {
72
+ isBreak = true;
73
+ break;
74
+ }
75
+ else if (re === true) {
76
+ continue;
77
+ }
43
78
  // @ts-ignore
44
- walk(arr[i][children], arr[i], level + 1);
79
+ if (item && Array.isArray(item[childField])) {
80
+ // @ts-ignore
81
+ walk(item[childField], item, level + 1);
82
+ }
45
83
  }
46
84
  }
47
85
  }
48
86
  else {
49
- for (let i = 0, len = arr.length; i < len; i++) {
87
+ for (let index = 0, len = arr.length; index < len; index++) {
50
88
  if (isBreak) {
51
89
  break;
52
90
  }
53
- const re = iterator(arr[i], i, arr, tree, parent, level);
54
- if (re === false) {
55
- isBreak = true;
56
- break;
91
+ const item = arr[index];
92
+ if (breadthFirst) {
93
+ // 广度优先
94
+ queue.push({ item, index: index, array: arr, tree, parent, level });
57
95
  }
58
- else if (re === true) {
59
- continue;
96
+ else {
97
+ // 深度优先
98
+ const re = iterator(item, index, arr, tree, parent, level);
99
+ if (re === false) {
100
+ isBreak = true;
101
+ break;
102
+ }
103
+ else if (re === true) {
104
+ continue;
105
+ }
106
+ // @ts-ignore
107
+ if (item && Array.isArray(item[childField])) {
108
+ // @ts-ignore
109
+ walk(item[childField], item, level + 1);
110
+ }
60
111
  }
61
- // @ts-ignore
62
- if (arr[i] && Array.isArray(arr[i][children])) {
112
+ }
113
+ if (breadthFirst) {
114
+ while (queue.length > 0 && !isBreak) {
115
+ const current = queue.shift();
116
+ if (!current)
117
+ break;
118
+ // @ts-ignore
119
+ const { item, index, array, tree, parent, level } = current;
120
+ const re = iterator(item, index, array, tree, parent, level);
121
+ if (re === false) {
122
+ isBreak = true;
123
+ break;
124
+ }
125
+ else if (re === true) {
126
+ continue;
127
+ }
63
128
  // @ts-ignore
64
- walk(arr[i][children], arr[i], level + 1);
129
+ if (item && Array.isArray(item[childField])) {
130
+ // @ts-ignore
131
+ walk(item[childField], item, level + 1);
132
+ }
65
133
  }
66
134
  }
67
135
  }
68
136
  };
69
- walk(tree, null);
137
+ walk(tree, null, 0);
70
138
  // @ts-ignore
71
139
  tree = null;
72
140
  }
@@ -76,20 +144,27 @@ function forEachDeep(tree, iterator, children = 'children', isReverse = false) {
76
144
  * 可遍历任何带有 length 属性和数字键的类数组对象
77
145
  * @param {ArrayLike<V>} tree 树形数据
78
146
  * @param {Function} iterator 迭代函数, 返回值为true时continue, 返回值为false时break
79
- * @param {string} children 定制子元素的key
80
- * @param {boolean} isReverse 是否反向遍历
147
+ * @param {options} options 支持定制子元素名称、反向遍历,默认{
148
+ childField: 'children',
149
+ reverse: false,
150
+ }
81
151
  * @returns {any[]} 新的一棵树
82
152
  */
83
- function mapDeep(tree, iterator, children = 'children', isReverse = false) {
153
+ function mapDeep(tree, iterator, options = {
154
+ childField: 'children',
155
+ reverse: false
156
+ }) {
157
+ const { childField = 'children', reverse = false } = isObject(options) ? options : {};
84
158
  let isBreak = false;
85
159
  const newTree = [];
86
160
  const walk = (arr, parent, newTree, level = 0) => {
87
- if (isReverse) {
161
+ if (reverse) {
88
162
  for (let i = arr.length - 1; i >= 0; i--) {
89
163
  if (isBreak) {
90
164
  break;
91
165
  }
92
- const re = iterator(arr[i], i, arr, tree, parent, level);
166
+ const item = arr[i];
167
+ const re = iterator(item, i, arr, tree, parent, level);
93
168
  if (re === false) {
94
169
  isBreak = true;
95
170
  break;
@@ -97,16 +172,16 @@ function mapDeep(tree, iterator, children = 'children', isReverse = false) {
97
172
  else if (re === true) {
98
173
  continue;
99
174
  }
100
- newTree.push(objectOmit(re, [children]));
175
+ newTree.push(objectOmit(re, [childField]));
101
176
  // @ts-ignore
102
- if (arr[i] && Array.isArray(arr[i][children])) {
103
- newTree[newTree.length - 1][children] = [];
177
+ if (item && Array.isArray(item[childField])) {
178
+ newTree[newTree.length - 1][childField] = [];
104
179
  // @ts-ignore
105
- walk(arr[i][children], arr[i], newTree[newTree.length - 1][children], level + 1);
180
+ walk(item[childField], item, newTree[newTree.length - 1][childField], level + 1);
106
181
  }
107
182
  else {
108
183
  // children非有效数组时,移除该属性字段
109
- delete re[children];
184
+ delete re[childField];
110
185
  }
111
186
  }
112
187
  }
@@ -115,7 +190,8 @@ function mapDeep(tree, iterator, children = 'children', isReverse = false) {
115
190
  if (isBreak) {
116
191
  break;
117
192
  }
118
- const re = iterator(arr[i], i, arr, tree, parent, level);
193
+ const item = arr[i];
194
+ const re = iterator(item, i, arr, tree, parent, level);
119
195
  if (re === false) {
120
196
  isBreak = true;
121
197
  break;
@@ -123,16 +199,16 @@ function mapDeep(tree, iterator, children = 'children', isReverse = false) {
123
199
  else if (re === true) {
124
200
  continue;
125
201
  }
126
- newTree.push(objectOmit(re, [children]));
202
+ newTree.push(objectOmit(re, [childField]));
127
203
  // @ts-ignore
128
- if (arr[i] && Array.isArray(arr[i][children])) {
129
- newTree[newTree.length - 1][children] = [];
204
+ if (item && Array.isArray(item[childField])) {
205
+ newTree[newTree.length - 1][childField] = [];
130
206
  // @ts-ignore
131
- walk(arr[i][children], arr[i], newTree[newTree.length - 1][children], level + 1);
207
+ walk(item[childField], item, newTree[newTree.length - 1][childField], level + 1);
132
208
  }
133
209
  else {
134
210
  // children非有效数组时,移除该属性字段
135
- delete re[children];
211
+ delete re[childField];
136
212
  }
137
213
  }
138
214
  }
@@ -146,30 +222,30 @@ function mapDeep(tree, iterator, children = 'children', isReverse = false) {
146
222
  * 在树中找到 id 为某个值的节点,并返回上游的所有父级节点
147
223
  *
148
224
  * @param {ArrayLike<T>} tree - 树形数据
149
- * @param {IdLike} nodeId - 元素ID
150
- * @param {ITreeConf} config - 迭代配置项
151
- * @returns {[IdLike[], ITreeItem<V>[]]} - 由parentId...childId, parentObject-childObject组成的二维数组
225
+ * @param {number | string} nodeId - 目标元素ID
226
+ * @param {ITreeConf} options - 迭代配置项, 默认:{ children = 'children', id = 'id' }
227
+ * @returns {[(number | string)[], V[]]} - 由parentId...childId, parentObject-childObject组成的二维数组
152
228
  */
153
- function searchTreeById(tree, nodeId, config) {
154
- const { children = 'children', id = 'id' } = config || {};
229
+ function searchTreeById(tree, nodeId, options = { childField: 'children', keyField: 'id' }) {
230
+ const { childField = 'children', keyField = 'id' } = isObject(options) ? options : {};
155
231
  const toFlatArray = (tree, parentId, parent) => {
156
232
  return tree.reduce((t, _) => {
157
- const child = _[children];
233
+ const child = _[childField];
158
234
  return [
159
235
  ...t,
160
236
  parentId ? { ..._, parentId, parent } : _,
161
- ...(child && child.length ? toFlatArray(child, _[id], _) : [])
237
+ ...(child && child.length ? toFlatArray(child, _[keyField], _) : [])
162
238
  ];
163
239
  }, []);
164
240
  };
165
241
  const getIds = (flatArray) => {
166
- let child = flatArray.find(_ => _[id] === nodeId);
242
+ let child = flatArray.find(_ => _[keyField] === nodeId);
167
243
  const { parent, parentId, ...other } = child;
168
244
  let ids = [nodeId], nodes = [other];
169
245
  while (child && child.parentId) {
170
246
  ids = [child.parentId, ...ids];
171
247
  nodes = [child.parent, ...nodes];
172
- child = flatArray.find(_ => _[id] === child.parentId); // eslint-disable-line
248
+ child = flatArray.find(_ => _[keyField] === child.parentId); // eslint-disable-line
173
249
  }
174
250
  return [ids, nodes];
175
251
  };
@@ -178,11 +254,12 @@ function searchTreeById(tree, nodeId, config) {
178
254
  /**
179
255
  * 扁平化数组转换成树
180
256
  * @param {any[]} list
181
- * @param {IFieldOptions} options
257
+ * @param {IFieldOptions} options 定制id字段名,子元素字段名,父元素字段名,默认
258
+ * { keyField: 'key', childField: 'children', pidField: 'pid' }
182
259
  * @returns {any[]}
183
260
  */
184
261
  function formatTree(list, options = defaultFieldOptions) {
185
- const { keyField, childField, pidField } = options;
262
+ const { keyField = 'key', childField = 'children', pidField = 'pid' } = isObject(options) ? options : {};
186
263
  const treeArr = [];
187
264
  const sourceMap = {};
188
265
  for (let i = 0, len = list.length; i < len; i++) {
@@ -205,12 +282,13 @@ function formatTree(list, options = defaultFieldOptions) {
205
282
  }
206
283
  /**
207
284
  * 树形结构转扁平化
208
- * @param {any} treeList
209
- * @param {IFieldOptions} options
210
- * @returns {*}
285
+ * @param {any[]} treeList
286
+ * @param {IFieldOptions} options 定制id字段名,子元素字段名,父元素字段名,默认
287
+ * { keyField: 'key', childField: 'children', pidField: 'pid' }
288
+ * @returns {any[]}
211
289
  */
212
290
  function flatTree(treeList, options = defaultFieldOptions) {
213
- const { childField, keyField, pidField } = options;
291
+ const { keyField = 'key', childField = 'children', pidField = 'pid' } = isObject(options) ? options : {};
214
292
  let res = [];
215
293
  for (let i = 0, len = treeList.length; i < len; i++) {
216
294
  const node = treeList[i];
@@ -241,7 +319,12 @@ function flatTree(treeList, options = defaultFieldOptions) {
241
319
  * 2. 若无任何过滤条件或keyword模式匹配且keyword为空串,返回原对象;其他情况返回新数组
242
320
  * @param {V[]} nodes
243
321
  * @param {IFilterCondition} filterCondition
244
- * @param {ISearchTreeOpts} options
322
+ * @param {ISearchTreeOpts} options 默认配置项 {
323
+ childField: 'children',
324
+ nameField: 'name',
325
+ removeEmptyChild: false,
326
+ ignoreCase: true
327
+ }
245
328
  * @returns {V[]}
246
329
  */
247
330
  function fuzzySearchTree(nodes, filterCondition, options = defaultSearchTreeOptions) {
package/lib/es/type.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.12.0
2
+ * sculp-js v1.13.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/unique.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.12.0
2
+ * sculp-js v1.13.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/url.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.12.0
2
+ * sculp-js v1.13.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.12.0
2
+ * sculp-js v1.13.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.12.0
2
+ * sculp-js v1.13.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.12.0
2
+ * sculp-js v1.13.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.12.0
2
+ * sculp-js v1.13.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/index.d.ts CHANGED
@@ -634,16 +634,16 @@ declare function asyncMap<T, R>(list: Array<T>, mapper: (val: T, idx: number, li
634
634
  * Execute a promise safely
635
635
  *
636
636
  * @param { Promise } promise
637
- * @param { Object= } errorExt - Additional Information you can pass to the err object
637
+ * @param { Object= } errorExt - Additional Information you can pass safeAwait the err object
638
638
  * @return { Promise }
639
639
  * @example
640
640
  * async function asyncTaskWithCb(cb) {
641
641
  let err, user, savedTask, notification;
642
642
 
643
- [ err, user ] = await to(UserModel.findById(1));
643
+ [ err, user ] = await safeAwait(UserModel.findById(1));
644
644
  if(!user) return cb('No user found');
645
645
 
646
- [ err, savedTask ] = await to(TaskModel({userId: user.id, name: 'Demo Task'}));
646
+ [ err, savedTask ] = await safeAwait(TaskModel({userId: user.id, name: 'Demo Task'}));
647
647
  if(err) return cb('Error occurred while saving task')
648
648
 
649
649
  cb(null, savedTask);
@@ -909,53 +909,65 @@ interface IFilterCondition<V> {
909
909
  filter?: (args: V) => boolean;
910
910
  }
911
911
  /**
912
- * 深度优先遍历函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
912
+ * 树遍历函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
913
913
  * @param {ArrayLike<V>} tree 树形数据
914
914
  * @param {Function} iterator 迭代函数, 返回值为true时continue, 返回值为false时break
915
- * @param {string} children 定制子元素的key
916
- * @param {boolean} isReverse 是否反向遍历
915
+ * @param {options} options 支持定制子元素名称、反向遍历、广度优先遍历,默认{
916
+ childField: 'children',
917
+ reverse: false,
918
+ breadthFirst: false
919
+ }
917
920
  * @returns {*}
918
921
  */
919
- 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;
922
+ declare function forEachDeep<V>(tree: ArrayLike<V>, iterator: (val: V, index: number, currentArr: ArrayLike<V>, tree: ArrayLike<V>, parent: V | null, level: number) => boolean | void, options?: {
923
+ childField?: string;
924
+ reverse?: boolean;
925
+ breadthFirst?: boolean;
926
+ }): void;
920
927
  /**
921
928
  * 创建一个新数组, 深度优先遍历的Map函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
922
929
  *
923
930
  * 可遍历任何带有 length 属性和数字键的类数组对象
924
931
  * @param {ArrayLike<V>} tree 树形数据
925
932
  * @param {Function} iterator 迭代函数, 返回值为true时continue, 返回值为false时break
926
- * @param {string} children 定制子元素的key
927
- * @param {boolean} isReverse 是否反向遍历
933
+ * @param {options} options 支持定制子元素名称、反向遍历,默认{
934
+ childField: 'children',
935
+ reverse: false,
936
+ }
928
937
  * @returns {any[]} 新的一棵树
929
938
  */
930
- declare function mapDeep<T>(tree: T[], iterator: (val: T, i: number, currentArr: T[], tree: T[], parent: T | null, level: number) => {
939
+ declare function mapDeep<T>(tree: T[], iterator: (val: T, index: number, currentArr: T[], tree: T[], parent: T | null, level: number) => {
931
940
  [k: string | number]: any;
932
- } | boolean, children?: string, isReverse?: boolean): any[];
941
+ } | boolean, options?: {
942
+ childField?: string;
943
+ reverse?: boolean;
944
+ breadthFirst?: boolean;
945
+ }): any[];
933
946
  type IdLike = number | string;
934
- interface ITreeConf {
935
- id: string | number;
936
- children: string;
937
- }
947
+ type ITreeConf = Omit<IFieldOptions, 'pidField'>;
938
948
  /**
939
949
  * 在树中找到 id 为某个值的节点,并返回上游的所有父级节点
940
950
  *
941
951
  * @param {ArrayLike<T>} tree - 树形数据
942
- * @param {IdLike} nodeId - 元素ID
943
- * @param {ITreeConf} config - 迭代配置项
944
- * @returns {[IdLike[], ITreeItem<V>[]]} - 由parentId...childId, parentObject-childObject组成的二维数组
952
+ * @param {number | string} nodeId - 目标元素ID
953
+ * @param {ITreeConf} options - 迭代配置项, 默认:{ children = 'children', id = 'id' }
954
+ * @returns {[(number | string)[], V[]]} - 由parentId...childId, parentObject-childObject组成的二维数组
945
955
  */
946
- declare function searchTreeById<V>(tree: ArrayLike<V>, nodeId: IdLike, config?: ITreeConf): [IdLike[], ArrayLike<V>[]];
956
+ declare function searchTreeById<V>(tree: ArrayLike<V>, nodeId: IdLike, options?: ITreeConf): [(number | string)[], ArrayLike<V>[]];
947
957
  /**
948
958
  * 扁平化数组转换成树
949
959
  * @param {any[]} list
950
- * @param {IFieldOptions} options
960
+ * @param {IFieldOptions} options 定制id字段名,子元素字段名,父元素字段名,默认
961
+ * { keyField: 'key', childField: 'children', pidField: 'pid' }
951
962
  * @returns {any[]}
952
963
  */
953
964
  declare function formatTree(list: any[], options?: IFieldOptions): any[];
954
965
  /**
955
966
  * 树形结构转扁平化
956
- * @param {any} treeList
957
- * @param {IFieldOptions} options
958
- * @returns {*}
967
+ * @param {any[]} treeList
968
+ * @param {IFieldOptions} options 定制id字段名,子元素字段名,父元素字段名,默认
969
+ * { keyField: 'key', childField: 'children', pidField: 'pid' }
970
+ * @returns {any[]}
959
971
  */
960
972
  declare function flatTree(treeList: any[], options?: IFieldOptions): any[];
961
973
  /**
@@ -969,7 +981,12 @@ declare function flatTree(treeList: any[], options?: IFieldOptions): any[];
969
981
  * 2. 若无任何过滤条件或keyword模式匹配且keyword为空串,返回原对象;其他情况返回新数组
970
982
  * @param {V[]} nodes
971
983
  * @param {IFilterCondition} filterCondition
972
- * @param {ISearchTreeOpts} options
984
+ * @param {ISearchTreeOpts} options 默认配置项 {
985
+ childField: 'children',
986
+ nameField: 'name',
987
+ removeEmptyChild: false,
988
+ ignoreCase: true
989
+ }
973
990
  * @returns {V[]}
974
991
  */
975
992
  declare function fuzzySearchTree<V>(nodes: V[], filterCondition: IFilterCondition<V>, options?: ISearchTreeOpts): V[];