@codady/utils 0.0.11 → 0.0.12

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/modules.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Last modified: 2025/12/24 17:05:40
2
+ * Last modified: 2025/12/25 12:08:43
3
3
  */
4
4
  'use strict';
5
5
  import deepClone from './src/deepClone';
@@ -13,12 +13,8 @@ import setMutableMethods from './src/setMutableMethods';
13
13
  import mapMutableMethods from './src/mapMutableMethods';
14
14
  import wrapSetMethods from './src/wrapSetMethods';
15
15
  import wrapMapMethods from './src/wrapMapMethods';
16
- import { deepEqual } from 'assert';
17
16
  import deepMerge from './src/deepMerge';
18
- import deepMergeArrays from './src/deepMergeArrays';
19
- import deepMergeMaps from './src/deepMergeMaps';
20
- import deepMergeObjects from './src/deepMergeObjects';
21
- import deepMergeSets from './src/deepMergeSets';
17
+ import shallowCopy from './src/shallowCopy';
22
18
  const utils = {
23
19
  //executeStr,
24
20
  getDataType,
@@ -34,11 +30,7 @@ const utils = {
34
30
  wrapSetMethods,
35
31
  wrapMapMethods,
36
32
  getUniqueId,
37
- deepEqual,
38
33
  deepMerge,
39
- deepMergeArrays,
40
- deepMergeMaps,
41
- deepMergeObjects,
42
- deepMergeSets,
34
+ shallowCopy,
43
35
  };
44
36
  export default utils;
package/modules.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Last modified: 2025/12/24 17:05:40
2
+ * Last modified: 2025/12/25 12:08:43
3
3
  */
4
4
  'use strict'
5
5
  import deepClone from './src/deepClone';
@@ -16,12 +16,13 @@ import setMutableMethods from './src/setMutableMethods';
16
16
  import mapMutableMethods from './src/mapMutableMethods';
17
17
  import wrapSetMethods from './src/wrapSetMethods';
18
18
  import wrapMapMethods from './src/wrapMapMethods';
19
- import { deepEqual } from 'assert';
20
19
  import deepMerge from './src/deepMerge';
21
20
  import deepMergeArrays from './src/deepMergeArrays';
22
21
  import deepMergeMaps from './src/deepMergeMaps';
23
22
  import deepMergeObjects from './src/deepMergeObjects';
24
23
  import deepMergeSets from './src/deepMergeSets';
24
+ import deepEqual from './src/deepEqual';
25
+ import shallowCopy from './src/shallowCopy';
25
26
 
26
27
 
27
28
 
@@ -40,12 +41,9 @@ const utils = {
40
41
  wrapSetMethods,
41
42
  wrapMapMethods,
42
43
  getUniqueId,
43
- deepEqual,
44
44
  deepMerge,
45
- deepMergeArrays,
46
- deepMergeMaps,
47
- deepMergeObjects,
48
- deepMergeSets,
45
+ shallowCopy,
46
+
49
47
  };
50
48
 
51
49
  export default utils;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codady/utils",
3
- "version": "0.0.11",
3
+ "version": "0.0.12",
4
4
  "author": "AXUI Development Team",
5
5
  "license": "MIT",
6
6
  "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.",
package/src/deepEqual.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @since Last modified: 2025/12/24 17:11:15
2
+ * @since Last modified: 2025/12/24 22:03:47
3
3
  * @function deepEqual
4
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
5
  * It supports comparison for arrays, objects, and primitive types (like numbers, strings, booleans, etc.). The comparison is done by value, not by reference.
package/src/deepEqual.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @since Last modified: 2025/12/24 17:11:15
2
+ * @since Last modified: 2025/12/24 22:03:47
3
3
  * @function deepEqual
4
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
5
  * It supports comparison for arrays, objects, and primitive types (like numbers, strings, booleans, etc.). The comparison is done by value, not by reference.
package/src/deepMerge.js CHANGED
@@ -1,34 +1,274 @@
1
1
  /**
2
- * @since Last modified: 2025/12/24 17:21:45
2
+ * @since Last modified: 2025/12/25 12:10:30
3
3
  * @function deepMerge
4
4
  * @description Deeply merges two data structures (Object, Array, Map, or Set) based on their types.
5
5
  * This function recursively merges the properties or items of the target and source, depending on their types.
6
6
  * If the types of target and source do not match, the target will be returned unchanged.
7
+ * 深度合并两个数据结构(对象、数组、Map或Set),根据类型递归合并属性和项。
8
+ * 如果目标和源的类型不匹配,则目标保持不变。
7
9
  * @param target The target data structure to merge into (can be Object, Array, Map, or Set).
10
+ * 要合并到的目标数据结构(可以是对象、数组、Map或Set)。
8
11
  * @param source The source data structure to merge from (can be Object, Array, Map, or Set).
12
+ * 要合并的源数据结构(可以是对象、数组、Map或Set)。
9
13
  * @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.
14
+ * 合并的配置选项,默认值包括:
15
+ * - `dataMode`: Specifies the merging mode (`'clear'`, `'replace'`, or `'concat'`).
16
+ * 指定合并模式('clear'、'replace' 或 'concat')。
17
+ * - `inheritMissing`: Whether to append properties from the source if they do not exist in the target.
18
+ * 如果源中的属性在目标中不存在,是否追加这些属性。
12
19
  * - `targetClone`: Whether to clone the target before merging (if true, the target is not modified).
20
+ * 是否在合并前克隆目标对象(如果为true,则目标对象不会被修改)。
13
21
  * - `useEnable`: Some special handling for specific objects that have `enable` properties.
22
+ * 对具有enable属性的特定对象进行特殊处理。
14
23
  * - `useSymbol`: Whether to merge symbol properties.
24
+ * 是否合并symbol属性。
15
25
  * @returns The deeply merged target data structure.
26
+ * 深度合并后的目标数据结构。
16
27
  * @example
17
28
  * let x = { a: 'man', b: 0, c: [] };
18
29
  * let y = { a: 'woman', b: 2, d: 'new' };
19
- * const merged = deepMerge(x, y, { itemMode: 'merge', propAppend: true });
30
+ * const merged = deepMerge(x, y, { dataMode: 'clear', inheritMissing: true });
20
31
  * // Returns { a: 'woman', b: 2, c: [], d: 'new' }
32
+ * // 返回 { a: 'woman', b: 2, c: [], d: 'new' }
21
33
  */
22
34
  'use strict';
23
- import deepMergeHelper from './deepMergeHelper';
35
+ import getDataType from './getDataType';
36
+ import shallowCopy from './shallowCopy';
24
37
  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);
38
+ // Set default options
39
+ // 设置默认选项
40
+ const options = Object.assign({
41
+ // Merging mode for array and set collections:
42
+ // 'clear' means clear first then append; 'concat' means append directly at the end;
43
+ // 'replace' means merge sequentially (deep merging)
44
+ // 对于array和set合集的合并方式:'clear'表示先清空再追加;'concat'表示直接在尾部追加;
45
+ // 'replace'表示按顺序进行合并(深度合并)
46
+ dataMode: 'clear',
47
+ // Whether to directly append properties/items from source that target doesn't have.
48
+ // true: append; false: discard
49
+ // source对象如果有target对象没有的item或prop属性,是否直接新追加。true则追加;false则丢弃
50
+ inheritMissing: true,
51
+ // Whether to clone the target object, generating a brand new object. Default false.
52
+ // 是否克隆target对象,生成全新对象,默认false。
53
+ targetClone: false,
54
+ // How to merge when target or source has enable property and boolean value into {enable:boolean}
55
+ // 当target或source有enable属性(prop)和boolean值时,如何合并为{enable:boolean}
56
+ useEnable: true,
57
+ // Whether to allow merging key=symbol key-value pairs when target is {} type.
58
+ // target是{}类型时,是否允许合并key=symbol的键值对。
59
+ useSymbol: true,
60
+ // Options passed to the deepClone function when targetClone is true
61
+ // 当targetClone为true时传递给deepClone函数的选项
62
+ deepClone: {},
63
+ // Callback function executed before merging each data structure
64
+ // 在每个数据结构合并前执行的回调函数
65
+ onBeforeMerge: undefined,
66
+ // Callback function executed after merging each data structure
67
+ // 在每个数据结构合并后执行的回调函数
68
+ onAfterMerge: undefined,
69
+ }, opts),
70
+ // Main helper function for recursive merging
71
+ // 递归合并的主辅助函数
72
+ deepMergeHelper = (target, source, options) => {
73
+ let targetType = getDataType(target), sourceType = getDataType(source), flag = true, type, result;
74
+ // Determine the type and perform appropriate merging
75
+ // 确定类型并执行相应的合并
76
+ if (targetType === 'Object' && sourceType === 'Object') {
77
+ result = deepMergeObjects(target, source, options);
78
+ type = 'Object';
79
+ }
80
+ else if (targetType === 'Array' && sourceType === 'Array') {
81
+ result = deepMergeArrays(target, source, options);
82
+ type = 'Array';
83
+ }
84
+ else if (targetType === 'Set' && sourceType === 'Set') {
85
+ result = deepMergeSets(target, source, options);
86
+ type = 'Set';
87
+ }
88
+ else if (targetType === 'Map' && sourceType === 'Map') {
89
+ result = deepMergeMaps(target, source, options);
90
+ type = 'Map';
91
+ }
92
+ else {
93
+ flag = false;
94
+ result = target;
95
+ }
96
+ return { result, flag, type };
97
+ },
98
+ // Special handling for objects with enable property
99
+ // 对具有enable属性的对象进行特殊处理
100
+ mergeEnableObject = (target, source) => {
101
+ if (target?.hasOwnProperty('enable') && typeof source === 'boolean') {
102
+ //result={enable:true,a:'',b:''},source[k]=false=>result={enable:false,a:'',b:''}
103
+ //仅设置enable属性
104
+ target.enable = source;
105
+ //返回target原对象
106
+ return target;
107
+ }
108
+ else if (source?.hasOwnProperty('enable') && typeof target === 'boolean') {
109
+ //source={enable:true,a:'',b:''},(result as any)[k]=false=>result={enable:false,a:'',b:''}
110
+ //返回新值,新值包含enable属性
111
+ return Object.assign({ enable: target }, source);
112
+ }
113
+ return source;
114
+ },
115
+ // Deep merge for objects
116
+ // 对象的深度合并
117
+ deepMergeObjects = (target, source, opts = {}) => {
118
+ let targetType = getDataType(target), sourceType = getDataType(source);
119
+ // If target is not an object or source is empty, return target directly
120
+ // 如果target不是对象或者source为空则直接返回
121
+ if (targetType !== 'Object' || sourceType !== 'Object') {
122
+ return target;
123
+ }
124
+ opts?.onBeforeMerge?.(target, source);
125
+ const options = Object.assign({ inheritMissing: true, targetClone: false, useEnable: true }, opts);
126
+ let result = {};
127
+ // If cloning is enabled, clone the target first
128
+ // 如果是复制方法,则先复制target
129
+ result = options.targetClone ? shallowCopy(target) : target;
130
+ for (let k in source) {
131
+ if (source.hasOwnProperty(k) && result.hasOwnProperty(k)) {
132
+ let resp = deepMergeHelper(result[k], source[k], opts);
133
+ //resp={result,flag,type}
134
+ //flag=true表示类型一致并完成了合并,false表示并没有合并需要直接赋值
135
+ if (!resp.flag) {
136
+ //类型不同则直接覆盖
137
+ if (options.useEnable) {
138
+ result[k] = mergeEnableObject(result[k], source[k]);
139
+ }
140
+ else {
141
+ //完全替换
142
+ result[k] = source[k];
143
+ }
144
+ }
145
+ else {
146
+ //类型相同
147
+ if (resp.type) {
148
+ if (resp.type === 'Object') {
149
+ //如果遇上对象则深度复制
150
+ result[k] = resp.result;
151
+ }
152
+ }
153
+ else {
154
+ //其他类型则直接覆盖
155
+ result[k] = source[k];
156
+ }
157
+ }
158
+ }
159
+ else if (source.hasOwnProperty(k) && !result.hasOwnProperty(k) && options.inheritMissing) {
160
+ //如果source有属性,result没有该属性,但是options允许追加属性则直接赋值
161
+ result[k] = source[k];
162
+ }
163
+ }
164
+ //Symbol键直接追加,因为Symbol是唯一,结果同Object.assign
165
+ if (options.useSymbol) {
166
+ let symbols = Object.getOwnPropertySymbols(source);
167
+ if (symbols.length) {
168
+ for (let k of symbols)
169
+ result[k] = source[k];
170
+ }
171
+ }
172
+ options?.onAfterMerge?.(result, target, source);
173
+ return result;
174
+ }, deepMergeArrays = (target, source, options = {}) => {
175
+ // Ensure both target and source are arrays
176
+ if (!Array.isArray(target) || !Array.isArray(source))
177
+ return target;
178
+ options?.onBeforeMerge?.(target, source);
179
+ // Merge options, with default values
180
+ const opts = Object.assign({ dataMode: 'clear', inheritMissing: true, targetClone: false }, options),
181
+ // If cloning is enabled, create a deep copy of the target array
182
+ result = opts.targetClone ? [...target] : target;
183
+ // Handle different merge strategies based on dataMode
184
+ if (opts.dataMode === 'replace') {
185
+ // "replace" mode: recursively merge items in the arrays
186
+ for (let i = 0; i < source.length; i++) {
187
+ // If not allowed to add beyond length
188
+ // 如果不允许添加超过长度
189
+ if (!opts.inheritMissing && i >= result.length)
190
+ break;
191
+ let resp = deepMergeHelper(result[i], source[i], opts);
192
+ //resp={result,flag,type}
193
+ //flag=true表示类型一致并完成了合并,false表示并没有合并需要直接赋值
194
+ if (!resp.flag) {
195
+ result[i] = source[i];
196
+ }
197
+ }
198
+ }
199
+ else if (opts.dataMode === 'concat') {
200
+ // Concatenate mode: append all items from the source array to the target array
201
+ result.push(...source);
202
+ }
203
+ else {
204
+ //Default replace mode: clear the target array and push all items from the source array
205
+ result.length = 0;
206
+ result.push(...source);
207
+ }
208
+ options?.onAfterMerge?.(result, target, source);
209
+ return result;
210
+ }, deepMergeMaps = (target, source, options = {}) => {
211
+ // Ensure both target and source are Maps
212
+ if (!(target instanceof Map) || !(source instanceof Map))
213
+ return target;
214
+ options?.onBeforeMerge?.(target, source);
215
+ // Merge options, with default values
216
+ const opts = Object.assign({ inheritMissing: true, targetClone: false, useEnable: true }, options),
217
+ // If cloning is enabled, create a deep copy of the target Map
218
+ result = opts.targetClone ? new Map([...target]) : target;
219
+ // Default "merge" mode: recursively merge entries in the Maps
220
+ for (const [key, value] of source.entries())
221
+ // Check if the key already exists in the target Map
222
+ if (result.has(key)) {
223
+ const _target = result.get(key), _source = value, resp = deepMergeHelper(_target, _source, opts);
224
+ //resp={result,flag,type}
225
+ //flag=true表示类型一致并完成了合并,false表示并没有合并需要直接赋值
226
+ if (!resp.flag) {
227
+ // For simple values, overwrite the target value with the source value
228
+ result.set(key, _source);
229
+ }
230
+ else {
231
+ // If both target and source are objects, merge them recursively
232
+ resp.type === 'Object' && result.set(key, resp.result);
233
+ }
234
+ }
235
+ else {
236
+ // If the key doesn't exist in the target, add the entry from the source Map
237
+ options.inheritMissing && result.set(key, value);
238
+ }
239
+ options?.onAfterMerge?.(result, target, source);
240
+ return result;
241
+ }, deepMergeSets = (target, source, options = {}) => {
242
+ // Ensure both target and source are Sets
243
+ if (!(target instanceof Set) || !(source instanceof Set))
244
+ return target;
245
+ options?.onBeforeMerge?.(target, source);
246
+ // Merge options, with default values
247
+ const opts = Object.assign({ dataMode: 'clear', inheritMissing: true, targetClone: false, useEnable: true }, options),
248
+ // If cloning is enabled, create a deep copy of the target Set
249
+ result = opts.targetClone ? new Set(...target) : target;
250
+ // Handle different merge strategies based on dataMode
251
+ if (opts.dataMode === 'replace') {
252
+ // Replace mode: recursively merge items in the Sets
253
+ const _result = [...result], _source = [...source], resp = deepMergeHelper(_result, _source, opts);
254
+ result.clear();
255
+ for (let item of resp.result)
256
+ result.add(item);
257
+ }
258
+ else if (opts.dataMode === 'concat') {
259
+ // Concatenate mode: add all items from the source Set to the target Set
260
+ for (let item of source)
261
+ result.add(item);
262
+ }
263
+ else {
264
+ //Default "clear" mode: clear the target Set and add all items from the source Set
265
+ result.clear();
266
+ for (let item of source)
267
+ result.add(item);
268
+ }
269
+ options?.onAfterMerge?.(result, target, source);
270
+ return result;
271
+ };
272
+ return deepMergeHelper(target, source, options).result;
33
273
  };
34
274
  export default deepMerge;