@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/CHANGELOG.md +22 -2
- package/dist/utils.cjs.js +231 -213
- package/dist/utils.cjs.min.js +3 -3
- package/dist/utils.esm.js +231 -213
- package/dist/utils.esm.min.js +3 -3
- package/dist/utils.umd.js +235 -215
- package/dist/utils.umd.min.js +3 -3
- package/dist.zip +0 -0
- package/examples/deepMerge.html +1049 -0
- package/modules.js +3 -11
- package/modules.ts +5 -7
- package/package.json +1 -1
- 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/mapMutableMethods - /345/211/257/346/234/254.js" +5 -0
- package/src/shallowCopy.js +46 -0
- package/src/shallowCopy.ts +55 -0
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since Last modified: 2025/12/25 12:07:26
|
|
3
|
+
* shallowCopy
|
|
4
|
+
*
|
|
5
|
+
* This function performs a shallow copy of various data structures:
|
|
6
|
+
* - Set
|
|
7
|
+
* - Map
|
|
8
|
+
* - Array
|
|
9
|
+
* - Plain Object (including properties with Symbol keys)
|
|
10
|
+
*
|
|
11
|
+
* It returns a new instance of the data structure, but the elements or properties of reference types (such as objects or arrays) will be copied by reference.
|
|
12
|
+
* Primitive types (e.g., number, string, boolean) will be returned directly.
|
|
13
|
+
*/
|
|
14
|
+
'use strict';
|
|
15
|
+
|
|
16
|
+
import getDataType from "./getDataType";
|
|
17
|
+
|
|
18
|
+
const shallowCopy = (data: any): any => {
|
|
19
|
+
const dataType = getDataType(data);
|
|
20
|
+
|
|
21
|
+
// Check if data is a Set
|
|
22
|
+
if (dataType === 'Set') {
|
|
23
|
+
// Shallow copy Set
|
|
24
|
+
return new Set([...data]);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Check if data is a Map
|
|
28
|
+
if (dataType === 'Map') {
|
|
29
|
+
// Shallow copy Map
|
|
30
|
+
return new Map([...data]);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Check if data is an Array
|
|
34
|
+
if (Array.isArray(data)) {
|
|
35
|
+
// Shallow copy Array
|
|
36
|
+
return [...data];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Check if data is a Plain Object (including Symbol keys)
|
|
40
|
+
if (dataType === 'object') {
|
|
41
|
+
const obj = data as { [key: string | symbol]: any }; // Ensure the object type includes string and symbol keys
|
|
42
|
+
const symbolProperties = Object.getOwnPropertySymbols(obj).reduce((acc: { [key: symbol]: any }, sym: symbol) => {
|
|
43
|
+
acc[sym] = obj[sym];
|
|
44
|
+
return acc;
|
|
45
|
+
}, {});
|
|
46
|
+
|
|
47
|
+
// Shallow copy the object and include the Symbol properties
|
|
48
|
+
return { ...obj, ...symbolProperties };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// For other types (such as numbers, strings, booleans, etc.), return the original value
|
|
52
|
+
return data;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export default shallowCopy;
|