@codady/utils 0.0.10 → 0.0.11

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.
@@ -0,0 +1,62 @@
1
+ /**
2
+ * @since Last modified: 2025/12/24 17:49:40
3
+ * @function deepMergeArrays
4
+ * Deeply merges two arrays, with flexible options to replace, concatenate, or merge items.
5
+ * @param target The target array to merge into
6
+ * @param source The source array to merge from
7
+ * @param options Configuration options for merging
8
+ * @returns The deeply merged array
9
+ */
10
+
11
+ 'use strict';
12
+ import getDataType from './getDataType';
13
+ import deepClone from './deepClone';
14
+ import deepMergeObjects from './deepMergeObjects';
15
+ import deepMergeSets from './deepMergeSets';
16
+ import deepMergeMaps from './deepMergeMaps';
17
+ import deepMergeHelper from './deepMergeHelper';
18
+
19
+ // Define options for deep merging
20
+ export interface DeepMergeOptions {
21
+ itemMode?: 'merge' | 'replace' | 'concat'; // Mode for handling array items
22
+ propAppend?: boolean; // Whether to append properties (for objects)
23
+ targetClone?: boolean; // Whether to clone the target array
24
+ useEnable?: boolean,//如果target={enable:true},source=false,那么将输出target={enable:false}
25
+ useSymbol?: boolean,//合并对象中是否处理key=symbol
26
+ }
27
+
28
+ const deepMergeArrays = (target: any[], source: any[], options: DeepMergeOptions = { itemMode: 'merge', propAppend: true, targetClone: false, useEnable: true }): any[] => {
29
+ // Ensure both target and source are arrays
30
+ if (!Array.isArray(target) || !Array.isArray(source)) return target;
31
+
32
+ // Merge options, with default values
33
+ const opts = Object.assign({ itemMode: 'merge', propAppend: true, targetClone: false }, options),
34
+
35
+ // If cloning is enabled, create a deep copy of the target array
36
+ result = opts.targetClone ? [...target] : target;
37
+
38
+ // Handle different merge strategies based on itemMode
39
+ if (opts.itemMode === 'replace') {
40
+ // Replace mode: clear the target array and push all items from the source array
41
+ result.length = 0;
42
+ result.push(...source);
43
+ return result;
44
+ } else if (opts.itemMode === 'concat') {
45
+ // Concatenate mode: append all items from the source array to the target array
46
+ result.push(...source);
47
+ return result;
48
+ } else {
49
+ // Default "merge" mode: recursively merge items in the arrays
50
+ for (let i = 0; i < source.length; i++) {
51
+ let resp = deepMergeHelper(result[i], source[i], opts);
52
+ //resp={result,flag,type}
53
+ //flag=true表示类型一致并完成了合并,false表示并没有合并需要直接赋值
54
+ if (!resp.flag) {
55
+ result[i] = source[i];
56
+ }
57
+ }
58
+ return result;
59
+ }
60
+ }
61
+
62
+ export default deepMergeArrays;
@@ -0,0 +1,40 @@
1
+ // deepMergeHelper.ts
2
+ /**
3
+ * @description A helper function for deeply merging different types of values (Object, Array, Map, Set).
4
+ * This function is used by other deep merge functions to handle recursive merging of different types.
5
+ */
6
+ import deepMergeObjects from './deepMergeObjects';
7
+ import deepMergeArrays from './deepMergeArrays';
8
+ import deepMergeMaps from './deepMergeMaps';
9
+ import deepMergeSets from './deepMergeSets';
10
+ import getDataType from './getDataType';
11
+ /**
12
+ * Deep merge values based on their types (Object, Array, Set, Map).
13
+ */
14
+ const deepMergeHelper = (target, source, options) => {
15
+ let targetType = getDataType(target), sourceType = getDataType(source), flag = true, type, result;
16
+ if (targetType === 'Object' && sourceType === 'Object') {
17
+ result = deepMergeObjects(target, source, options);
18
+ type = 'Object';
19
+ }
20
+ else if (targetType === 'Array' && sourceType === 'Array') {
21
+ result = deepMergeArrays(target, source, options);
22
+ type = 'Array';
23
+ }
24
+ else if (targetType === 'Set' && sourceType === 'Set') {
25
+ result = deepMergeSets(target, source, options);
26
+ type = 'Set';
27
+ }
28
+ else if (targetType === 'Map' && sourceType === 'Map') {
29
+ result = deepMergeMaps(target, source, options);
30
+ type = 'Map';
31
+ }
32
+ else {
33
+ flag = false;
34
+ result = target; // Default case, replace primitive values
35
+ }
36
+ return {
37
+ result, flag, type
38
+ };
39
+ };
40
+ export default deepMergeHelper;
@@ -0,0 +1,45 @@
1
+ // deepMergeHelper.ts
2
+ /**
3
+ * @description A helper function for deeply merging different types of values (Object, Array, Map, Set).
4
+ * This function is used by other deep merge functions to handle recursive merging of different types.
5
+ */
6
+
7
+ import deepMergeObjects from './deepMergeObjects';
8
+ import deepMergeArrays from './deepMergeArrays';
9
+ import deepMergeMaps from './deepMergeMaps';
10
+ import deepMergeSets from './deepMergeSets';
11
+ import { DeepMergeOptions } from './deepMergeArrays';
12
+ import getDataType from './getDataType';
13
+
14
+ /**
15
+ * Deep merge values based on their types (Object, Array, Set, Map).
16
+ */
17
+ const deepMergeHelper = (target: any, source: any, options: DeepMergeOptions): any => {
18
+ let targetType = getDataType(target),
19
+ sourceType = getDataType(source),
20
+ flag = true,
21
+ type,
22
+ result;
23
+
24
+ if (targetType === 'Object' && sourceType === 'Object') {
25
+ result = deepMergeObjects(target, source, options);
26
+ type = 'Object';
27
+ } else if (targetType === 'Array' && sourceType === 'Array') {
28
+ result = deepMergeArrays(target, source, options);
29
+ type = 'Array';
30
+ } else if (targetType === 'Set' && sourceType === 'Set') {
31
+ result = deepMergeSets(target, source, options);
32
+ type = 'Set';
33
+ } else if (targetType === 'Map' && sourceType === 'Map') {
34
+ result = deepMergeMaps(target, source, options);
35
+ type = 'Map';
36
+ } else {
37
+ flag = false;
38
+ result = target; // Default case, replace primitive values
39
+ }
40
+ return {
41
+ result, flag,type
42
+ };
43
+ }
44
+
45
+ export default deepMergeHelper;
@@ -0,0 +1,78 @@
1
+ /**
2
+ * @since Last modified: 2025/12/24 17:05:22
3
+ * @function deepMergeMaps
4
+ * Deeply merges two Maps, with flexible options to replace, concatenate, or merge entries.
5
+ * @param target The target Map to merge into
6
+ * @param source The source Map to merge from
7
+ * @param options Configuration options for merging
8
+ * @returns The deeply merged Map
9
+ */
10
+ 'use strict';
11
+ import getDataType from './getDataType';
12
+ import deepMergeObjects from './deepMergeObjects';
13
+ import deepMergeArrays from './deepMergeArrays';
14
+ import deepMergeSets from './deepMergeSets';
15
+ const deepMergeMaps = (target, source, options = { itemMode: 'merge', propAppend: true, targetClone: false, useEnable: true }) => {
16
+ // Ensure both target and source are Maps
17
+ if (!(target instanceof Map) || !(source instanceof Map))
18
+ return target;
19
+ // Merge options, with default values
20
+ const opts = Object.assign({ itemMode: 'merge', propAppend: true, targetClone: false, useEnable: true }, options),
21
+ // If cloning is enabled, create a deep copy of the target Map
22
+ result = opts.targetClone ? new Map(target) : target;
23
+ // Handle different merge strategies based on itemMode
24
+ if (opts.itemMode === 'replace') {
25
+ // Replace mode: clear the target Map and add all entries from the source Map
26
+ result.clear();
27
+ source.forEach((value, key) => result.set(key, value));
28
+ return result;
29
+ }
30
+ else if (opts.itemMode === 'concat') {
31
+ // Concatenate mode: add all entries from the source Map to the target Map
32
+ source.forEach((value, key) => result.set(key, value));
33
+ return result;
34
+ }
35
+ else {
36
+ // Default "merge" mode: recursively merge entries in the Maps
37
+ source.forEach((value, key) => {
38
+ // Check if the key already exists in the target Map
39
+ if (result.has(key)) {
40
+ const targetValue = result.get(key), sourceValue = value;
41
+ const targetType = getDataType(targetValue), sourceType = getDataType(sourceValue);
42
+ // If both target and source values are of the same type, merge them
43
+ if (targetType === sourceType) {
44
+ if (targetType === 'Object') {
45
+ // If both target and source are objects, merge them recursively
46
+ result.set(key, deepMergeObjects(targetValue, sourceValue, opts));
47
+ }
48
+ else if (targetType === 'Array') {
49
+ // If both target and source are arrays, merge them recursively
50
+ deepMergeArrays(targetValue, sourceValue, opts);
51
+ }
52
+ else if (targetType === 'Map') {
53
+ // If both target and source are Maps, merge them recursively
54
+ deepMergeMaps(targetValue, sourceValue, opts);
55
+ }
56
+ else if (targetType === 'Set') {
57
+ // If both target and source are Sets, merge them recursively
58
+ deepMergeSets(targetValue, sourceValue, opts);
59
+ }
60
+ else {
61
+ // For simple values, overwrite the target value with the source value
62
+ result.set(key, sourceValue);
63
+ }
64
+ }
65
+ else {
66
+ // If types don't match, overwrite the target value with the source value
67
+ result.set(key, sourceValue);
68
+ }
69
+ }
70
+ else {
71
+ // If the key doesn't exist in the target, add the entry from the source Map
72
+ result.set(key, value);
73
+ }
74
+ });
75
+ return result;
76
+ }
77
+ };
78
+ export default deepMergeMaps;
@@ -0,0 +1,57 @@
1
+ /**
2
+ * @since Last modified: 2025/12/24 17:48:20
3
+ * @function deepMergeMaps
4
+ * Deeply merges two Maps, with flexible options to replace, concatenate, or merge entries.
5
+ * @param target The target Map to merge into
6
+ * @param source The source Map to merge from
7
+ * @param options Configuration options for merging
8
+ * @returns The deeply merged Map
9
+ */
10
+ 'use strict';
11
+ import deepMergeHelper from './deepMergeHelper';
12
+ const deepMergeMaps = (target, source, options = { itemMode: 'merge', propAppend: true, targetClone: false, useEnable: true }) => {
13
+ // Ensure both target and source are Maps
14
+ if (!(target instanceof Map) || !(source instanceof Map))
15
+ return target;
16
+ // Merge options, with default values
17
+ const opts = Object.assign({ itemMode: 'merge', propAppend: true, targetClone: false, useEnable: true }, options),
18
+ // If cloning is enabled, create a deep copy of the target Map
19
+ result = opts.targetClone ? new Map(target) : target;
20
+ // Handle different merge strategies based on itemMode
21
+ if (opts.itemMode === 'replace') {
22
+ // Replace mode: clear the target Map and add all entries from the source Map
23
+ result.clear();
24
+ source.forEach((value, key) => result.set(key, value));
25
+ return result;
26
+ }
27
+ else if (opts.itemMode === 'concat') {
28
+ // Concatenate mode: add all entries from the source Map to the target Map
29
+ source.forEach((value, key) => result.set(key, value));
30
+ return result;
31
+ }
32
+ else {
33
+ // Default "merge" mode: recursively merge entries in the Maps
34
+ source.forEach((value, key) => {
35
+ // Check if the key already exists in the target Map
36
+ if (result.has(key)) {
37
+ const targetValue = result.get(key), sourceValue = value, resp = deepMergeHelper(targetValue, sourceValue, opts);
38
+ //resp={result,flag,type}
39
+ //flag=true表示类型一致并完成了合并,false表示并没有合并需要直接赋值
40
+ if (!resp.flag) {
41
+ // For simple values, overwrite the target value with the source value
42
+ result.set(key, sourceValue);
43
+ }
44
+ else {
45
+ // If both target and source are objects, merge them recursively
46
+ resp.type === 'Object' && result.set(key, resp.result);
47
+ }
48
+ }
49
+ else {
50
+ // If the key doesn't exist in the target, add the entry from the source Map
51
+ result.set(key, value);
52
+ }
53
+ });
54
+ return result;
55
+ }
56
+ };
57
+ export default deepMergeMaps;
@@ -0,0 +1,67 @@
1
+ /**
2
+ * @since Last modified: 2025/12/24 17:48:20
3
+ * @function deepMergeMaps
4
+ * Deeply merges two Maps, with flexible options to replace, concatenate, or merge entries.
5
+ * @param target The target Map to merge into
6
+ * @param source The source Map to merge from
7
+ * @param options Configuration options for merging
8
+ * @returns The deeply merged Map
9
+ */
10
+
11
+ 'use strict';
12
+
13
+ import getDataType from './getDataType';
14
+ import deepMergeObjects from './deepMergeObjects';
15
+ import deepMergeArrays, { DeepMergeOptions } from './deepMergeArrays';
16
+ import deepMergeSets from './deepMergeSets';
17
+ import deepEqual from './deepEqual';
18
+ import deepMergeHelper from './deepMergeHelper';
19
+
20
+ const deepMergeMaps = (target: Map<any, any>, source: Map<any, any>, options: DeepMergeOptions = { itemMode: 'merge', propAppend: true, targetClone: false, useEnable: true }): Map<any, any> => {
21
+ // Ensure both target and source are Maps
22
+ if (!(target instanceof Map) || !(source instanceof Map)) return target;
23
+
24
+ // Merge options, with default values
25
+ const opts = Object.assign({ itemMode: 'merge', propAppend: true, targetClone: false, useEnable: true }, options),
26
+
27
+ // If cloning is enabled, create a deep copy of the target Map
28
+ result = opts.targetClone ? new Map(target) : target;
29
+
30
+ // Handle different merge strategies based on itemMode
31
+ if (opts.itemMode === 'replace') {
32
+ // Replace mode: clear the target Map and add all entries from the source Map
33
+ result.clear();
34
+ source.forEach((value, key) => result.set(key, value));
35
+ return result;
36
+ } else if (opts.itemMode === 'concat') {
37
+ // Concatenate mode: add all entries from the source Map to the target Map
38
+ source.forEach((value, key) => result.set(key, value));
39
+ return result;
40
+ } else {
41
+ // Default "merge" mode: recursively merge entries in the Maps
42
+ source.forEach((value, key) => {
43
+ // Check if the key already exists in the target Map
44
+ if (result.has(key)) {
45
+ const targetValue = result.get(key),
46
+ sourceValue = value,
47
+ resp = deepMergeHelper(targetValue, sourceValue, opts);
48
+ //resp={result,flag,type}
49
+ //flag=true表示类型一致并完成了合并,false表示并没有合并需要直接赋值
50
+ if (!resp.flag) {
51
+ // For simple values, overwrite the target value with the source value
52
+ result.set(key, sourceValue);
53
+ } else {
54
+ // If both target and source are objects, merge them recursively
55
+ resp.type === 'Object' && result.set(key, resp.result);
56
+ }
57
+ } else {
58
+ // If the key doesn't exist in the target, add the entry from the source Map
59
+ result.set(key, value);
60
+ }
61
+ });
62
+
63
+ return result;
64
+ }
65
+ }
66
+
67
+ export default deepMergeMaps;
@@ -0,0 +1,82 @@
1
+ /**
2
+ * @since Last modified: 2025/12/24 17:46:42
3
+ * @function deepMergeObjects
4
+ * @description Merge two objects. The key name of Source is allowed to be the Symbol type, and the Symbol key name of the source will be added directly to the target.
5
+ * @param {Object} target - The original target of the merge.
6
+ * @param {Object} source - The object that will merge into the target.
7
+ * @param {DeepMergeOptions} [opts] - Parameters used for merging objects.
8
+ * @param {boolean} [opts.arrAppend=false] - If value is an array, whether to append or override.
9
+ * @param {boolean} [opts.propAppend=true] - Whether to append properties from source to target if not already present.
10
+ * @param {boolean} [opts.targetClone=false] - Whether to clone the target object, if true, the original target will not be changed.
11
+ * @param {boolean} [opts.useEnable=true] - Whether to merge enable property.
12
+ * @param {boolean} [opts.useSymbol=true] - Whether to merge symbol properties.
13
+ * @returns {Object} - The merged object.
14
+ * @example
15
+ * let x ={a:'man',b:0,c:[]}, b={a:'woman',b:2};
16
+ * deepMergeObjects(x, b);
17
+ * // Returns {a:'woman', b:2, c:[]}
18
+ */
19
+ 'use strict';
20
+ import getDataType from './getDataType';
21
+ import deepClone from './deepClone';
22
+ import deepMergeHelper from './deepMergeHelper';
23
+ const deepMergeObjects = (target, source, opts = {}) => {
24
+ let targetType = getDataType(target), sourceType = getDataType(source);
25
+ //target不是对象或者source为空则直接返回
26
+ if (targetType !== 'Object' || sourceType !== 'Object') {
27
+ return target;
28
+ }
29
+ const options = Object.assign({ itemMode: 'merge', propAppend: true, targetClone: false, useEnable: true }, opts),
30
+ //如果是复制方法,则先复制target
31
+ result = options.targetClone ? deepClone(target) : target;
32
+ for (let k in source) {
33
+ if (source.hasOwnProperty(k) && result.hasOwnProperty(k)) {
34
+ let resp = deepMergeHelper(result[k], source[k], opts);
35
+ //resp={result,flag,type}
36
+ //flag=true表示类型一致并完成了合并,false表示并没有合并需要直接赋值
37
+ if (!resp.flag) {
38
+ //类型不同则直接覆盖
39
+ if (options.useEnable && result.hasOwnProperty(k) && result[k]?.hasOwnProperty('enable') && typeof source[k] === 'boolean') {
40
+ //部分替换,仅针对result={enable:true/false,a:''},source=false/true这种情况和相反的情况,因为这种情况再笨框架比较多见
41
+ if (result[k]?.hasOwnProperty('enable') && typeof source[k] === 'boolean') {
42
+ //result={enable:true,a:'',b:''},source[k]=false=>result={enable:false,a:'',b:''}
43
+ result[k].enable = source[k];
44
+ }
45
+ else if (source[k]?.hasOwnProperty('enable') && typeof result[k] === 'boolean') {
46
+ //source={enable:true,a:'',b:''},(result as any)[k]=false=>result={enable:false,a:'',b:''}
47
+ result = Object.assign({ enable: result[k] }, source[k]);
48
+ }
49
+ else {
50
+ //完全替换
51
+ result[k] = source[k];
52
+ }
53
+ }
54
+ else {
55
+ //完全替换
56
+ result[k] = source[k];
57
+ }
58
+ }
59
+ else {
60
+ // If both target and source are objects, merge them recursively
61
+ if (resp.type === 'Object') {
62
+ result[k] = resp.result;
63
+ }
64
+ }
65
+ }
66
+ else if (source.hasOwnProperty(k) && !result.hasOwnProperty(k) && options.propAppend) {
67
+ //如果source有属性,result没有该属性,但是options允许追加属性则直接赋值
68
+ result[k] = source[k];
69
+ }
70
+ }
71
+ //Symbol键直接追加,因为Symbol是唯一,结果同Object.assign
72
+ if (options.useSymbol) {
73
+ let symbols = Object.getOwnPropertySymbols(source);
74
+ if (symbols.length > 0) {
75
+ for (let k of symbols) {
76
+ result[k] = source[k];
77
+ }
78
+ }
79
+ }
80
+ return result;
81
+ };
82
+ export default deepMergeObjects;
@@ -0,0 +1,85 @@
1
+ /**
2
+ * @since Last modified: 2025/12/24 17:46:42
3
+ * @function deepMergeObjects
4
+ * @description Merge two objects. The key name of Source is allowed to be the Symbol type, and the Symbol key name of the source will be added directly to the target.
5
+ * @param {Object} target - The original target of the merge.
6
+ * @param {Object} source - The object that will merge into the target.
7
+ * @param {DeepMergeOptions} [opts] - Parameters used for merging objects.
8
+ * @param {boolean} [opts.arrAppend=false] - If value is an array, whether to append or override.
9
+ * @param {boolean} [opts.propAppend=true] - Whether to append properties from source to target if not already present.
10
+ * @param {boolean} [opts.targetClone=false] - Whether to clone the target object, if true, the original target will not be changed.
11
+ * @param {boolean} [opts.useEnable=true] - Whether to merge enable property.
12
+ * @param {boolean} [opts.useSymbol=true] - Whether to merge symbol properties.
13
+ * @returns {Object} - The merged object.
14
+ * @example
15
+ * let x ={a:'man',b:0,c:[]}, b={a:'woman',b:2};
16
+ * deepMergeObjects(x, b);
17
+ * // Returns {a:'woman', b:2, c:[]}
18
+ */
19
+ 'use strict';
20
+ import getDataType from './getDataType';
21
+ import deepClone from './deepClone';
22
+ import deepMergeArrays, { DeepMergeOptions } from './deepMergeArrays';
23
+ import deepMergeSets from './deepMergeSets';
24
+ import deepMergeMaps from './deepMergeMaps';
25
+ import deepMergeHelper from './deepMergeHelper';
26
+ const deepMergeObjects = (target: Object, source: any, opts: DeepMergeOptions = {}): Object => {
27
+ let targetType = getDataType(target),
28
+ sourceType = getDataType(source);
29
+ //target不是对象或者source为空则直接返回
30
+ if (targetType !== 'Object' || sourceType !== 'Object') {
31
+ return target;
32
+ }
33
+
34
+ const options = Object.assign({ itemMode: 'merge', propAppend: true, targetClone: false, useEnable: true }, opts),
35
+ //如果是复制方法,则先复制target
36
+ result = options.targetClone ? deepClone(target) : target;
37
+
38
+ for (let k in source) {
39
+ if (source.hasOwnProperty(k) && result.hasOwnProperty(k)) {
40
+
41
+ let resp = deepMergeHelper((result as any)[k], source[k], opts);
42
+ //resp={result,flag,type}
43
+ //flag=true表示类型一致并完成了合并,false表示并没有合并需要直接赋值
44
+ if (!resp.flag) {
45
+ //类型不同则直接覆盖
46
+ if (options.useEnable && result.hasOwnProperty(k) && (result as any)[k]?.hasOwnProperty('enable') && typeof source[k] === 'boolean') {
47
+ //部分替换,仅针对result={enable:true/false,a:''},source=false/true这种情况和相反的情况,因为这种情况再笨框架比较多见
48
+ if ((result as any)[k]?.hasOwnProperty('enable') && typeof source[k] === 'boolean') {
49
+ //result={enable:true,a:'',b:''},source[k]=false=>result={enable:false,a:'',b:''}
50
+ (result as any)[k].enable = source[k];
51
+ } else if (source[k]?.hasOwnProperty('enable') && typeof (result as any)[k] === 'boolean') {
52
+ //source={enable:true,a:'',b:''},(result as any)[k]=false=>result={enable:false,a:'',b:''}
53
+ (result as any) = Object.assign({ enable: (result as any)[k] }, source[k]);
54
+ } else {
55
+ //完全替换
56
+ (result as any)[k] = source[k];
57
+ }
58
+ } else {
59
+ //完全替换
60
+ (result as any)[k] = source[k];
61
+ }
62
+ } else {
63
+ // If both target and source are objects, merge them recursively
64
+ if (resp.type === 'Object') {
65
+ (result as any)[k] = resp.result;
66
+ }
67
+ }
68
+
69
+ } else if (source.hasOwnProperty(k) && !result.hasOwnProperty(k) && options.propAppend) {
70
+ //如果source有属性,result没有该属性,但是options允许追加属性则直接赋值
71
+ (result as any)[k] = source[k];
72
+ }
73
+ }
74
+ //Symbol键直接追加,因为Symbol是唯一,结果同Object.assign
75
+ if (options.useSymbol) {
76
+ let symbols = Object.getOwnPropertySymbols(source);
77
+ if (symbols.length > 0) {
78
+ for (let k of symbols) {
79
+ (result as any)[k] = source[k];
80
+ }
81
+ }
82
+ }
83
+ return result;
84
+ }
85
+ export default deepMergeObjects;
@@ -0,0 +1,48 @@
1
+ /**
2
+ * @since Last modified: 2025/12/24 17:48:38
3
+ * @function deepMergeSets
4
+ * Deeply merges two Sets, with flexible options to replace, concatenate, or merge items.
5
+ * @param target The target Set to merge into
6
+ * @param source The source Set to merge from
7
+ * @param options Configuration options for merging
8
+ * @returns The deeply merged Set
9
+ */
10
+ 'use strict';
11
+ import deepEqual from './deepEqual';
12
+ import deepMergeHelper from './deepMergeHelper';
13
+ const deepMergeSets = (target, source, options = { itemMode: 'merge', propAppend: true, targetClone: false, useEnable: true }) => {
14
+ // Ensure both target and source are Sets
15
+ if (!(target instanceof Set) || !(source instanceof Set))
16
+ return target;
17
+ // Merge options, with default values
18
+ const opts = Object.assign({ itemMode: 'merge', propAppend: true, targetClone: false, useEnable: true }, options),
19
+ // If cloning is enabled, create a deep copy of the target Set
20
+ result = opts.targetClone ? new Set(target) : target;
21
+ // Handle different merge strategies based on itemMode
22
+ if (opts.itemMode === 'replace') {
23
+ // Replace mode: clear the target Set and add all items from the source Set
24
+ result.clear();
25
+ for (let item of source)
26
+ result.add(item);
27
+ return result;
28
+ }
29
+ else if (opts.itemMode === 'concat') {
30
+ // Concatenate mode: add all items from the source Set to the target Set
31
+ for (let item of source)
32
+ result.add(item);
33
+ return result;
34
+ }
35
+ else {
36
+ // Default "merge" mode: recursively merge items in the Sets
37
+ for (let item of source) {
38
+ // Check the type of the target and source items
39
+ let _target = [...result].find(val => deepEqual(val, item)), resp = deepMergeHelper(_target, item, opts);
40
+ //resp={result,flag}
41
+ //flag=true表示类型一致并完成了合并,false表示并没有合并需要直接赋值
42
+ !resp.flag && result.add(item);
43
+ }
44
+ ;
45
+ return result;
46
+ }
47
+ };
48
+ export default deepMergeSets;
@@ -0,0 +1,55 @@
1
+ /**
2
+ * @since Last modified: 2025/12/24 17:48:38
3
+ * @function deepMergeSets
4
+ * Deeply merges two Sets, with flexible options to replace, concatenate, or merge items.
5
+ * @param target The target Set to merge into
6
+ * @param source The source Set to merge from
7
+ * @param options Configuration options for merging
8
+ * @returns The deeply merged Set
9
+ */
10
+
11
+ 'use strict';
12
+
13
+ import getDataType from './getDataType';
14
+ import deepMergeObjects from './deepMergeObjects';
15
+ import deepMergeArrays, { DeepMergeOptions } from './deepMergeArrays';
16
+ import deepEqual from './deepEqual';
17
+ import deepMergeMaps from './deepMergeMaps';
18
+ import deepMergeHelper from './deepMergeHelper';
19
+
20
+
21
+ const deepMergeSets = (target: Set<any>, source: Set<any>, options: DeepMergeOptions = { itemMode: 'merge', propAppend: true, targetClone: false, useEnable: true }): Set<any> => {
22
+ // Ensure both target and source are Sets
23
+ if (!(target instanceof Set) || !(source instanceof Set)) return target;
24
+
25
+ // Merge options, with default values
26
+ const opts = Object.assign({ itemMode: 'merge', propAppend: true, targetClone: false, useEnable: true }, options),
27
+
28
+ // If cloning is enabled, create a deep copy of the target Set
29
+ result = opts.targetClone ? new Set(target) : target;
30
+
31
+ // Handle different merge strategies based on itemMode
32
+ if (opts.itemMode === 'replace') {
33
+ // Replace mode: clear the target Set and add all items from the source Set
34
+ result.clear();
35
+ for (let item of source) result.add(item);
36
+ return result;
37
+ } else if (opts.itemMode === 'concat') {
38
+ // Concatenate mode: add all items from the source Set to the target Set
39
+ for (let item of source) result.add(item);
40
+ return result;
41
+ } else {
42
+ // Default "merge" mode: recursively merge items in the Sets
43
+ for (let item of source) {
44
+ // Check the type of the target and source items
45
+ let _target = [...result].find(val => deepEqual(val, item)),
46
+ resp = deepMergeHelper(_target, item, opts);
47
+ //resp={result,flag}
48
+ //flag=true表示类型一致并完成了合并,false表示并没有合并需要直接赋值
49
+ !resp.flag && result.add(item);
50
+ };
51
+ return result;
52
+ }
53
+ }
54
+
55
+ export default deepMergeSets;