sculp-js 1.12.0 → 1.13.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/lib/cjs/array.js +1 -1
- package/lib/cjs/async.js +4 -4
- package/lib/cjs/base64.js +1 -1
- package/lib/cjs/clipboard.js +1 -1
- package/lib/cjs/cloneDeep.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 +1 -1
- package/lib/cjs/isEqual.js +1 -1
- package/lib/cjs/math.js +1 -1
- 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 +109 -34
- package/lib/cjs/type.js +1 -1
- package/lib/cjs/unique.js +1 -1
- package/lib/cjs/url.js +1 -1
- package/lib/cjs/validator.js +1 -1
- package/lib/cjs/variable.js +1 -1
- package/lib/cjs/watermark.js +1 -1
- package/lib/cjs/we-decode.js +1 -1
- package/lib/es/array.js +1 -1
- package/lib/es/async.js +4 -4
- package/lib/es/base64.js +1 -1
- package/lib/es/clipboard.js +1 -1
- package/lib/es/cloneDeep.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 +1 -1
- package/lib/es/isEqual.js +1 -1
- package/lib/es/math.js +1 -1
- 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 +110 -35
- package/lib/es/type.js +1 -1
- package/lib/es/unique.js +1 -1
- package/lib/es/url.js +1 -1
- package/lib/es/validator.js +1 -1
- package/lib/es/variable.js +1 -1
- package/lib/es/watermark.js +1 -1
- package/lib/es/we-decode.js +1 -1
- package/lib/index.d.ts +26 -12
- package/lib/umd/index.js +2 -2
- package/package.json +1 -1
package/lib/cjs/array.js
CHANGED
package/lib/cjs/async.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.
|
|
2
|
+
* sculp-js v1.13.0
|
|
3
3
|
* (c) 2023-present chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -62,16 +62,16 @@ function asyncMap(list, mapper, concurrency = Infinity) {
|
|
|
62
62
|
* Execute a promise safely
|
|
63
63
|
*
|
|
64
64
|
* @param { Promise } promise
|
|
65
|
-
* @param { Object= } errorExt - Additional Information you can pass
|
|
65
|
+
* @param { Object= } errorExt - Additional Information you can pass safeAwait the err object
|
|
66
66
|
* @return { Promise }
|
|
67
67
|
* @example
|
|
68
68
|
* async function asyncTaskWithCb(cb) {
|
|
69
69
|
let err, user, savedTask, notification;
|
|
70
70
|
|
|
71
|
-
[ err, user ] = await
|
|
71
|
+
[ err, user ] = await safeAwait(UserModel.findById(1));
|
|
72
72
|
if(!user) return cb('No user found');
|
|
73
73
|
|
|
74
|
-
[ err, savedTask ] = await
|
|
74
|
+
[ err, savedTask ] = await safeAwait(TaskModel({userId: user.id, name: 'Demo Task'}));
|
|
75
75
|
if(err) return cb('Error occurred while saving task')
|
|
76
76
|
|
|
77
77
|
cb(null, savedTask);
|
package/lib/cjs/base64.js
CHANGED
package/lib/cjs/clipboard.js
CHANGED
package/lib/cjs/cloneDeep.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
package/lib/cjs/isEqual.js
CHANGED
package/lib/cjs/math.js
CHANGED
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
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.
|
|
2
|
+
* sculp-js v1.13.0
|
|
3
3
|
* (c) 2023-present chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -17,58 +17,126 @@ const defaultSearchTreeOptions = {
|
|
|
17
17
|
ignoreCase: true
|
|
18
18
|
};
|
|
19
19
|
/**
|
|
20
|
-
*
|
|
20
|
+
* 树遍历函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
|
|
21
21
|
* @param {ArrayLike<V>} tree 树形数据
|
|
22
22
|
* @param {Function} iterator 迭代函数, 返回值为true时continue, 返回值为false时break
|
|
23
|
-
* @param {
|
|
24
|
-
|
|
23
|
+
* @param {options} options 支持定制子元素名称、反向遍历、广度优先遍历,默认{
|
|
24
|
+
childField: 'children',
|
|
25
|
+
reverse: false,
|
|
26
|
+
breadthFirst: false
|
|
27
|
+
}
|
|
25
28
|
* @returns {*}
|
|
26
29
|
*/
|
|
27
|
-
function forEachDeep(tree, iterator,
|
|
30
|
+
function forEachDeep(tree, iterator, options = {
|
|
31
|
+
childField: 'children',
|
|
32
|
+
reverse: false,
|
|
33
|
+
breadthFirst: false
|
|
34
|
+
}) {
|
|
35
|
+
const { childField = 'children', reverse = false, breadthFirst = false } = type.isObject(options) ? options : {};
|
|
28
36
|
let isBreak = false;
|
|
37
|
+
const queue = [];
|
|
29
38
|
const walk = (arr, parent, level = 0) => {
|
|
30
|
-
if (
|
|
31
|
-
for (let
|
|
39
|
+
if (reverse) {
|
|
40
|
+
for (let index = arr.length - 1; index >= 0; index--) {
|
|
32
41
|
if (isBreak) {
|
|
33
42
|
break;
|
|
34
43
|
}
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
44
|
+
const item = arr[index];
|
|
45
|
+
// 广度优先
|
|
46
|
+
if (breadthFirst) {
|
|
47
|
+
queue.push({ item, index, array: arr, tree, parent, level });
|
|
39
48
|
}
|
|
40
|
-
else
|
|
41
|
-
|
|
49
|
+
else {
|
|
50
|
+
const re = iterator(item, index, arr, tree, parent, level);
|
|
51
|
+
if (re === false) {
|
|
52
|
+
isBreak = true;
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
else if (re === true) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
// @ts-ignore
|
|
59
|
+
if (item && Array.isArray(item[childField])) {
|
|
60
|
+
// @ts-ignore
|
|
61
|
+
walk(item[childField], item, level + 1);
|
|
62
|
+
}
|
|
42
63
|
}
|
|
43
|
-
|
|
44
|
-
|
|
64
|
+
}
|
|
65
|
+
if (breadthFirst) {
|
|
66
|
+
// Process queue
|
|
67
|
+
while (!isBreak) {
|
|
68
|
+
const current = queue.shift();
|
|
69
|
+
// iterate(info);
|
|
45
70
|
// @ts-ignore
|
|
46
|
-
|
|
71
|
+
const { item, index, array, tree, parent, level } = current;
|
|
72
|
+
const re = iterator(item, index, array, tree, parent, level);
|
|
73
|
+
if (re === false) {
|
|
74
|
+
isBreak = true;
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
else if (re === true) {
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
// @ts-ignore
|
|
81
|
+
if (item && Array.isArray(item[childField])) {
|
|
82
|
+
// @ts-ignore
|
|
83
|
+
walk(item[childField], item, level + 1);
|
|
84
|
+
}
|
|
47
85
|
}
|
|
48
86
|
}
|
|
49
87
|
}
|
|
50
88
|
else {
|
|
51
|
-
for (let
|
|
89
|
+
for (let index = 0, len = arr.length; index < len; index++) {
|
|
52
90
|
if (isBreak) {
|
|
53
91
|
break;
|
|
54
92
|
}
|
|
55
|
-
const
|
|
56
|
-
if (
|
|
57
|
-
|
|
58
|
-
|
|
93
|
+
const item = arr[index];
|
|
94
|
+
if (breadthFirst) {
|
|
95
|
+
// 广度优先
|
|
96
|
+
queue.push({ item, index: index, array: arr, tree, parent, level });
|
|
59
97
|
}
|
|
60
|
-
else
|
|
61
|
-
|
|
98
|
+
else {
|
|
99
|
+
// 深度优先
|
|
100
|
+
const re = iterator(item, index, arr, tree, parent, level);
|
|
101
|
+
if (re === false) {
|
|
102
|
+
isBreak = true;
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
else if (re === true) {
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
// @ts-ignore
|
|
109
|
+
if (item && Array.isArray(item[childField])) {
|
|
110
|
+
// @ts-ignore
|
|
111
|
+
walk(item[childField], item, level + 1);
|
|
112
|
+
}
|
|
62
113
|
}
|
|
63
|
-
|
|
64
|
-
|
|
114
|
+
}
|
|
115
|
+
if (breadthFirst) {
|
|
116
|
+
while (!isBreak) {
|
|
117
|
+
const current = queue.shift();
|
|
118
|
+
if (!current)
|
|
119
|
+
break;
|
|
65
120
|
// @ts-ignore
|
|
66
|
-
|
|
121
|
+
const { item, index, array, tree, parent, level } = current;
|
|
122
|
+
const re = iterator(item, index, array, tree, parent, level);
|
|
123
|
+
if (re === false) {
|
|
124
|
+
isBreak = true;
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
else if (re === true) {
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
// @ts-ignore
|
|
131
|
+
if (item && Array.isArray(item[childField])) {
|
|
132
|
+
// @ts-ignore
|
|
133
|
+
walk(item[childField], item, level + 1);
|
|
134
|
+
}
|
|
67
135
|
}
|
|
68
136
|
}
|
|
69
137
|
}
|
|
70
138
|
};
|
|
71
|
-
walk(tree, null);
|
|
139
|
+
walk(tree, null, 0);
|
|
72
140
|
// @ts-ignore
|
|
73
141
|
tree = null;
|
|
74
142
|
}
|
|
@@ -180,11 +248,12 @@ function searchTreeById(tree, nodeId, config) {
|
|
|
180
248
|
/**
|
|
181
249
|
* 扁平化数组转换成树
|
|
182
250
|
* @param {any[]} list
|
|
183
|
-
* @param {IFieldOptions} options
|
|
251
|
+
* @param {IFieldOptions} options 定制id字段名,子元素字段名,父元素字段名,默认
|
|
252
|
+
* { keyField: 'key', childField: 'children', pidField: 'pid' }
|
|
184
253
|
* @returns {any[]}
|
|
185
254
|
*/
|
|
186
255
|
function formatTree(list, options = defaultFieldOptions) {
|
|
187
|
-
const { keyField, childField, pidField } = options;
|
|
256
|
+
const { keyField = 'key', childField = 'children', pidField = 'pid' } = type.isObject(options) ? options : {};
|
|
188
257
|
const treeArr = [];
|
|
189
258
|
const sourceMap = {};
|
|
190
259
|
for (let i = 0, len = list.length; i < len; i++) {
|
|
@@ -207,12 +276,13 @@ function formatTree(list, options = defaultFieldOptions) {
|
|
|
207
276
|
}
|
|
208
277
|
/**
|
|
209
278
|
* 树形结构转扁平化
|
|
210
|
-
* @param {any} treeList
|
|
211
|
-
* @param {IFieldOptions} options
|
|
212
|
-
*
|
|
279
|
+
* @param {any[]} treeList
|
|
280
|
+
* @param {IFieldOptions} options 定制id字段名,子元素字段名,父元素字段名,默认
|
|
281
|
+
* { keyField: 'key', childField: 'children', pidField: 'pid' }
|
|
282
|
+
* @returns {any[]}
|
|
213
283
|
*/
|
|
214
284
|
function flatTree(treeList, options = defaultFieldOptions) {
|
|
215
|
-
const {
|
|
285
|
+
const { keyField = 'key', childField = 'children', pidField = 'pid' } = type.isObject(options) ? options : {};
|
|
216
286
|
let res = [];
|
|
217
287
|
for (let i = 0, len = treeList.length; i < len; i++) {
|
|
218
288
|
const node = treeList[i];
|
|
@@ -243,7 +313,12 @@ function flatTree(treeList, options = defaultFieldOptions) {
|
|
|
243
313
|
* 2. 若无任何过滤条件或keyword模式匹配且keyword为空串,返回原对象;其他情况返回新数组
|
|
244
314
|
* @param {V[]} nodes
|
|
245
315
|
* @param {IFilterCondition} filterCondition
|
|
246
|
-
* @param {ISearchTreeOpts} options
|
|
316
|
+
* @param {ISearchTreeOpts} options 默认配置项 {
|
|
317
|
+
childField: 'children',
|
|
318
|
+
nameField: 'name',
|
|
319
|
+
removeEmptyChild: false,
|
|
320
|
+
ignoreCase: true
|
|
321
|
+
}
|
|
247
322
|
* @returns {V[]}
|
|
248
323
|
*/
|
|
249
324
|
function fuzzySearchTree(nodes, filterCondition, options = defaultSearchTreeOptions) {
|
package/lib/cjs/type.js
CHANGED
package/lib/cjs/unique.js
CHANGED
package/lib/cjs/url.js
CHANGED
package/lib/cjs/validator.js
CHANGED
package/lib/cjs/variable.js
CHANGED
package/lib/cjs/watermark.js
CHANGED
package/lib/cjs/we-decode.js
CHANGED
package/lib/es/array.js
CHANGED
package/lib/es/async.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.
|
|
2
|
+
* sculp-js v1.13.0
|
|
3
3
|
* (c) 2023-present chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -60,16 +60,16 @@ function asyncMap(list, mapper, concurrency = Infinity) {
|
|
|
60
60
|
* Execute a promise safely
|
|
61
61
|
*
|
|
62
62
|
* @param { Promise } promise
|
|
63
|
-
* @param { Object= } errorExt - Additional Information you can pass
|
|
63
|
+
* @param { Object= } errorExt - Additional Information you can pass safeAwait the err object
|
|
64
64
|
* @return { Promise }
|
|
65
65
|
* @example
|
|
66
66
|
* async function asyncTaskWithCb(cb) {
|
|
67
67
|
let err, user, savedTask, notification;
|
|
68
68
|
|
|
69
|
-
[ err, user ] = await
|
|
69
|
+
[ err, user ] = await safeAwait(UserModel.findById(1));
|
|
70
70
|
if(!user) return cb('No user found');
|
|
71
71
|
|
|
72
|
-
[ err, savedTask ] = await
|
|
72
|
+
[ err, savedTask ] = await safeAwait(TaskModel({userId: user.id, name: 'Demo Task'}));
|
|
73
73
|
if(err) return cb('Error occurred while saving task')
|
|
74
74
|
|
|
75
75
|
cb(null, savedTask);
|
package/lib/es/base64.js
CHANGED
package/lib/es/clipboard.js
CHANGED
package/lib/es/cloneDeep.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
package/lib/es/isEqual.js
CHANGED
package/lib/es/math.js
CHANGED
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