@codady/utils 0.0.9 → 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.
Files changed (48) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/dist/utils.cjs.js +488 -28
  3. package/dist/utils.cjs.min.js +3 -3
  4. package/dist/utils.esm.js +488 -28
  5. package/dist/utils.esm.min.js +3 -3
  6. package/dist/utils.umd - /345/211/257/346/234/254.js" +749 -0
  7. package/dist/utils.umd.js +490 -32
  8. package/dist/utils.umd.min.js +3 -3
  9. package/dist.zip +0 -0
  10. package/modules.js +24 -4
  11. package/modules.ts +24 -4
  12. package/package.json +1 -1
  13. package/src/arrayMutableMethods - /345/211/257/346/234/254.js" +5 -0
  14. package/src/arrayMutableMethods.js +5 -0
  15. package/src/{mutableMethods.ts → arrayMutableMethods.ts} +3 -3
  16. package/src/deepClone.js +151 -26
  17. package/src/deepClone.ts +194 -35
  18. package/src/deepCloneToJSON - /345/211/257/346/234/254.js" +47 -0
  19. package/src/deepEqual.js +48 -0
  20. package/src/deepEqual.ts +46 -0
  21. package/src/deepMerge.js +34 -0
  22. package/src/deepMerge.ts +40 -0
  23. package/src/deepMergeArrays.js +45 -0
  24. package/src/deepMergeArrays.ts +62 -0
  25. package/src/deepMergeHelper.js +40 -0
  26. package/src/deepMergeHelper.ts +45 -0
  27. package/src/deepMergeMaps - /345/211/257/346/234/254.js" +78 -0
  28. package/src/deepMergeMaps.js +57 -0
  29. package/src/deepMergeMaps.ts +67 -0
  30. package/src/deepMergeObjects.js +82 -0
  31. package/src/deepMergeObjects.ts +85 -0
  32. package/src/deepMergeSets.js +48 -0
  33. package/src/deepMergeSets.ts +55 -0
  34. package/src/getUniqueId.js +11 -7
  35. package/src/getUniqueId.ts +16 -9
  36. package/src/mapMutableMethods.js +5 -0
  37. package/src/mapMutableMethods.ts +15 -0
  38. package/src/mutableMethods.js +2 -2
  39. package/src/setMutableMethods - /345/211/257/346/234/254.js" +5 -0
  40. package/src/setMutableMethods.js +5 -0
  41. package/src/setMutableMethods.ts +14 -0
  42. package/src/wrapArrayMethods.js +5 -5
  43. package/src/wrapArrayMethods.ts +7 -7
  44. package/src/wrapMap - /345/211/257/346/234/254.js" +119 -0
  45. package/src/wrapMapMethods.js +118 -0
  46. package/src/wrapMapMethods.ts +226 -0
  47. package/src/wrapSetMethods.js +112 -0
  48. package/src/wrapSetMethods.ts +215 -0
@@ -0,0 +1,48 @@
1
+ /**
2
+ * @since Last modified: 2025/12/24 17:11:15
3
+ * @function deepEqual
4
+ * @description Compares two values for deep equality. This function checks whether the values are deeply equal, meaning that if the values are objects or arrays, their properties or items are recursively compared. If they are primitive types, a direct comparison is performed.
5
+ * It supports comparison for arrays, objects, and primitive types (like numbers, strings, booleans, etc.). The comparison is done by value, not by reference.
6
+ *
7
+ * @param a The first value to compare.
8
+ * @param b The second value to compare.
9
+ * @returns {boolean} `true` if the values are deeply equal, otherwise `false`.
10
+ *
11
+ * @example
12
+ * deepEqual([1, 2, 3], [1, 2, 3]); // returns true
13
+ * deepEqual([1, 2, 3], [3, 2, 1]); // returns false
14
+ * deepEqual({ a: 1, b: 2 }, { a: 1, b: 2 }); // returns true
15
+ * deepEqual({ a: 1, b: 2 }, { a: 1, b: 3 }); // returns false
16
+ * deepEqual(5, 5); // returns true
17
+ * deepEqual('hello', 'hello'); // returns true
18
+ * deepEqual(5, '5'); // returns false
19
+ */
20
+ const deepEqual = (a, b) => {
21
+ // If both are equal by reference
22
+ if (a === b)
23
+ return true;
24
+ // If both are arrays, check equality recursively
25
+ if (Array.isArray(a) && Array.isArray(b)) {
26
+ if (a.length !== b.length)
27
+ return false;
28
+ for (let i = 0; i < a.length; i++) {
29
+ if (!deepEqual(a[i], b[i]))
30
+ return false;
31
+ }
32
+ return true;
33
+ }
34
+ // If both are objects, check equality recursively
35
+ if (typeof a === 'object' && typeof b === 'object') {
36
+ const keysA = Object.keys(a), keysB = Object.keys(b);
37
+ if (keysA.length !== keysB.length)
38
+ return false;
39
+ for (let key of keysA) {
40
+ if (!keysB.includes(key) || !deepEqual(a[key], b[key]))
41
+ return false;
42
+ }
43
+ return true;
44
+ }
45
+ // For other types, direct comparison
46
+ return a === b;
47
+ };
48
+ export default deepEqual;
@@ -0,0 +1,46 @@
1
+ /**
2
+ * @since Last modified: 2025/12/24 17:11:15
3
+ * @function deepEqual
4
+ * @description Compares two values for deep equality. This function checks whether the values are deeply equal, meaning that if the values are objects or arrays, their properties or items are recursively compared. If they are primitive types, a direct comparison is performed.
5
+ * It supports comparison for arrays, objects, and primitive types (like numbers, strings, booleans, etc.). The comparison is done by value, not by reference.
6
+ *
7
+ * @param a The first value to compare.
8
+ * @param b The second value to compare.
9
+ * @returns {boolean} `true` if the values are deeply equal, otherwise `false`.
10
+ *
11
+ * @example
12
+ * deepEqual([1, 2, 3], [1, 2, 3]); // returns true
13
+ * deepEqual([1, 2, 3], [3, 2, 1]); // returns false
14
+ * deepEqual({ a: 1, b: 2 }, { a: 1, b: 2 }); // returns true
15
+ * deepEqual({ a: 1, b: 2 }, { a: 1, b: 3 }); // returns false
16
+ * deepEqual(5, 5); // returns true
17
+ * deepEqual('hello', 'hello'); // returns true
18
+ * deepEqual(5, '5'); // returns false
19
+ */
20
+ const deepEqual = (a: any, b: any): boolean => {
21
+ // If both are equal by reference
22
+ if (a === b) return true;
23
+
24
+ // If both are arrays, check equality recursively
25
+ if (Array.isArray(a) && Array.isArray(b)) {
26
+ if (a.length !== b.length) return false;
27
+ for (let i = 0; i < a.length; i++) {
28
+ if (!deepEqual(a[i], b[i])) return false;
29
+ }
30
+ return true;
31
+ }
32
+
33
+ // If both are objects, check equality recursively
34
+ if (typeof a === 'object' && typeof b === 'object') {
35
+ const keysA = Object.keys(a), keysB = Object.keys(b);
36
+ if (keysA.length !== keysB.length) return false;
37
+ for (let key of keysA) {
38
+ if (!keysB.includes(key) || !deepEqual(a[key], b[key])) return false;
39
+ }
40
+ return true;
41
+ }
42
+
43
+ // For other types, direct comparison
44
+ return a === b;
45
+ }
46
+ export default deepEqual;
@@ -0,0 +1,34 @@
1
+ /**
2
+ * @since Last modified: 2025/12/24 17:21:45
3
+ * @function deepMerge
4
+ * @description Deeply merges two data structures (Object, Array, Map, or Set) based on their types.
5
+ * This function recursively merges the properties or items of the target and source, depending on their types.
6
+ * If the types of target and source do not match, the target will be returned unchanged.
7
+ * @param target The target data structure to merge into (can be Object, Array, Map, or Set).
8
+ * @param source The source data structure to merge from (can be Object, Array, Map, or Set).
9
+ * @param opts Configuration options for merging. The default options include:
10
+ * - `itemMode`: Specifies the merging mode (`'merge'`, `'replace'`, or `'concat'`).
11
+ * - `propAppend`: Whether to append properties from the source if they do not exist in the target.
12
+ * - `targetClone`: Whether to clone the target before merging (if true, the target is not modified).
13
+ * - `useEnable`: Some special handling for specific objects that have `enable` properties.
14
+ * - `useSymbol`: Whether to merge symbol properties.
15
+ * @returns The deeply merged target data structure.
16
+ * @example
17
+ * let x = { a: 'man', b: 0, c: [] };
18
+ * let y = { a: 'woman', b: 2, d: 'new' };
19
+ * const merged = deepMerge(x, y, { itemMode: 'merge', propAppend: true });
20
+ * // Returns { a: 'woman', b: 2, c: [], d: 'new' }
21
+ */
22
+ 'use strict';
23
+ import deepMergeHelper from './deepMergeHelper';
24
+ const deepMerge = (target, source, opts = {}) => {
25
+ // Get the data types of the target and source
26
+ let options = Object.assign({
27
+ itemMode: 'merge', // Default merge mode
28
+ propAppend: true, // Default to appending properties from source to target
29
+ targetClone: false, // Do not clone target by default
30
+ useEnable: true // Enable special handling for objects with an `enable` property
31
+ }, opts);
32
+ return deepMergeHelper(target, source, options);
33
+ };
34
+ export default deepMerge;
@@ -0,0 +1,40 @@
1
+ /**
2
+ * @since Last modified: 2025/12/24 17:21:45
3
+ * @function deepMerge
4
+ * @description Deeply merges two data structures (Object, Array, Map, or Set) based on their types.
5
+ * This function recursively merges the properties or items of the target and source, depending on their types.
6
+ * If the types of target and source do not match, the target will be returned unchanged.
7
+ * @param target The target data structure to merge into (can be Object, Array, Map, or Set).
8
+ * @param source The source data structure to merge from (can be Object, Array, Map, or Set).
9
+ * @param opts Configuration options for merging. The default options include:
10
+ * - `itemMode`: Specifies the merging mode (`'merge'`, `'replace'`, or `'concat'`).
11
+ * - `propAppend`: Whether to append properties from the source if they do not exist in the target.
12
+ * - `targetClone`: Whether to clone the target before merging (if true, the target is not modified).
13
+ * - `useEnable`: Some special handling for specific objects that have `enable` properties.
14
+ * - `useSymbol`: Whether to merge symbol properties.
15
+ * @returns The deeply merged target data structure.
16
+ * @example
17
+ * let x = { a: 'man', b: 0, c: [] };
18
+ * let y = { a: 'woman', b: 2, d: 'new' };
19
+ * const merged = deepMerge(x, y, { itemMode: 'merge', propAppend: true });
20
+ * // Returns { a: 'woman', b: 2, c: [], d: 'new' }
21
+ */
22
+ 'use strict';
23
+ import getDataType from './getDataType';
24
+ import deepMergeObjects from './deepMergeObjects';
25
+ import deepMergeArrays, { DeepMergeOptions } from './deepMergeArrays';
26
+ import deepMergeMaps from './deepMergeMaps';
27
+ import deepMergeSets from './deepMergeSets';
28
+ import deepMergeHelper from './deepMergeHelper';
29
+ const deepMerge = (target: Object | Array<any> | Map<any, any> | Set<any>, source: any, opts: DeepMergeOptions = {}): Object | Array<any> | Map<any, any> | Set<any> => {
30
+ // Get the data types of the target and source
31
+ let options = Object.assign({
32
+ itemMode: 'merge', // Default merge mode
33
+ propAppend: true, // Default to appending properties from source to target
34
+ targetClone: false, // Do not clone target by default
35
+ useEnable: true // Enable special handling for objects with an `enable` property
36
+ }, opts);
37
+
38
+ return deepMergeHelper(target,source,options);
39
+ }
40
+ export default deepMerge;
@@ -0,0 +1,45 @@
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
+ 'use strict';
11
+ import deepMergeHelper from './deepMergeHelper';
12
+ const deepMergeArrays = (target, source, options = { itemMode: 'merge', propAppend: true, targetClone: false, useEnable: true }) => {
13
+ // Ensure both target and source are arrays
14
+ if (!Array.isArray(target) || !Array.isArray(source))
15
+ return target;
16
+ // Merge options, with default values
17
+ const opts = Object.assign({ itemMode: 'merge', propAppend: true, targetClone: false }, options),
18
+ // If cloning is enabled, create a deep copy of the target array
19
+ result = opts.targetClone ? [...target] : target;
20
+ // Handle different merge strategies based on itemMode
21
+ if (opts.itemMode === 'replace') {
22
+ // Replace mode: clear the target array and push all items from the source array
23
+ result.length = 0;
24
+ result.push(...source);
25
+ return result;
26
+ }
27
+ else if (opts.itemMode === 'concat') {
28
+ // Concatenate mode: append all items from the source array to the target array
29
+ result.push(...source);
30
+ return result;
31
+ }
32
+ else {
33
+ // Default "merge" mode: recursively merge items in the arrays
34
+ for (let i = 0; i < source.length; i++) {
35
+ let resp = deepMergeHelper(result[i], source[i], opts);
36
+ //resp={result,flag,type}
37
+ //flag=true表示类型一致并完成了合并,false表示并没有合并需要直接赋值
38
+ if (!resp.flag) {
39
+ result[i] = source[i];
40
+ }
41
+ }
42
+ return result;
43
+ }
44
+ };
45
+ export default deepMergeArrays;
@@ -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;