@codady/utils 0.0.11 → 0.0.13
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/CHANGELOG.md +40 -2
- package/dist/utils.cjs.js +269 -213
- package/dist/utils.cjs.min.js +3 -3
- package/dist/utils.esm.js +269 -213
- package/dist/utils.esm.min.js +3 -3
- package/dist/utils.umd.js +273 -215
- package/dist/utils.umd.min.js +3 -3
- package/dist.zip +0 -0
- package/examples/deepMerge.html +1049 -0
- package/modules.js +5 -11
- package/modules.ts +7 -7
- package/package.json +1 -1
- package/src/copyObjectWithSymbol.js +26 -0
- package/src/copyObjectWithSymbol.ts +28 -0
- package/src/deepEqual.js +1 -1
- package/src/deepEqual.ts +1 -1
- package/src/deepMerge.js +253 -13
- package/src/deepMerge.ts +306 -18
- package/src/deepMergeHelper.js +1 -3
- package/src/deepMergeHelper.ts +1 -3
- package/src/deepMergeMaps.js +4 -4
- package/src/deepMergeMaps.ts +6 -6
- package/src/deepMergeObjects.js +11 -4
- package/src/deepMergeObjects.ts +10 -4
- package/src/deepMergeSets.js +5 -10
- package/src/deepMergeSets.ts +6 -9
- package/src/getDataType - /345/211/257/346/234/254.js" +38 -0
- package/src/isEmpty - /345/211/257/346/234/254.js" +45 -0
- package/src/isEmpty.js +45 -0
- package/src/isEmpty.ts +44 -0
- package/src/mapMutableMethods - /345/211/257/346/234/254.js" +5 -0
- package/src/shallowCopy.js +78 -0
- package/src/shallowCopy.ts +93 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,7 +2,45 @@
|
|
|
2
2
|
|
|
3
3
|
All changes to Utils including new features, updates, and removals are documented here.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
|
|
6
|
+
## [v0.0.13] - 2025-12-25
|
|
7
|
+
|
|
8
|
+
### Distribution Files
|
|
9
|
+
* **JS**: https://unpkg.com/@codady/utils@0.0.13/dist/js/utils.js
|
|
10
|
+
* **Zip**:https://unpkg.com/@codady/utils@0.0.13/dist.zip
|
|
11
|
+
|
|
12
|
+
### Changes
|
|
13
|
+
|
|
14
|
+
#### Fixed
|
|
15
|
+
* Modify the `shallowCopy` function to add support for data types such as Date, Regex, Error, Buffer, ArrayBuffer, WeakSet, and WeakMap.修改`shallowCopy`函数,增加对Date、Regex、Error、Buffer、ArrayBuffer、WeakSet、WeakMap数据类型的支持。
|
|
16
|
+
|
|
17
|
+
#### Added
|
|
18
|
+
* Added the following functions: `copyObjectWithSymbol`.新增`copyObjectWithSymbol`函数。
|
|
19
|
+
|
|
20
|
+
#### Removed
|
|
21
|
+
* Null
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
## [v0.0.12] - 2025-12-25
|
|
25
|
+
|
|
26
|
+
### Distribution Files
|
|
27
|
+
* **JS**: https://unpkg.com/@codady/utils@0.0.12/dist/js/utils.js
|
|
28
|
+
* **Zip**:https://unpkg.com/@codady/utils@0.0.12/dist.zip
|
|
29
|
+
|
|
30
|
+
### Changes
|
|
31
|
+
|
|
32
|
+
#### Fixed
|
|
33
|
+
* Improved the `deepMerge` function to support deep merging of Arrays, Objects, Sets, and Maps.强化了`deepMerge`函数,支持深度合并Array、Object、Set和Map。
|
|
34
|
+
|
|
35
|
+
#### Added
|
|
36
|
+
* New parameters added to the deepMerge function: `deepClone`, `onBeforeMerge`, and `onAfterMerge`.`deepMerge`函数的参数新增deepClone、onBeforeMerge、onAfterMerge参数。
|
|
37
|
+
* Added the following functions: `shallowCopy`.新增`shallowCopy`函数。
|
|
38
|
+
|
|
39
|
+
#### Removed
|
|
40
|
+
* Removed the following functions: `deepEqual`, `deepMergeArrays`, `deepMergeObjects`, `deepMergeSets`, and `deepMergeMaps`.删除`deepEqual`、`deepMergeArrays`、`deepMergeObjects`、`deepMergeSets`、和`deepMegeMaps`函数。
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
## [v0.0.11] - 2025-12-24
|
|
6
44
|
|
|
7
45
|
### Distribution Files
|
|
8
46
|
* **JS**: https://unpkg.com/@codady/utils@0.0.11/dist/js/utils.js
|
|
@@ -14,7 +52,7 @@ All changes to Utils including new features, updates, and removals are documente
|
|
|
14
52
|
* Null
|
|
15
53
|
|
|
16
54
|
#### Added
|
|
17
|
-
*
|
|
55
|
+
* Added the following functions: `deepEqual`, `deepMerge`, `deepMergeArrays`, `deepMergeObjects`, `deepMergeSets`, and `deepMergeMaps`.新增`deepEqual`、`deepMerge`、`deepMergeArrays`、`deepMergeObjects`、`deepMergeSets`、和`deepMegeMaps`函数。
|
|
18
56
|
|
|
19
57
|
#### Removed
|
|
20
58
|
* Null
|
package/dist/utils.cjs.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
|
|
2
2
|
/*!
|
|
3
|
-
* @since Last modified: 2025-12-
|
|
3
|
+
* @since Last modified: 2025-12-25 14:33:12
|
|
4
4
|
* @name Utils for web front-end.
|
|
5
|
-
* @version 0.0.
|
|
5
|
+
* @version 0.0.13
|
|
6
6
|
* @author AXUI development team <3217728223@qq.com>
|
|
7
7
|
* @description This is a set of general-purpose JavaScript utility functions developed by the AXUI team. All functions are pure and do not involve CSS or other third-party libraries. They are suitable for any web front-end environment.
|
|
8
8
|
* @see {@link https://www.axui.cn|Official website}
|
|
@@ -16,8 +16,6 @@
|
|
|
16
16
|
|
|
17
17
|
'use strict';
|
|
18
18
|
|
|
19
|
-
var assert = require('assert');
|
|
20
|
-
|
|
21
19
|
const getDataType = (obj) => {
|
|
22
20
|
let tmp = Object.prototype.toString.call(obj).slice(8, -1), result;
|
|
23
21
|
if (tmp === 'Function' && /^\s*class\s+/.test(obj.toString())) {
|
|
@@ -424,31 +422,176 @@ const wrapMapMethods = ({ target, onBeforeMutate = () => { }, onAfterMutate = ()
|
|
|
424
422
|
return methods;
|
|
425
423
|
};
|
|
426
424
|
|
|
427
|
-
const
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
425
|
+
const copyObjectWithSymbol = (data) => {
|
|
426
|
+
if (!data || typeof data !== 'object') {
|
|
427
|
+
return data;
|
|
428
|
+
}
|
|
429
|
+
// Ensure the object type includes string and symbol keys
|
|
430
|
+
const obj = data, symbolProperties = Object.getOwnPropertySymbols(obj).reduce((acc, sym) => {
|
|
431
|
+
acc[sym] = obj[sym];
|
|
432
|
+
return acc;
|
|
433
|
+
}, {});
|
|
434
|
+
// Shallow copy the object and include the Symbol properties
|
|
435
|
+
return { ...obj, ...symbolProperties };
|
|
436
|
+
};
|
|
437
|
+
|
|
438
|
+
const shallowCopy = (data, options = {}) => {
|
|
439
|
+
const dataType = getDataType(data);
|
|
440
|
+
// Check if data is a Set
|
|
441
|
+
if (dataType === 'Set') {
|
|
442
|
+
// Shallow copy Set
|
|
443
|
+
return new Set([...data]);
|
|
444
|
+
}
|
|
445
|
+
// Check if data is a Map
|
|
446
|
+
if (dataType === 'Map') {
|
|
447
|
+
// Shallow copy Map
|
|
448
|
+
return new Map([...data]);
|
|
449
|
+
}
|
|
450
|
+
// Check if data is an Array
|
|
451
|
+
if (Array.isArray(data)) {
|
|
452
|
+
// Shallow copy Array
|
|
453
|
+
return [...data];
|
|
454
|
+
}
|
|
455
|
+
// Check if data is a Plain Object (including Symbol keys)
|
|
456
|
+
if (dataType === 'object') {
|
|
457
|
+
// Ensure the object type includes string and symbol keys
|
|
458
|
+
// Shallow copy the object and include the Symbol properties
|
|
459
|
+
return copyObjectWithSymbol(data);
|
|
460
|
+
}
|
|
461
|
+
// Check if data is a Date
|
|
462
|
+
if (dataType === 'Date') {
|
|
463
|
+
return new Date(data.getTime());
|
|
464
|
+
}
|
|
465
|
+
// Check if data is a RegExp
|
|
466
|
+
if (dataType === 'RegExp') {
|
|
467
|
+
return new RegExp(data.source, data.flags);
|
|
468
|
+
}
|
|
469
|
+
// Check if data is a Buffer (for Node.js)
|
|
470
|
+
if (dataType === 'Buffer') {
|
|
471
|
+
return Buffer.from(data);
|
|
472
|
+
}
|
|
473
|
+
// Check if data is an ArrayBuffer or TypedArray
|
|
474
|
+
if (dataType === 'ArrayBuffer' || ArrayBuffer.isView(data)) {
|
|
475
|
+
return data.slice(0);
|
|
476
|
+
}
|
|
477
|
+
// Check if data is a WeakSet
|
|
478
|
+
if (dataType === 'WeakSet') {
|
|
479
|
+
return new WeakSet([...data]);
|
|
480
|
+
}
|
|
481
|
+
// Check if data is a WeakMap
|
|
482
|
+
if (dataType === 'WeakMap') {
|
|
483
|
+
return new WeakMap([...data]);
|
|
484
|
+
}
|
|
485
|
+
// Check if data is an Error
|
|
486
|
+
if (dataType === 'Error') {
|
|
487
|
+
return new Error(data.message);
|
|
488
|
+
}
|
|
489
|
+
// For other types (such as numbers, strings, booleans, etc.), return the original value
|
|
490
|
+
return data;
|
|
491
|
+
};
|
|
492
|
+
|
|
493
|
+
const deepMerge = (target, source, opts = {}) => {
|
|
494
|
+
// Set default options
|
|
495
|
+
// 设置默认选项
|
|
496
|
+
const options = Object.assign({
|
|
497
|
+
// Merging mode for array and set collections:
|
|
498
|
+
// 'clear' means clear first then append; 'concat' means append directly at the end;
|
|
499
|
+
// 'replace' means merge sequentially (deep merging)
|
|
500
|
+
// 对于array和set合集的合并方式:'clear'表示先清空再追加;'concat'表示直接在尾部追加;
|
|
501
|
+
// 'replace'表示按顺序进行合并(深度合并)
|
|
502
|
+
dataMode: 'clear',
|
|
503
|
+
// Whether to directly append properties/items from source that target doesn't have.
|
|
504
|
+
// true: append; false: discard
|
|
505
|
+
// source对象如果有target对象没有的item或prop属性,是否直接新追加。true则追加;false则丢弃
|
|
506
|
+
inheritMissing: true,
|
|
507
|
+
// Whether to clone the target object, generating a brand new object. Default false.
|
|
508
|
+
// 是否克隆target对象,生成全新对象,默认false。
|
|
509
|
+
targetClone: false,
|
|
510
|
+
// How to merge when target or source has enable property and boolean value into {enable:boolean}
|
|
511
|
+
// 当target或source有enable属性(prop)和boolean值时,如何合并为{enable:boolean}
|
|
512
|
+
useEnable: true,
|
|
513
|
+
// Whether to allow merging key=symbol key-value pairs when target is {} type.
|
|
514
|
+
// target是{}类型时,是否允许合并key=symbol的键值对。
|
|
515
|
+
useSymbol: true,
|
|
516
|
+
// Options passed to the deepClone function when targetClone is true
|
|
517
|
+
// 当targetClone为true时传递给deepClone函数的选项
|
|
518
|
+
deepClone: {},
|
|
519
|
+
// Callback function executed before merging each data structure
|
|
520
|
+
// 在每个数据结构合并前执行的回调函数
|
|
521
|
+
onBeforeMerge: undefined,
|
|
522
|
+
// Callback function executed after merging each data structure
|
|
523
|
+
// 在每个数据结构合并后执行的回调函数
|
|
524
|
+
onAfterMerge: undefined,
|
|
525
|
+
}, opts),
|
|
526
|
+
// Main helper function for recursive merging
|
|
527
|
+
// 递归合并的主辅助函数
|
|
528
|
+
deepMergeHelper = (target, source, options) => {
|
|
529
|
+
let targetType = getDataType(target), sourceType = getDataType(source), flag = true, type, result;
|
|
530
|
+
// Determine the type and perform appropriate merging
|
|
531
|
+
// 确定类型并执行相应的合并
|
|
532
|
+
if (targetType === 'Object' && sourceType === 'Object') {
|
|
533
|
+
result = deepMergeObjects(target, source, options);
|
|
534
|
+
type = 'Object';
|
|
535
|
+
}
|
|
536
|
+
else if (targetType === 'Array' && sourceType === 'Array') {
|
|
537
|
+
result = deepMergeArrays(target, source, options);
|
|
538
|
+
type = 'Array';
|
|
539
|
+
}
|
|
540
|
+
else if (targetType === 'Set' && sourceType === 'Set') {
|
|
541
|
+
result = deepMergeSets(target, source, options);
|
|
542
|
+
type = 'Set';
|
|
543
|
+
}
|
|
544
|
+
else if (targetType === 'Map' && sourceType === 'Map') {
|
|
545
|
+
result = deepMergeMaps(target, source, options);
|
|
546
|
+
type = 'Map';
|
|
547
|
+
}
|
|
548
|
+
else {
|
|
549
|
+
flag = false;
|
|
550
|
+
result = target;
|
|
551
|
+
}
|
|
552
|
+
return { result, flag, type };
|
|
553
|
+
},
|
|
554
|
+
// Special handling for objects with enable property
|
|
555
|
+
// 对具有enable属性的对象进行特殊处理
|
|
556
|
+
mergeEnableObject = (target, source) => {
|
|
557
|
+
if (target?.hasOwnProperty('enable') && typeof source === 'boolean') {
|
|
558
|
+
//result={enable:true,a:'',b:''},source[k]=false=>result={enable:false,a:'',b:''}
|
|
559
|
+
//仅设置enable属性
|
|
560
|
+
target.enable = source;
|
|
561
|
+
//返回target原对象
|
|
562
|
+
return target;
|
|
563
|
+
}
|
|
564
|
+
else if (source?.hasOwnProperty('enable') && typeof target === 'boolean') {
|
|
565
|
+
//source={enable:true,a:'',b:''},(result as any)[k]=false=>result={enable:false,a:'',b:''}
|
|
566
|
+
//返回新值,新值包含enable属性
|
|
567
|
+
return Object.assign({ enable: target }, source);
|
|
568
|
+
}
|
|
569
|
+
return source;
|
|
570
|
+
},
|
|
571
|
+
// Deep merge for objects
|
|
572
|
+
// 对象的深度合并
|
|
573
|
+
deepMergeObjects = (target, source, opts = {}) => {
|
|
574
|
+
let targetType = getDataType(target), sourceType = getDataType(source);
|
|
575
|
+
// If target is not an object or source is empty, return target directly
|
|
576
|
+
// 如果target不是对象或者source为空则直接返回
|
|
577
|
+
if (targetType !== 'Object' || sourceType !== 'Object') {
|
|
578
|
+
return target;
|
|
579
|
+
}
|
|
580
|
+
opts?.onBeforeMerge?.(target, source);
|
|
581
|
+
const options = Object.assign({ inheritMissing: true, targetClone: false, useEnable: true }, opts);
|
|
582
|
+
let result = {};
|
|
583
|
+
// If cloning is enabled, clone the target first
|
|
584
|
+
// 如果是复制方法,则先复制target
|
|
585
|
+
result = options.targetClone ? shallowCopy(target) : target;
|
|
586
|
+
for (let k in source) {
|
|
587
|
+
if (source.hasOwnProperty(k) && result.hasOwnProperty(k)) {
|
|
588
|
+
let resp = deepMergeHelper(result[k], source[k], opts);
|
|
589
|
+
//resp={result,flag,type}
|
|
590
|
+
//flag=true表示类型一致并完成了合并,false表示并没有合并需要直接赋值
|
|
591
|
+
if (!resp.flag) {
|
|
592
|
+
//类型不同则直接覆盖
|
|
593
|
+
if (options.useEnable) {
|
|
594
|
+
result[k] = mergeEnableObject(result[k], source[k]);
|
|
452
595
|
}
|
|
453
596
|
else {
|
|
454
597
|
//完全替换
|
|
@@ -456,99 +599,89 @@ const deepMergeObjects = (target, source, opts = {}) => {
|
|
|
456
599
|
}
|
|
457
600
|
}
|
|
458
601
|
else {
|
|
459
|
-
|
|
460
|
-
|
|
602
|
+
//类型相同
|
|
603
|
+
if (resp.type) {
|
|
604
|
+
if (resp.type === 'Object') {
|
|
605
|
+
//如果遇上对象则深度复制
|
|
606
|
+
result[k] = resp.result;
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
else {
|
|
610
|
+
//其他类型则直接覆盖
|
|
611
|
+
result[k] = source[k];
|
|
612
|
+
}
|
|
461
613
|
}
|
|
462
614
|
}
|
|
463
|
-
else {
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
result[k] = resp.result;
|
|
467
|
-
}
|
|
615
|
+
else if (source.hasOwnProperty(k) && !result.hasOwnProperty(k) && options.inheritMissing) {
|
|
616
|
+
//如果source有属性,result没有该属性,但是options允许追加属性则直接赋值
|
|
617
|
+
result[k] = source[k];
|
|
468
618
|
}
|
|
469
619
|
}
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
if (options.useSymbol) {
|
|
477
|
-
let symbols = Object.getOwnPropertySymbols(source);
|
|
478
|
-
if (symbols.length > 0) {
|
|
479
|
-
for (let k of symbols) {
|
|
480
|
-
result[k] = source[k];
|
|
620
|
+
//Symbol键直接追加,因为Symbol是唯一,结果同Object.assign
|
|
621
|
+
if (options.useSymbol) {
|
|
622
|
+
let symbols = Object.getOwnPropertySymbols(source);
|
|
623
|
+
if (symbols.length) {
|
|
624
|
+
for (let k of symbols)
|
|
625
|
+
result[k] = source[k];
|
|
481
626
|
}
|
|
482
627
|
}
|
|
483
|
-
|
|
484
|
-
return result;
|
|
485
|
-
};
|
|
486
|
-
|
|
487
|
-
const deepMergeArrays = (target, source, options = { itemMode: 'merge', propAppend: true, targetClone: false, useEnable: true }) => {
|
|
488
|
-
// Ensure both target and source are arrays
|
|
489
|
-
if (!Array.isArray(target) || !Array.isArray(source))
|
|
490
|
-
return target;
|
|
491
|
-
// Merge options, with default values
|
|
492
|
-
const opts = Object.assign({ itemMode: 'merge', propAppend: true, targetClone: false }, options),
|
|
493
|
-
// If cloning is enabled, create a deep copy of the target array
|
|
494
|
-
result = opts.targetClone ? [...target] : target;
|
|
495
|
-
// Handle different merge strategies based on itemMode
|
|
496
|
-
if (opts.itemMode === 'replace') {
|
|
497
|
-
// Replace mode: clear the target array and push all items from the source array
|
|
498
|
-
result.length = 0;
|
|
499
|
-
result.push(...source);
|
|
628
|
+
options?.onAfterMerge?.(result, target, source);
|
|
500
629
|
return result;
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
//
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
//
|
|
513
|
-
|
|
514
|
-
|
|
630
|
+
}, deepMergeArrays = (target, source, options = {}) => {
|
|
631
|
+
// Ensure both target and source are arrays
|
|
632
|
+
if (!Array.isArray(target) || !Array.isArray(source))
|
|
633
|
+
return target;
|
|
634
|
+
options?.onBeforeMerge?.(target, source);
|
|
635
|
+
// Merge options, with default values
|
|
636
|
+
const opts = Object.assign({ dataMode: 'clear', inheritMissing: true, targetClone: false }, options),
|
|
637
|
+
// If cloning is enabled, create a deep copy of the target array
|
|
638
|
+
result = opts.targetClone ? [...target] : target;
|
|
639
|
+
// Handle different merge strategies based on dataMode
|
|
640
|
+
if (opts.dataMode === 'replace') {
|
|
641
|
+
// "replace" mode: recursively merge items in the arrays
|
|
642
|
+
for (let i = 0; i < source.length; i++) {
|
|
643
|
+
// If not allowed to add beyond length
|
|
644
|
+
// 如果不允许添加超过长度
|
|
645
|
+
if (!opts.inheritMissing && i >= result.length)
|
|
646
|
+
break;
|
|
647
|
+
let resp = deepMergeHelper(result[i], source[i], opts);
|
|
648
|
+
//resp={result,flag,type}
|
|
649
|
+
//flag=true表示类型一致并完成了合并,false表示并没有合并需要直接赋值
|
|
650
|
+
if (!resp.flag) {
|
|
651
|
+
result[i] = source[i];
|
|
652
|
+
}
|
|
515
653
|
}
|
|
516
654
|
}
|
|
655
|
+
else if (opts.dataMode === 'concat') {
|
|
656
|
+
// Concatenate mode: append all items from the source array to the target array
|
|
657
|
+
result.push(...source);
|
|
658
|
+
}
|
|
659
|
+
else {
|
|
660
|
+
//Default replace mode: clear the target array and push all items from the source array
|
|
661
|
+
result.length = 0;
|
|
662
|
+
result.push(...source);
|
|
663
|
+
}
|
|
664
|
+
options?.onAfterMerge?.(result, target, source);
|
|
517
665
|
return result;
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
// If cloning is enabled, create a deep copy of the target Map
|
|
528
|
-
result = opts.targetClone ? new Map(target) : target;
|
|
529
|
-
// Handle different merge strategies based on itemMode
|
|
530
|
-
if (opts.itemMode === 'replace') {
|
|
531
|
-
// Replace mode: clear the target Map and add all entries from the source Map
|
|
532
|
-
result.clear();
|
|
533
|
-
source.forEach((value, key) => result.set(key, value));
|
|
534
|
-
return result;
|
|
535
|
-
}
|
|
536
|
-
else if (opts.itemMode === 'concat') {
|
|
537
|
-
// Concatenate mode: add all entries from the source Map to the target Map
|
|
538
|
-
source.forEach((value, key) => result.set(key, value));
|
|
539
|
-
return result;
|
|
540
|
-
}
|
|
541
|
-
else {
|
|
666
|
+
}, deepMergeMaps = (target, source, options = {}) => {
|
|
667
|
+
// Ensure both target and source are Maps
|
|
668
|
+
if (!(target instanceof Map) || !(source instanceof Map))
|
|
669
|
+
return target;
|
|
670
|
+
options?.onBeforeMerge?.(target, source);
|
|
671
|
+
// Merge options, with default values
|
|
672
|
+
const opts = Object.assign({ inheritMissing: true, targetClone: false, useEnable: true }, options),
|
|
673
|
+
// If cloning is enabled, create a deep copy of the target Map
|
|
674
|
+
result = opts.targetClone ? new Map([...target]) : target;
|
|
542
675
|
// Default "merge" mode: recursively merge entries in the Maps
|
|
543
|
-
|
|
676
|
+
for (const [key, value] of source.entries())
|
|
544
677
|
// Check if the key already exists in the target Map
|
|
545
678
|
if (result.has(key)) {
|
|
546
|
-
const
|
|
679
|
+
const _target = result.get(key), _source = value, resp = deepMergeHelper(_target, _source, opts);
|
|
547
680
|
//resp={result,flag,type}
|
|
548
681
|
//flag=true表示类型一致并完成了合并,false表示并没有合并需要直接赋值
|
|
549
682
|
if (!resp.flag) {
|
|
550
683
|
// For simple values, overwrite the target value with the source value
|
|
551
|
-
result.set(key,
|
|
684
|
+
result.set(key, _source);
|
|
552
685
|
}
|
|
553
686
|
else {
|
|
554
687
|
// If both target and source are objects, merge them recursively
|
|
@@ -557,116 +690,42 @@ const deepMergeMaps = (target, source, options = { itemMode: 'merge', propAppend
|
|
|
557
690
|
}
|
|
558
691
|
else {
|
|
559
692
|
// If the key doesn't exist in the target, add the entry from the source Map
|
|
560
|
-
result.set(key, value);
|
|
693
|
+
options.inheritMissing && result.set(key, value);
|
|
561
694
|
}
|
|
562
|
-
|
|
695
|
+
options?.onAfterMerge?.(result, target, source);
|
|
563
696
|
return result;
|
|
564
|
-
}
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
697
|
+
}, deepMergeSets = (target, source, options = {}) => {
|
|
698
|
+
// Ensure both target and source are Sets
|
|
699
|
+
if (!(target instanceof Set) || !(source instanceof Set))
|
|
700
|
+
return target;
|
|
701
|
+
options?.onBeforeMerge?.(target, source);
|
|
702
|
+
// Merge options, with default values
|
|
703
|
+
const opts = Object.assign({ dataMode: 'clear', inheritMissing: true, targetClone: false, useEnable: true }, options),
|
|
704
|
+
// If cloning is enabled, create a deep copy of the target Set
|
|
705
|
+
result = opts.targetClone ? new Set(...target) : target;
|
|
706
|
+
// Handle different merge strategies based on dataMode
|
|
707
|
+
if (opts.dataMode === 'replace') {
|
|
708
|
+
// Replace mode: recursively merge items in the Sets
|
|
709
|
+
const _result = [...result], _source = [...source], resp = deepMergeHelper(_result, _source, opts);
|
|
710
|
+
result.clear();
|
|
711
|
+
for (let item of resp.result)
|
|
712
|
+
result.add(item);
|
|
578
713
|
}
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
const keysA = Object.keys(a), keysB = Object.keys(b);
|
|
584
|
-
if (keysA.length !== keysB.length)
|
|
585
|
-
return false;
|
|
586
|
-
for (let key of keysA) {
|
|
587
|
-
if (!keysB.includes(key) || !deepEqual(a[key], b[key]))
|
|
588
|
-
return false;
|
|
714
|
+
else if (opts.dataMode === 'concat') {
|
|
715
|
+
// Concatenate mode: add all items from the source Set to the target Set
|
|
716
|
+
for (let item of source)
|
|
717
|
+
result.add(item);
|
|
589
718
|
}
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
const deepMergeSets = (target, source, options = { itemMode: 'merge', propAppend: true, targetClone: false, useEnable: true }) => {
|
|
597
|
-
// Ensure both target and source are Sets
|
|
598
|
-
if (!(target instanceof Set) || !(source instanceof Set))
|
|
599
|
-
return target;
|
|
600
|
-
// Merge options, with default values
|
|
601
|
-
const opts = Object.assign({ itemMode: 'merge', propAppend: true, targetClone: false, useEnable: true }, options),
|
|
602
|
-
// If cloning is enabled, create a deep copy of the target Set
|
|
603
|
-
result = opts.targetClone ? new Set(target) : target;
|
|
604
|
-
// Handle different merge strategies based on itemMode
|
|
605
|
-
if (opts.itemMode === 'replace') {
|
|
606
|
-
// Replace mode: clear the target Set and add all items from the source Set
|
|
607
|
-
result.clear();
|
|
608
|
-
for (let item of source)
|
|
609
|
-
result.add(item);
|
|
610
|
-
return result;
|
|
611
|
-
}
|
|
612
|
-
else if (opts.itemMode === 'concat') {
|
|
613
|
-
// Concatenate mode: add all items from the source Set to the target Set
|
|
614
|
-
for (let item of source)
|
|
615
|
-
result.add(item);
|
|
616
|
-
return result;
|
|
617
|
-
}
|
|
618
|
-
else {
|
|
619
|
-
// Default "merge" mode: recursively merge items in the Sets
|
|
620
|
-
for (let item of source) {
|
|
621
|
-
// Check the type of the target and source items
|
|
622
|
-
let _target = [...result].find(val => deepEqual(val, item)), resp = deepMergeHelper(_target, item, opts);
|
|
623
|
-
//resp={result,flag}
|
|
624
|
-
//flag=true表示类型一致并完成了合并,false表示并没有合并需要直接赋值
|
|
625
|
-
!resp.flag && result.add(item);
|
|
719
|
+
else {
|
|
720
|
+
//Default "clear" mode: clear the target Set and add all items from the source Set
|
|
721
|
+
result.clear();
|
|
722
|
+
for (let item of source)
|
|
723
|
+
result.add(item);
|
|
626
724
|
}
|
|
725
|
+
options?.onAfterMerge?.(result, target, source);
|
|
627
726
|
return result;
|
|
628
|
-
}
|
|
629
|
-
};
|
|
630
|
-
|
|
631
|
-
// deepMergeHelper.ts
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
const deepMergeHelper = (target, source, options) => {
|
|
635
|
-
let targetType = getDataType(target), sourceType = getDataType(source), flag = true, type, result;
|
|
636
|
-
if (targetType === 'Object' && sourceType === 'Object') {
|
|
637
|
-
result = deepMergeObjects(target, source, options);
|
|
638
|
-
type = 'Object';
|
|
639
|
-
}
|
|
640
|
-
else if (targetType === 'Array' && sourceType === 'Array') {
|
|
641
|
-
result = deepMergeArrays(target, source, options);
|
|
642
|
-
type = 'Array';
|
|
643
|
-
}
|
|
644
|
-
else if (targetType === 'Set' && sourceType === 'Set') {
|
|
645
|
-
result = deepMergeSets(target, source, options);
|
|
646
|
-
type = 'Set';
|
|
647
|
-
}
|
|
648
|
-
else if (targetType === 'Map' && sourceType === 'Map') {
|
|
649
|
-
result = deepMergeMaps(target, source, options);
|
|
650
|
-
type = 'Map';
|
|
651
|
-
}
|
|
652
|
-
else {
|
|
653
|
-
flag = false;
|
|
654
|
-
result = target; // Default case, replace primitive values
|
|
655
|
-
}
|
|
656
|
-
return {
|
|
657
|
-
result, flag, type
|
|
658
727
|
};
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
const deepMerge = (target, source, opts = {}) => {
|
|
662
|
-
// Get the data types of the target and source
|
|
663
|
-
let options = Object.assign({
|
|
664
|
-
itemMode: 'merge', // Default merge mode
|
|
665
|
-
propAppend: true, // Default to appending properties from source to target
|
|
666
|
-
targetClone: false, // Do not clone target by default
|
|
667
|
-
useEnable: true // Enable special handling for objects with an `enable` property
|
|
668
|
-
}, opts);
|
|
669
|
-
return deepMergeHelper(target, source, options);
|
|
728
|
+
return deepMergeHelper(target, source, options).result;
|
|
670
729
|
};
|
|
671
730
|
|
|
672
731
|
const utils = {
|
|
@@ -684,12 +743,9 @@ const utils = {
|
|
|
684
743
|
wrapSetMethods,
|
|
685
744
|
wrapMapMethods,
|
|
686
745
|
getUniqueId,
|
|
687
|
-
deepEqual: assert.deepEqual,
|
|
688
746
|
deepMerge,
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
deepMergeObjects,
|
|
692
|
-
deepMergeSets,
|
|
747
|
+
shallowCopy,
|
|
748
|
+
copyObjectWithSymbol,
|
|
693
749
|
};
|
|
694
750
|
|
|
695
751
|
module.exports = utils;
|