sculp-js 1.7.0 → 1.7.2
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 +2 -1
- package/lib/cjs/array.js +1 -1
- package/lib/cjs/async.js +1 -1
- package/lib/cjs/base64.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 +2 -2
- package/lib/cjs/math.js +1 -1
- package/lib/cjs/number.js +1 -1
- package/lib/cjs/object.js +104 -20
- 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 +36 -14
- 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 +1 -1
- package/lib/es/base64.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 +2 -2
- package/lib/es/math.js +1 -1
- package/lib/es/number.js +1 -1
- package/lib/es/object.js +104 -20
- 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 +36 -14
- 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 +32 -12
- package/lib/umd/index.js +134 -31
- package/package.json +1 -1
- package/lib/tsdoc-metadata.json +0 -11
package/README.md
CHANGED
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
- Tree
|
|
51
51
|
|
|
52
52
|
- forEachDeep 高性能的深度优先遍历函数, 支持continue、break,可定制id、children
|
|
53
|
-
-
|
|
53
|
+
- mapDeep 高性能的深度优先遍历的Map函数, 支持continue、break,可定制id、children
|
|
54
54
|
- searchTreeById 在树中找到 id 为某个值的节点,并返回上游的所有父级节点
|
|
55
55
|
- formatTree 高性能的数组转树函数
|
|
56
56
|
- flatTree 树转数组
|
|
@@ -64,6 +64,7 @@
|
|
|
64
64
|
- objectEach
|
|
65
65
|
- objectEachAsync
|
|
66
66
|
- objectGet
|
|
67
|
+
- cloneDeep
|
|
67
68
|
|
|
68
69
|
- Number
|
|
69
70
|
|
package/lib/cjs/array.js
CHANGED
package/lib/cjs/async.js
CHANGED
package/lib/cjs/base64.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.7.
|
|
2
|
+
* sculp-js v1.7.2
|
|
3
3
|
* (c) 2023-present chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -141,9 +141,9 @@ exports.tooltipEvent = tooltip.tooltipEvent;
|
|
|
141
141
|
exports.buildTree = tree.buildTree;
|
|
142
142
|
exports.flatTree = tree.flatTree;
|
|
143
143
|
exports.forEachDeep = tree.forEachDeep;
|
|
144
|
-
exports.forEachMap = tree.forEachMap;
|
|
145
144
|
exports.formatTree = tree.formatTree;
|
|
146
145
|
exports.fuzzySearchTree = tree.fuzzySearchTree;
|
|
146
|
+
exports.mapDeep = tree.mapDeep;
|
|
147
147
|
exports.searchTreeById = tree.searchTreeById;
|
|
148
148
|
exports.add = math.add;
|
|
149
149
|
exports.divide = math.divide;
|
package/lib/cjs/math.js
CHANGED
package/lib/cjs/number.js
CHANGED
package/lib/cjs/object.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.7.
|
|
2
|
+
* sculp-js v1.7.2
|
|
3
3
|
* (c) 2023-present chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -83,7 +83,7 @@ function objectPick(obj, keys) {
|
|
|
83
83
|
return obj2;
|
|
84
84
|
}
|
|
85
85
|
/**
|
|
86
|
-
*
|
|
86
|
+
* 对象去除
|
|
87
87
|
* @param {O} obj
|
|
88
88
|
* @param {K} keys
|
|
89
89
|
* @returns {Pick<O, ArrayElements<K>>}
|
|
@@ -198,26 +198,110 @@ function objectGet(obj, path, strict = false) {
|
|
|
198
198
|
}
|
|
199
199
|
/**
|
|
200
200
|
* 深拷贝堪称完全体 即:任何类型的数据都会被深拷贝
|
|
201
|
-
*
|
|
201
|
+
*
|
|
202
|
+
* 包含对null、原始值、对象循环引用的处理
|
|
203
|
+
*
|
|
204
|
+
* 对Map、Set、ArrayBuffer、Date、RegExp、Array、Object及原型链属性方法执行深拷贝
|
|
205
|
+
* @param {T} source
|
|
202
206
|
* @param {WeakMap} map
|
|
203
|
-
* @returns {
|
|
207
|
+
* @returns {T}
|
|
204
208
|
*/
|
|
205
|
-
function cloneDeep(
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
209
|
+
function cloneDeep(source, map = new WeakMap()) {
|
|
210
|
+
// 处理原始类型和 null/undefined
|
|
211
|
+
if (source === null || typeof source !== 'object') {
|
|
212
|
+
return source;
|
|
213
|
+
}
|
|
214
|
+
// 处理循环引用
|
|
215
|
+
if (map.has(source)) {
|
|
216
|
+
return map.get(source);
|
|
217
|
+
}
|
|
218
|
+
// 处理 ArrayBuffer
|
|
219
|
+
if (source instanceof ArrayBuffer) {
|
|
220
|
+
const copy = new ArrayBuffer(source.byteLength);
|
|
221
|
+
new Uint8Array(copy).set(new Uint8Array(source));
|
|
222
|
+
map.set(source, copy);
|
|
223
|
+
return copy;
|
|
224
|
+
}
|
|
225
|
+
// 处理 DataView 和 TypedArray (Uint8Array 等)
|
|
226
|
+
if (ArrayBuffer.isView(source)) {
|
|
227
|
+
const constructor = source.constructor;
|
|
228
|
+
const bufferCopy = cloneDeep(source.buffer, map);
|
|
229
|
+
return new constructor(bufferCopy, source.byteOffset, source.length);
|
|
230
|
+
}
|
|
231
|
+
// 处理 Date 对象
|
|
232
|
+
if (source instanceof Date) {
|
|
233
|
+
const copy = new Date(source.getTime());
|
|
234
|
+
map.set(source, copy);
|
|
235
|
+
return copy;
|
|
236
|
+
}
|
|
237
|
+
// 处理 RegExp 对象
|
|
238
|
+
if (source instanceof RegExp) {
|
|
239
|
+
const copy = new RegExp(source.source, source.flags);
|
|
240
|
+
copy.lastIndex = source.lastIndex; // 保留匹配状态
|
|
241
|
+
map.set(source, copy);
|
|
242
|
+
return copy;
|
|
243
|
+
}
|
|
244
|
+
// 处理 Map
|
|
245
|
+
if (source instanceof Map) {
|
|
246
|
+
const copy = new Map();
|
|
247
|
+
map.set(source, copy);
|
|
248
|
+
source.forEach((value, key) => {
|
|
249
|
+
copy.set(cloneDeep(key, map), cloneDeep(value, map));
|
|
250
|
+
});
|
|
251
|
+
return copy;
|
|
252
|
+
}
|
|
253
|
+
// 处理 Set
|
|
254
|
+
if (source instanceof Set) {
|
|
255
|
+
const copy = new Set();
|
|
256
|
+
map.set(source, copy);
|
|
257
|
+
source.forEach(value => {
|
|
258
|
+
copy.add(cloneDeep(value, map));
|
|
259
|
+
});
|
|
260
|
+
return copy;
|
|
261
|
+
}
|
|
262
|
+
// 处理数组 (包含稀疏数组)
|
|
263
|
+
if (Array.isArray(source)) {
|
|
264
|
+
const copy = Array.from({ length: source.length });
|
|
265
|
+
map.set(source, copy);
|
|
266
|
+
// 克隆所有有效索引
|
|
267
|
+
for (let i = 0; i < source.length; i++) {
|
|
268
|
+
if (i in source) {
|
|
269
|
+
// 保留空位
|
|
270
|
+
copy[i] = cloneDeep(source[i], map);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
// 克隆数组的自定义属性
|
|
274
|
+
const descriptors = Object.getOwnPropertyDescriptors(source);
|
|
275
|
+
for (const key of Reflect.ownKeys(descriptors)) {
|
|
276
|
+
Object.defineProperty(copy, key, {
|
|
277
|
+
...descriptors[key],
|
|
278
|
+
value: cloneDeep(descriptors[key].value, map)
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
return copy;
|
|
282
|
+
}
|
|
283
|
+
// 处理普通对象和类实例
|
|
284
|
+
const copy = Object.create(Object.getPrototypeOf(source));
|
|
285
|
+
map.set(source, copy);
|
|
286
|
+
const descriptors = Object.getOwnPropertyDescriptors(source);
|
|
287
|
+
for (const key of Reflect.ownKeys(descriptors)) {
|
|
288
|
+
const descriptor = descriptors[key];
|
|
289
|
+
if ('value' in descriptor) {
|
|
290
|
+
// 克隆数据属性
|
|
291
|
+
descriptor.value = cloneDeep(descriptor.value, map);
|
|
292
|
+
}
|
|
293
|
+
else {
|
|
294
|
+
// 处理访问器属性 (getter/setter)
|
|
295
|
+
if (descriptor.get) {
|
|
296
|
+
descriptor.get = cloneDeep(descriptor.get, map);
|
|
297
|
+
}
|
|
298
|
+
if (descriptor.set) {
|
|
299
|
+
descriptor.set = cloneDeep(descriptor.set, map);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
Object.defineProperty(copy, key, descriptor);
|
|
303
|
+
}
|
|
304
|
+
return copy;
|
|
221
305
|
}
|
|
222
306
|
|
|
223
307
|
exports.cloneDeep = cloneDeep;
|
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,16 +1,20 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.7.
|
|
2
|
+
* sculp-js v1.7.2
|
|
3
3
|
* (c) 2023-present chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
'use strict';
|
|
8
8
|
|
|
9
|
+
var object = require('./object.js');
|
|
10
|
+
var type = require('./type.js');
|
|
11
|
+
|
|
9
12
|
const defaultFieldOptions = { keyField: 'key', childField: 'children', pidField: 'pid' };
|
|
10
13
|
const defaultSearchTreeOptions = {
|
|
11
14
|
childField: 'children',
|
|
12
15
|
nameField: 'name',
|
|
13
|
-
|
|
16
|
+
removeEmptyChild: false,
|
|
17
|
+
ignoreCase: true
|
|
14
18
|
};
|
|
15
19
|
/**
|
|
16
20
|
* 深度优先遍历函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
|
|
@@ -67,14 +71,16 @@ function forEachDeep(tree, iterator, children = 'children', isReverse = false) {
|
|
|
67
71
|
walk(tree, null);
|
|
68
72
|
}
|
|
69
73
|
/**
|
|
70
|
-
* 深度优先遍历的Map函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
|
|
74
|
+
* 创建一个新数组, 深度优先遍历的Map函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
|
|
75
|
+
*
|
|
76
|
+
* 可遍历任何带有 length 属性和数字键的类数组对象
|
|
71
77
|
* @param {ArrayLike<V>} tree 树形数据
|
|
72
78
|
* @param {Function} iterator 迭代函数, 返回值为true时continue, 返回值为false时break
|
|
73
79
|
* @param {string} children 定制子元素的key
|
|
74
80
|
* @param {boolean} isReverse 是否反向遍历
|
|
75
81
|
* @returns {any[]} 新的一棵树
|
|
76
82
|
*/
|
|
77
|
-
function
|
|
83
|
+
function mapDeep(tree, iterator, children = 'children', isReverse = false) {
|
|
78
84
|
let isBreak = false;
|
|
79
85
|
const newTree = [];
|
|
80
86
|
const walk = (arr, parent, newTree, level = 0) => {
|
|
@@ -91,7 +97,7 @@ function forEachMap(tree, iterator, children = 'children', isReverse = false) {
|
|
|
91
97
|
else if (re === true) {
|
|
92
98
|
continue;
|
|
93
99
|
}
|
|
94
|
-
newTree.push(re);
|
|
100
|
+
newTree.push(object.objectOmit(re, [children]));
|
|
95
101
|
// @ts-ignore
|
|
96
102
|
if (arr[i] && Array.isArray(arr[i][children])) {
|
|
97
103
|
newTree[newTree.length - 1][children] = [];
|
|
@@ -117,7 +123,7 @@ function forEachMap(tree, iterator, children = 'children', isReverse = false) {
|
|
|
117
123
|
else if (re === true) {
|
|
118
124
|
continue;
|
|
119
125
|
}
|
|
120
|
-
newTree.push(re);
|
|
126
|
+
newTree.push(object.objectOmit(re, [children]));
|
|
121
127
|
// @ts-ignore
|
|
122
128
|
if (arr[i] && Array.isArray(arr[i][children])) {
|
|
123
129
|
newTree[newTree.length - 1][children] = [];
|
|
@@ -303,20 +309,36 @@ function flatTree(treeList, options = defaultFieldOptions) {
|
|
|
303
309
|
}
|
|
304
310
|
/**
|
|
305
311
|
* 模糊搜索函数,返回包含搜索字符的节点及其祖先节点, 适用于树型组件的字符过滤功能
|
|
306
|
-
*
|
|
307
|
-
*
|
|
312
|
+
* 以下搜索条件二选一,按先后优先级处理:
|
|
313
|
+
* 1. 过滤函数filter, 返回true/false
|
|
314
|
+
* 2. 匹配关键词,支持是否启用忽略大小写来判断
|
|
315
|
+
*
|
|
316
|
+
* 有以下特性:
|
|
317
|
+
* 1. 可配置removeEmptyChild字段,来决定是否移除搜索结果中的空children字段
|
|
318
|
+
* 2. 若无任何过滤条件或keyword模式匹配且keyword为空串,返回原对象;其他情况返回新数组
|
|
319
|
+
* @param {V[]} nodes
|
|
320
|
+
* @param {IFilterCondition} filterCondition
|
|
308
321
|
* @param {ISearchTreeOpts} options
|
|
309
|
-
* @returns {
|
|
322
|
+
* @returns {V[]}
|
|
310
323
|
*/
|
|
311
|
-
function fuzzySearchTree(nodes,
|
|
324
|
+
function fuzzySearchTree(nodes, filterCondition, options = defaultSearchTreeOptions) {
|
|
325
|
+
if (!type.objectHas(filterCondition, 'filter') &&
|
|
326
|
+
(!type.objectHas(filterCondition, 'keyword') || type.isEmpty(filterCondition.keyword))) {
|
|
327
|
+
return nodes;
|
|
328
|
+
}
|
|
312
329
|
const result = [];
|
|
313
330
|
for (const node of nodes) {
|
|
314
331
|
// 递归检查子节点是否匹配
|
|
315
332
|
const matchedChildren = node[options.childField] && node[options.childField].length > 0
|
|
316
|
-
? fuzzySearchTree(node[options.childField] || [],
|
|
333
|
+
? fuzzySearchTree(node[options.childField] || [], filterCondition, options)
|
|
317
334
|
: [];
|
|
318
335
|
// 检查当前节点是否匹配或者有匹配的子节点
|
|
319
|
-
if (
|
|
336
|
+
if ((type.objectHas(filterCondition, 'filter')
|
|
337
|
+
? filterCondition.filter(node)
|
|
338
|
+
: !options.ignoreCase
|
|
339
|
+
? node[options.nameField].includes(filterCondition.keyword)
|
|
340
|
+
: node[options.nameField].toLowerCase().includes(filterCondition.keyword.toLowerCase())) ||
|
|
341
|
+
matchedChildren.length > 0) {
|
|
320
342
|
// 将当前节点加入结果中
|
|
321
343
|
if (node[options.childField]) {
|
|
322
344
|
if (matchedChildren.length > 0) {
|
|
@@ -325,7 +347,7 @@ function fuzzySearchTree(nodes, query, options = defaultSearchTreeOptions) {
|
|
|
325
347
|
[options.childField]: matchedChildren // 包含匹配的子节点
|
|
326
348
|
});
|
|
327
349
|
}
|
|
328
|
-
else if (options.
|
|
350
|
+
else if (options.removeEmptyChild) {
|
|
329
351
|
node[options.childField] && delete node[options.childField];
|
|
330
352
|
result.push({
|
|
331
353
|
...node
|
|
@@ -352,7 +374,7 @@ function fuzzySearchTree(nodes, query, options = defaultSearchTreeOptions) {
|
|
|
352
374
|
exports.buildTree = buildTree;
|
|
353
375
|
exports.flatTree = flatTree;
|
|
354
376
|
exports.forEachDeep = forEachDeep;
|
|
355
|
-
exports.forEachMap = forEachMap;
|
|
356
377
|
exports.formatTree = formatTree;
|
|
357
378
|
exports.fuzzySearchTree = fuzzySearchTree;
|
|
379
|
+
exports.mapDeep = mapDeep;
|
|
358
380
|
exports.searchTreeById = searchTreeById;
|
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
package/lib/es/base64.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,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.7.
|
|
2
|
+
* sculp-js v1.7.2
|
|
3
3
|
* (c) 2023-present chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -24,7 +24,7 @@ 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, flatTree, forEachDeep,
|
|
27
|
+
export { buildTree, flatTree, forEachDeep, formatTree, fuzzySearchTree, mapDeep, searchTreeById } from './tree.js';
|
|
28
28
|
export { add, divide, multiply, strip, subtract } from './math.js';
|
|
29
29
|
export { weAtob, weBtoa } from './we-decode.js';
|
|
30
30
|
export { decodeFromBase64, encodeToBase64 } from './base64.js';
|
package/lib/es/math.js
CHANGED
package/lib/es/number.js
CHANGED