sculp-js 1.7.1 → 1.8.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 +5 -3
- 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 -28
- 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 +3 -4
- package/lib/cjs/math.js +1 -1
- package/lib/cjs/number.js +1 -1
- package/lib/cjs/object.js +190 -24
- 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 +30 -96
- package/lib/cjs/type.js +11 -4
- 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 +2 -27
- 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 +5 -5
- package/lib/es/math.js +1 -1
- package/lib/es/number.js +1 -1
- package/lib/es/object.js +190 -25
- 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 +31 -96
- package/lib/es/type.js +11 -5
- 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 +52 -54
- package/lib/umd/index.js +228 -149
- package/package.json +3 -10
package/lib/umd/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.
|
|
2
|
+
* sculp-js v1.8.0
|
|
3
3
|
* (c) 2023-present chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -122,11 +122,17 @@
|
|
|
122
122
|
return objectHas(any, 'length');
|
|
123
123
|
}
|
|
124
124
|
/**
|
|
125
|
-
*
|
|
125
|
+
* 判断任意值的数据类型,检查非对象时不如typeof、instanceof的性能高
|
|
126
|
+
*
|
|
127
|
+
* 当检查类对象时是不可靠的,对象可以通过定义 Symbol.toStringTag 属性来更改检查结果
|
|
128
|
+
*
|
|
129
|
+
* 详见:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/toString
|
|
126
130
|
* @param {unknown} any
|
|
127
|
-
* @returns
|
|
131
|
+
* @returns
|
|
128
132
|
*/
|
|
129
|
-
|
|
133
|
+
function typeIs(any) {
|
|
134
|
+
return Object.prototype.toString.call(any).slice(8, -1);
|
|
135
|
+
}
|
|
130
136
|
// 基本数据类型判断
|
|
131
137
|
const isString = (any) => typeof any === 'string';
|
|
132
138
|
const isBoolean = (any) => typeof any === 'boolean';
|
|
@@ -309,7 +315,7 @@
|
|
|
309
315
|
return obj2;
|
|
310
316
|
}
|
|
311
317
|
/**
|
|
312
|
-
*
|
|
318
|
+
* 对象去除
|
|
313
319
|
* @param {O} obj
|
|
314
320
|
* @param {K} keys
|
|
315
321
|
* @returns {Pick<O, ArrayElements<K>>}
|
|
@@ -394,6 +400,13 @@
|
|
|
394
400
|
});
|
|
395
401
|
return source;
|
|
396
402
|
}
|
|
403
|
+
/**
|
|
404
|
+
* 获取对象指定层级下的属性值(现在可用ES6+的可选链?.来替代)
|
|
405
|
+
* @param {AnyObject} obj
|
|
406
|
+
* @param {string} path
|
|
407
|
+
* @param {boolean} strict
|
|
408
|
+
* @returns
|
|
409
|
+
*/
|
|
397
410
|
function objectGet(obj, path, strict = false) {
|
|
398
411
|
path = path.replace(/\[(\w+)\]/g, '.$1');
|
|
399
412
|
path = path.replace(/^\./, '');
|
|
@@ -424,12 +437,16 @@
|
|
|
424
437
|
}
|
|
425
438
|
/**
|
|
426
439
|
* 深拷贝堪称完全体 即:任何类型的数据都会被深拷贝
|
|
440
|
+
*
|
|
441
|
+
* 包含对null、原始值、对象循环引用的处理
|
|
442
|
+
*
|
|
443
|
+
* 对Map、Set、ArrayBuffer、Date、RegExp、Array、Object及原型链属性方法执行深拷贝
|
|
427
444
|
* @param {T} source
|
|
428
445
|
* @param {WeakMap} map
|
|
429
446
|
* @returns {T}
|
|
430
447
|
*/
|
|
431
448
|
function cloneDeep(source, map = new WeakMap()) {
|
|
432
|
-
//
|
|
449
|
+
// 处理原始类型和 null/undefined
|
|
433
450
|
if (source === null || typeof source !== 'object') {
|
|
434
451
|
return source;
|
|
435
452
|
}
|
|
@@ -437,28 +454,33 @@
|
|
|
437
454
|
if (map.has(source)) {
|
|
438
455
|
return map.get(source);
|
|
439
456
|
}
|
|
440
|
-
// 处理
|
|
457
|
+
// 处理 ArrayBuffer
|
|
458
|
+
if (source instanceof ArrayBuffer) {
|
|
459
|
+
const copy = new ArrayBuffer(source.byteLength);
|
|
460
|
+
new Uint8Array(copy).set(new Uint8Array(source));
|
|
461
|
+
map.set(source, copy);
|
|
462
|
+
return copy;
|
|
463
|
+
}
|
|
464
|
+
// 处理 DataView 和 TypedArray (Uint8Array 等)
|
|
465
|
+
if (ArrayBuffer.isView(source)) {
|
|
466
|
+
const constructor = source.constructor;
|
|
467
|
+
const bufferCopy = cloneDeep(source.buffer, map);
|
|
468
|
+
return new constructor(bufferCopy, source.byteOffset, source.length);
|
|
469
|
+
}
|
|
470
|
+
// 处理 Date 对象
|
|
441
471
|
if (source instanceof Date) {
|
|
442
472
|
const copy = new Date(source.getTime());
|
|
443
473
|
map.set(source, copy);
|
|
444
474
|
return copy;
|
|
445
475
|
}
|
|
446
|
-
// 处理 RegExp
|
|
476
|
+
// 处理 RegExp 对象
|
|
447
477
|
if (source instanceof RegExp) {
|
|
448
478
|
const copy = new RegExp(source.source, source.flags);
|
|
479
|
+
copy.lastIndex = source.lastIndex; // 保留匹配状态
|
|
449
480
|
map.set(source, copy);
|
|
450
481
|
return copy;
|
|
451
482
|
}
|
|
452
|
-
//
|
|
453
|
-
if (Array.isArray(source)) {
|
|
454
|
-
const copy = [];
|
|
455
|
-
map.set(source, copy);
|
|
456
|
-
for (const item of source) {
|
|
457
|
-
copy.push(cloneDeep(item, map));
|
|
458
|
-
}
|
|
459
|
-
return copy;
|
|
460
|
-
}
|
|
461
|
-
// 处理 Map 类型
|
|
483
|
+
// 处理 Map
|
|
462
484
|
if (source instanceof Map) {
|
|
463
485
|
const copy = new Map();
|
|
464
486
|
map.set(source, copy);
|
|
@@ -467,7 +489,7 @@
|
|
|
467
489
|
});
|
|
468
490
|
return copy;
|
|
469
491
|
}
|
|
470
|
-
// 处理 Set
|
|
492
|
+
// 处理 Set
|
|
471
493
|
if (source instanceof Set) {
|
|
472
494
|
const copy = new Set();
|
|
473
495
|
map.set(source, copy);
|
|
@@ -476,22 +498,171 @@
|
|
|
476
498
|
});
|
|
477
499
|
return copy;
|
|
478
500
|
}
|
|
479
|
-
//
|
|
480
|
-
if (source
|
|
481
|
-
const copy =
|
|
482
|
-
new Uint8Array(copy).set(new Uint8Array(source));
|
|
501
|
+
// 处理数组 (包含稀疏数组)
|
|
502
|
+
if (Array.isArray(source)) {
|
|
503
|
+
const copy = Array.from({ length: source.length });
|
|
483
504
|
map.set(source, copy);
|
|
505
|
+
// 克隆所有有效索引
|
|
506
|
+
for (let i = 0; i < source.length; i++) {
|
|
507
|
+
if (i in source) {
|
|
508
|
+
// 保留空位
|
|
509
|
+
copy[i] = cloneDeep(source[i], map);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
// 克隆数组的自定义属性
|
|
513
|
+
const descriptors = Object.getOwnPropertyDescriptors(source);
|
|
514
|
+
for (const key of Reflect.ownKeys(descriptors)) {
|
|
515
|
+
Object.defineProperty(copy, key, {
|
|
516
|
+
...descriptors[key],
|
|
517
|
+
value: cloneDeep(descriptors[key].value, map)
|
|
518
|
+
});
|
|
519
|
+
}
|
|
484
520
|
return copy;
|
|
485
521
|
}
|
|
486
|
-
//
|
|
522
|
+
// 处理普通对象和类实例
|
|
487
523
|
const copy = Object.create(Object.getPrototypeOf(source));
|
|
488
524
|
map.set(source, copy);
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
525
|
+
const descriptors = Object.getOwnPropertyDescriptors(source);
|
|
526
|
+
for (const key of Reflect.ownKeys(descriptors)) {
|
|
527
|
+
const descriptor = descriptors[key];
|
|
528
|
+
if ('value' in descriptor) {
|
|
529
|
+
// 克隆数据属性
|
|
530
|
+
descriptor.value = cloneDeep(descriptor.value, map);
|
|
531
|
+
}
|
|
532
|
+
else {
|
|
533
|
+
// 处理访问器属性 (getter/setter)
|
|
534
|
+
if (descriptor.get) {
|
|
535
|
+
descriptor.get = cloneDeep(descriptor.get, map);
|
|
536
|
+
}
|
|
537
|
+
if (descriptor.set) {
|
|
538
|
+
descriptor.set = cloneDeep(descriptor.set, map);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
Object.defineProperty(copy, key, descriptor);
|
|
492
542
|
}
|
|
493
543
|
return copy;
|
|
494
544
|
}
|
|
545
|
+
/**
|
|
546
|
+
* 比较两值是否相等,适用所有数据类型
|
|
547
|
+
* @param {Comparable} a
|
|
548
|
+
* @param {Comparable} b
|
|
549
|
+
* @returns {boolean}
|
|
550
|
+
*/
|
|
551
|
+
function isEqual(a, b) {
|
|
552
|
+
return deepEqual(a, b);
|
|
553
|
+
}
|
|
554
|
+
function deepEqual(a, b, compared = new WeakMap()) {
|
|
555
|
+
// 相同值快速返回
|
|
556
|
+
if (Object.is(a, b))
|
|
557
|
+
return true;
|
|
558
|
+
// 类型不同直接返回false
|
|
559
|
+
const typeA = Object.prototype.toString.call(a);
|
|
560
|
+
const typeB = Object.prototype.toString.call(b);
|
|
561
|
+
if (typeA !== typeB)
|
|
562
|
+
return false;
|
|
563
|
+
// 只缓存对象类型
|
|
564
|
+
if (isObject(a) && isObject(b)) {
|
|
565
|
+
if (compared.has(a))
|
|
566
|
+
return compared.get(a) === b;
|
|
567
|
+
compared.set(a, b);
|
|
568
|
+
compared.set(b, a);
|
|
569
|
+
}
|
|
570
|
+
// 处理特殊对象类型
|
|
571
|
+
switch (typeA) {
|
|
572
|
+
case '[object Date]':
|
|
573
|
+
return a.getTime() === b.getTime();
|
|
574
|
+
case '[object RegExp]':
|
|
575
|
+
return a.toString() === b.toString();
|
|
576
|
+
case '[object Map]':
|
|
577
|
+
return compareMap(a, b, compared);
|
|
578
|
+
case '[object Set]':
|
|
579
|
+
return compareSet(a, b, compared);
|
|
580
|
+
case '[object ArrayBuffer]':
|
|
581
|
+
return compareArrayBuffer(a, b);
|
|
582
|
+
case '[object DataView]':
|
|
583
|
+
return compareDataView(a, b, compared);
|
|
584
|
+
case '[object Int8Array]':
|
|
585
|
+
case '[object Uint8Array]':
|
|
586
|
+
case '[object Uint8ClampedArray]':
|
|
587
|
+
case '[object Int16Array]':
|
|
588
|
+
case '[object Uint16Array]':
|
|
589
|
+
case '[object Int32Array]':
|
|
590
|
+
case '[object Uint32Array]':
|
|
591
|
+
case '[object Float32Array]':
|
|
592
|
+
case '[object Float64Array]':
|
|
593
|
+
return compareTypedArray(a, b, compared);
|
|
594
|
+
case '[object Object]':
|
|
595
|
+
return compareObjects(a, b, compared);
|
|
596
|
+
case '[object Array]':
|
|
597
|
+
return compareArrays(a, b, compared);
|
|
598
|
+
}
|
|
599
|
+
return false;
|
|
600
|
+
}
|
|
601
|
+
// 辅助比较函数
|
|
602
|
+
function compareMap(a, b, compared) {
|
|
603
|
+
if (a.size !== b.size)
|
|
604
|
+
return false;
|
|
605
|
+
for (const [key, value] of a) {
|
|
606
|
+
if (!b.has(key) || !deepEqual(value, b.get(key), compared))
|
|
607
|
+
return false;
|
|
608
|
+
}
|
|
609
|
+
return true;
|
|
610
|
+
}
|
|
611
|
+
function compareSet(a, b, compared) {
|
|
612
|
+
if (a.size !== b.size)
|
|
613
|
+
return false;
|
|
614
|
+
for (const value of a) {
|
|
615
|
+
let found = false;
|
|
616
|
+
for (const bValue of b) {
|
|
617
|
+
if (deepEqual(value, bValue, compared)) {
|
|
618
|
+
found = true;
|
|
619
|
+
break;
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
if (!found)
|
|
623
|
+
return false;
|
|
624
|
+
}
|
|
625
|
+
return true;
|
|
626
|
+
}
|
|
627
|
+
function compareArrayBuffer(a, b) {
|
|
628
|
+
if (a.byteLength !== b.byteLength)
|
|
629
|
+
return false;
|
|
630
|
+
return new DataView(a).getInt32(0) === new DataView(b).getInt32(0);
|
|
631
|
+
}
|
|
632
|
+
function compareDataView(a, b, compared) {
|
|
633
|
+
return a.byteLength === b.byteLength && deepEqual(new Uint8Array(a.buffer), new Uint8Array(b.buffer), compared);
|
|
634
|
+
}
|
|
635
|
+
function compareTypedArray(a, b, compared) {
|
|
636
|
+
return a.byteLength === b.byteLength && deepEqual(Array.from(a), Array.from(b), compared);
|
|
637
|
+
}
|
|
638
|
+
function compareObjects(a, b, compared) {
|
|
639
|
+
const keysA = Reflect.ownKeys(a);
|
|
640
|
+
const keysB = Reflect.ownKeys(b);
|
|
641
|
+
if (keysA.length !== keysB.length)
|
|
642
|
+
return false;
|
|
643
|
+
for (const key of keysA) {
|
|
644
|
+
if (!keysB.includes(key))
|
|
645
|
+
return false;
|
|
646
|
+
if (!deepEqual(a[key], b[key], compared))
|
|
647
|
+
return false;
|
|
648
|
+
}
|
|
649
|
+
// 原型链比较
|
|
650
|
+
return Object.getPrototypeOf(a) === Object.getPrototypeOf(b);
|
|
651
|
+
}
|
|
652
|
+
function compareArrays(a, b, compared) {
|
|
653
|
+
// 增加有效索引检查
|
|
654
|
+
const keysA = Object.keys(a).map(Number);
|
|
655
|
+
const keysB = Object.keys(b).map(Number);
|
|
656
|
+
if (keysA.length !== keysB.length)
|
|
657
|
+
return false;
|
|
658
|
+
// 递归比较每个元素
|
|
659
|
+
for (let i = 0; i < a.length; i++) {
|
|
660
|
+
if (!deepEqual(a[i], b[i], compared))
|
|
661
|
+
return false;
|
|
662
|
+
}
|
|
663
|
+
// 比较数组对象的其他属性
|
|
664
|
+
return compareObjects(a, b, compared);
|
|
665
|
+
}
|
|
495
666
|
|
|
496
667
|
/**
|
|
497
668
|
* 将字符串转换为驼峰格式
|
|
@@ -736,31 +907,6 @@
|
|
|
736
907
|
render();
|
|
737
908
|
});
|
|
738
909
|
}
|
|
739
|
-
const domReadyCallbacks = [];
|
|
740
|
-
const eventType = 'DOMContentLoaded';
|
|
741
|
-
const listener = () => {
|
|
742
|
-
domReadyCallbacks.forEach(callback => callback());
|
|
743
|
-
domReadyCallbacks.length = 0;
|
|
744
|
-
document.removeEventListener(eventType, listener);
|
|
745
|
-
};
|
|
746
|
-
document.addEventListener(eventType, listener);
|
|
747
|
-
let readied = false;
|
|
748
|
-
function isDomReady() {
|
|
749
|
-
if (readied)
|
|
750
|
-
return true;
|
|
751
|
-
readied = ['complete', 'loaded', 'interactive'].indexOf(document.readyState) !== -1;
|
|
752
|
-
return readied;
|
|
753
|
-
}
|
|
754
|
-
function onDomReady(callback) {
|
|
755
|
-
// document readied
|
|
756
|
-
if (isDomReady()) {
|
|
757
|
-
setTimeout(callback, 0);
|
|
758
|
-
}
|
|
759
|
-
// listen document to ready
|
|
760
|
-
else {
|
|
761
|
-
domReadyCallbacks.push(callback);
|
|
762
|
-
}
|
|
763
|
-
}
|
|
764
910
|
/**
|
|
765
911
|
* 获取元素样式属性的计算值
|
|
766
912
|
* @param {HTMLElement} el
|
|
@@ -2179,7 +2325,8 @@
|
|
|
2179
2325
|
const defaultSearchTreeOptions = {
|
|
2180
2326
|
childField: 'children',
|
|
2181
2327
|
nameField: 'name',
|
|
2182
|
-
|
|
2328
|
+
removeEmptyChild: false,
|
|
2329
|
+
ignoreCase: true
|
|
2183
2330
|
};
|
|
2184
2331
|
/**
|
|
2185
2332
|
* 深度优先遍历函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
|
|
@@ -2236,7 +2383,9 @@
|
|
|
2236
2383
|
walk(tree, null);
|
|
2237
2384
|
}
|
|
2238
2385
|
/**
|
|
2239
|
-
* 深度优先遍历的Map函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
|
|
2386
|
+
* 创建一个新数组, 深度优先遍历的Map函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
|
|
2387
|
+
*
|
|
2388
|
+
* 可遍历任何带有 length 属性和数字键的类数组对象
|
|
2240
2389
|
* @param {ArrayLike<V>} tree 树形数据
|
|
2241
2390
|
* @param {Function} iterator 迭代函数, 返回值为true时continue, 返回值为false时break
|
|
2242
2391
|
* @param {string} children 定制子元素的key
|
|
@@ -2336,91 +2485,6 @@
|
|
|
2336
2485
|
};
|
|
2337
2486
|
return getIds(toFlatArray(tree));
|
|
2338
2487
|
}
|
|
2339
|
-
/**
|
|
2340
|
-
* 使用迭代函数转换数组
|
|
2341
|
-
* @param {T} array
|
|
2342
|
-
* @param {Function} callback 迭代函数
|
|
2343
|
-
* @returns {Array}
|
|
2344
|
-
*/
|
|
2345
|
-
function flatMap(array, callback) {
|
|
2346
|
-
const result = [];
|
|
2347
|
-
array.forEach((value, index) => {
|
|
2348
|
-
result.push(...callback(value, index, array));
|
|
2349
|
-
});
|
|
2350
|
-
return result;
|
|
2351
|
-
}
|
|
2352
|
-
/**
|
|
2353
|
-
* 根据 idProp 与 parentIdProp 从对象数组中构建对应的树
|
|
2354
|
-
* 当 A[parentIdProp] === B[idProp] 时,对象A会被移动到对象B的children。
|
|
2355
|
-
* 当一个对象的 parentIdProp 不与其他对象的 idProp 字段相等时,该对象被作为树的顶层节点
|
|
2356
|
-
* @param {string} idProp 元素ID
|
|
2357
|
-
* @param {string} parentIdProp 父元素ID
|
|
2358
|
-
* @param {object[]} items 一维数组
|
|
2359
|
-
* @returns {WithChildren<T>[]} 树
|
|
2360
|
-
* @example
|
|
2361
|
-
* const array = [
|
|
2362
|
-
* { id: 'node-1', parent: 'root' },
|
|
2363
|
-
* { id: 'node-2', parent: 'root' },
|
|
2364
|
-
* { id: 'node-3', parent: 'node-2' },
|
|
2365
|
-
* { id: 'node-4', parent: 'node-2' },
|
|
2366
|
-
* { id: 'node-5', parent: 'node-4' },
|
|
2367
|
-
* ]
|
|
2368
|
-
* const tree = buildTree('id', 'parent', array)
|
|
2369
|
-
* expect(tree).toEqual([
|
|
2370
|
-
* { id: 'node-1', parent: 'root' },
|
|
2371
|
-
* {
|
|
2372
|
-
* id: 'node-2',
|
|
2373
|
-
* parent: 'root',
|
|
2374
|
-
* children: [
|
|
2375
|
-
* { id: 'node-3', parent: 'node-2' },
|
|
2376
|
-
* {
|
|
2377
|
-
* id: 'node-4',
|
|
2378
|
-
* parent: 'node-2',
|
|
2379
|
-
* children: [{ id: 'node-5', parent: 'node-4' }],
|
|
2380
|
-
* },
|
|
2381
|
-
* ],
|
|
2382
|
-
* },
|
|
2383
|
-
* ])
|
|
2384
|
-
*/
|
|
2385
|
-
function buildTree(idProp, parentIdProp, items) {
|
|
2386
|
-
const wrapperMap = new Map();
|
|
2387
|
-
const ensure = (id) => {
|
|
2388
|
-
if (wrapperMap.has(id)) {
|
|
2389
|
-
return wrapperMap.get(id);
|
|
2390
|
-
}
|
|
2391
|
-
//@ts-ignore
|
|
2392
|
-
const wrapper = { id, parent: null, item: null, children: [] };
|
|
2393
|
-
wrapperMap.set(id, wrapper);
|
|
2394
|
-
return wrapper;
|
|
2395
|
-
};
|
|
2396
|
-
for (const item of items) {
|
|
2397
|
-
const parentWrapper = ensure(item[parentIdProp]);
|
|
2398
|
-
const itemWrapper = ensure(item[idProp]);
|
|
2399
|
-
//@ts-ignore
|
|
2400
|
-
itemWrapper.parent = parentWrapper;
|
|
2401
|
-
//@ts-ignore
|
|
2402
|
-
parentWrapper.children.push(itemWrapper);
|
|
2403
|
-
//@ts-ignore
|
|
2404
|
-
itemWrapper.item = item;
|
|
2405
|
-
}
|
|
2406
|
-
const topLevelWrappers = flatMap(Array.from(wrapperMap.values()).filter(wrapper => wrapper.parent === null), wrapper => wrapper.children);
|
|
2407
|
-
return unwrapRecursively(topLevelWrappers);
|
|
2408
|
-
function unwrapRecursively(wrapperArray) {
|
|
2409
|
-
const result = [];
|
|
2410
|
-
for (const wrapper of wrapperArray) {
|
|
2411
|
-
if (wrapper.children.length === 0) {
|
|
2412
|
-
result.push(wrapper.item);
|
|
2413
|
-
}
|
|
2414
|
-
else {
|
|
2415
|
-
result.push({
|
|
2416
|
-
...wrapper.item,
|
|
2417
|
-
children: unwrapRecursively(wrapper.children)
|
|
2418
|
-
});
|
|
2419
|
-
}
|
|
2420
|
-
}
|
|
2421
|
-
return result;
|
|
2422
|
-
}
|
|
2423
|
-
}
|
|
2424
2488
|
/**
|
|
2425
2489
|
* 扁平化数组转换成树(效率高于buildTree)
|
|
2426
2490
|
* @param {any[]} list
|
|
@@ -2472,20 +2536,36 @@
|
|
|
2472
2536
|
}
|
|
2473
2537
|
/**
|
|
2474
2538
|
* 模糊搜索函数,返回包含搜索字符的节点及其祖先节点, 适用于树型组件的字符过滤功能
|
|
2475
|
-
*
|
|
2476
|
-
*
|
|
2539
|
+
* 以下搜索条件二选一,按先后优先级处理:
|
|
2540
|
+
* 1. 过滤函数filter, 返回true/false
|
|
2541
|
+
* 2. 匹配关键词,支持是否启用忽略大小写来判断
|
|
2542
|
+
*
|
|
2543
|
+
* 有以下特性:
|
|
2544
|
+
* 1. 可配置removeEmptyChild字段,来决定是否移除搜索结果中的空children字段
|
|
2545
|
+
* 2. 若无任何过滤条件或keyword模式匹配且keyword为空串,返回原对象;其他情况返回新数组
|
|
2546
|
+
* @param {V[]} nodes
|
|
2547
|
+
* @param {IFilterCondition} filterCondition
|
|
2477
2548
|
* @param {ISearchTreeOpts} options
|
|
2478
|
-
* @returns {
|
|
2549
|
+
* @returns {V[]}
|
|
2479
2550
|
*/
|
|
2480
|
-
function fuzzySearchTree(nodes,
|
|
2551
|
+
function fuzzySearchTree(nodes, filterCondition, options = defaultSearchTreeOptions) {
|
|
2552
|
+
if (!objectHas(filterCondition, 'filter') &&
|
|
2553
|
+
(!objectHas(filterCondition, 'keyword') || isEmpty(filterCondition.keyword))) {
|
|
2554
|
+
return nodes;
|
|
2555
|
+
}
|
|
2481
2556
|
const result = [];
|
|
2482
2557
|
for (const node of nodes) {
|
|
2483
2558
|
// 递归检查子节点是否匹配
|
|
2484
2559
|
const matchedChildren = node[options.childField] && node[options.childField].length > 0
|
|
2485
|
-
? fuzzySearchTree(node[options.childField] || [],
|
|
2560
|
+
? fuzzySearchTree(node[options.childField] || [], filterCondition, options)
|
|
2486
2561
|
: [];
|
|
2487
2562
|
// 检查当前节点是否匹配或者有匹配的子节点
|
|
2488
|
-
if (
|
|
2563
|
+
if ((objectHas(filterCondition, 'filter')
|
|
2564
|
+
? filterCondition.filter(node)
|
|
2565
|
+
: !options.ignoreCase
|
|
2566
|
+
? node[options.nameField].includes(filterCondition.keyword)
|
|
2567
|
+
: node[options.nameField].toLowerCase().includes(filterCondition.keyword.toLowerCase())) ||
|
|
2568
|
+
matchedChildren.length > 0) {
|
|
2489
2569
|
// 将当前节点加入结果中
|
|
2490
2570
|
if (node[options.childField]) {
|
|
2491
2571
|
if (matchedChildren.length > 0) {
|
|
@@ -2494,7 +2574,7 @@
|
|
|
2494
2574
|
[options.childField]: matchedChildren // 包含匹配的子节点
|
|
2495
2575
|
});
|
|
2496
2576
|
}
|
|
2497
|
-
else if (options.
|
|
2577
|
+
else if (options.removeEmptyChild) {
|
|
2498
2578
|
node[options.childField] && delete node[options.childField];
|
|
2499
2579
|
result.push({
|
|
2500
2580
|
...node
|
|
@@ -2887,7 +2967,6 @@
|
|
|
2887
2967
|
exports.arrayLike = arrayLike;
|
|
2888
2968
|
exports.arrayRemove = arrayRemove;
|
|
2889
2969
|
exports.asyncMap = asyncMap;
|
|
2890
|
-
exports.buildTree = buildTree;
|
|
2891
2970
|
exports.calculateDate = calculateDate;
|
|
2892
2971
|
exports.calculateDateTime = calculateDateTime;
|
|
2893
2972
|
exports.chooseLocalFile = chooseLocalFile;
|
|
@@ -2928,9 +3007,9 @@
|
|
|
2928
3007
|
exports.isBoolean = isBoolean;
|
|
2929
3008
|
exports.isDate = isDate;
|
|
2930
3009
|
exports.isDigit = isDigit;
|
|
2931
|
-
exports.isDomReady = isDomReady;
|
|
2932
3010
|
exports.isEmail = isEmail;
|
|
2933
3011
|
exports.isEmpty = isEmpty;
|
|
3012
|
+
exports.isEqual = isEqual;
|
|
2934
3013
|
exports.isError = isError;
|
|
2935
3014
|
exports.isFloat = isFloat;
|
|
2936
3015
|
exports.isFunction = isFunction;
|
|
@@ -2942,6 +3021,7 @@
|
|
|
2942
3021
|
exports.isNaN = isNaN;
|
|
2943
3022
|
exports.isNull = isNull;
|
|
2944
3023
|
exports.isNullOrUnDef = isNullOrUnDef;
|
|
3024
|
+
exports.isNullish = isNullOrUnDef;
|
|
2945
3025
|
exports.isNumber = isNumber;
|
|
2946
3026
|
exports.isNumerical = isNumerical;
|
|
2947
3027
|
exports.isObject = isObject;
|
|
@@ -2968,7 +3048,6 @@
|
|
|
2968
3048
|
exports.objectMerge = objectAssign;
|
|
2969
3049
|
exports.objectOmit = objectOmit;
|
|
2970
3050
|
exports.objectPick = objectPick;
|
|
2971
|
-
exports.onDomReady = onDomReady;
|
|
2972
3051
|
exports.once = once;
|
|
2973
3052
|
exports.parseQueryParams = parseQueryParams;
|
|
2974
3053
|
exports.parseVarFromString = parseVarFromString;
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sculp-js",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.0",
|
|
4
4
|
"packageManager": "npm@8.19.2",
|
|
5
|
-
"description": "js
|
|
5
|
+
"description": "js utils library, includes function library、class library",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"prepare": "husky install",
|
|
8
8
|
"build": "rollup --bundleConfigAsCjs --config rollup.config.js",
|
|
@@ -19,7 +19,6 @@
|
|
|
19
19
|
"release:major": "standard-version --release-as major",
|
|
20
20
|
"commit": "git-cz"
|
|
21
21
|
},
|
|
22
|
-
"author": "chendq <deqiaochen@gmail.com>",
|
|
23
22
|
"main": "lib/cjs/index.js",
|
|
24
23
|
"module": "lib/es/index.js",
|
|
25
24
|
"browser": "lib/umd/index.js",
|
|
@@ -49,8 +48,7 @@
|
|
|
49
48
|
"license": "MIT",
|
|
50
49
|
"homepage": "https://github.com/chandq/sculp-js#readme",
|
|
51
50
|
"dependencies": {
|
|
52
|
-
"bezier-easing": "^2.1.0"
|
|
53
|
-
"core-js": "^3.33.0"
|
|
51
|
+
"bezier-easing": "^2.1.0"
|
|
54
52
|
},
|
|
55
53
|
"devDependencies": {
|
|
56
54
|
"@babel/core": "^7.23.2",
|
|
@@ -89,11 +87,6 @@
|
|
|
89
87
|
"rollup-plugin-dts": "^6.1.0",
|
|
90
88
|
"rollup-plugin-subpath-externals": "^3.4.0",
|
|
91
89
|
"standard-version": "^9.5.0",
|
|
92
|
-
"stylelint": "14.16",
|
|
93
|
-
"stylelint-config-css-modules": "^4.3.0",
|
|
94
|
-
"stylelint-config-prettier": "^9.0.5",
|
|
95
|
-
"stylelint-config-standard": "^20.0.0",
|
|
96
|
-
"ts-loader": "^9.5.0",
|
|
97
90
|
"typescript": "5.0.4"
|
|
98
91
|
},
|
|
99
92
|
"config": {
|