deepie-merge 1.3.7 → 2.0.0
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/dist/index.d.ts +13 -8
- package/dist/index.js +32 -35
- package/package.json +14 -12
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
|
+
//#region index.d.ts
|
|
1
2
|
type ArrayExtend = boolean | Array<string>;
|
|
2
|
-
type DeepieMergeOpts = {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
maxRecursion?: number;
|
|
3
|
+
type DeepieMergeOpts<T = any> = {
|
|
4
|
+
/** Either a boolean or a array of property keys to allow extension. Default: false */arrayExtend?: ArrayExtend; /** Maximum recursions to perform. Default: 10. */
|
|
5
|
+
maxRecursion?: number; /** Return a new value instead of mutating the first argument. Default: false */
|
|
6
|
+
clone?: boolean | ((value: T) => T);
|
|
7
7
|
};
|
|
8
8
|
type DeepMergeable = {
|
|
9
|
-
|
|
9
|
+
[key: string]: any;
|
|
10
10
|
} | Array<any>;
|
|
11
11
|
/** deep-merge b into a */
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
declare function deepMerge<T extends DeepMergeable>(a: T, b: any, {
|
|
13
|
+
arrayExtend,
|
|
14
|
+
maxRecursion,
|
|
15
|
+
clone
|
|
16
|
+
}?: DeepieMergeOpts<T>): T;
|
|
17
|
+
//#endregion
|
|
18
|
+
export { deepMerge };
|
package/dist/index.js
CHANGED
|
@@ -1,42 +1,39 @@
|
|
|
1
|
+
//#region index.ts
|
|
1
2
|
function isObject(obj) {
|
|
2
|
-
|
|
3
|
+
return Object.prototype.toString.call(obj) === "[object Object]";
|
|
3
4
|
}
|
|
4
5
|
function getType(obj) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
if (isObject(obj)) return "object";
|
|
7
|
+
if (Array.isArray(obj)) return "array";
|
|
8
|
+
return typeof obj;
|
|
8
9
|
}
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
/** deep-merge b into a */
|
|
11
|
+
function deepMerge(a, b, { arrayExtend = false, maxRecursion = 20, clone = false } = {
|
|
12
|
+
arrayExtend: false,
|
|
13
|
+
maxRecursion: 10
|
|
14
|
+
}) {
|
|
15
|
+
return merge(clone ? typeof clone === "function" ? clone(a) : structuredClone(a) : a, b, arrayExtend, maxRecursion);
|
|
11
16
|
}
|
|
12
17
|
function merge(a, b, arrayExtend, maxRecursion) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
} else if (typeA === "object") {
|
|
32
|
-
a[key] = merge(a[key], b[key], arrayExtend, maxRecursion - 1);
|
|
33
|
-
} else {
|
|
34
|
-
a[key] = b[key];
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
return a;
|
|
18
|
+
if (maxRecursion === 0) return a;
|
|
19
|
+
if (Array.isArray(a)) {
|
|
20
|
+
if (Array.isArray(b)) return arrayExtend ? Array.from(new Set([...a, ...b])) : b;
|
|
21
|
+
return b;
|
|
22
|
+
}
|
|
23
|
+
if (Array.isArray(b)) return b;
|
|
24
|
+
if (isObject(a) && isObject(b)) {
|
|
25
|
+
const keys = Object.keys(b);
|
|
26
|
+
for (let i = 0, len = keys.length; i < len; i++) {
|
|
27
|
+
const key = keys[i];
|
|
28
|
+
const typeA = getType(a[key]);
|
|
29
|
+
if (typeA !== getType(b[key])) a[key] = b[key];
|
|
30
|
+
else if (typeA === "array" && (Array.isArray(arrayExtend) ? arrayExtend.includes(key) : arrayExtend)) a[key] = Array.from(new Set([...a[key], ...b[key]]));
|
|
31
|
+
else if (typeA === "object") a[key] = merge(a[key], b[key], arrayExtend, maxRecursion - 1);
|
|
32
|
+
else a[key] = b[key];
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return a;
|
|
39
36
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
};
|
|
37
|
+
|
|
38
|
+
//#endregion
|
|
39
|
+
export { deepMerge };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "deepie-merge",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Yay, another deep merge",
|
|
5
5
|
"author": "silverwind <me@silverwind.io>",
|
|
6
6
|
"repository": "silverwind/deepie-merge",
|
|
@@ -14,20 +14,22 @@
|
|
|
14
14
|
"dist"
|
|
15
15
|
],
|
|
16
16
|
"engines": {
|
|
17
|
-
"node": ">=
|
|
17
|
+
"node": ">=22"
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
|
-
"@types/node": "25.
|
|
21
|
-
"
|
|
22
|
-
"eslint
|
|
20
|
+
"@types/node": "25.6.0",
|
|
21
|
+
"@typescript/native-preview": "7.0.0-dev.20260413.1",
|
|
22
|
+
"eslint": "10.2.0",
|
|
23
|
+
"eslint-config-silverwind": "131.0.4",
|
|
23
24
|
"jest-extended": "7.0.0",
|
|
25
|
+
"tsdown": "0.21.7",
|
|
26
|
+
"tsdown-config-silverwind": "3.0.0",
|
|
24
27
|
"typescript": "5.9.3",
|
|
25
|
-
"typescript-config-silverwind": "
|
|
26
|
-
"updates": "17.
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"vitest": "
|
|
31
|
-
"vitest-config-silverwind": "10.6.1"
|
|
28
|
+
"typescript-config-silverwind": "17.0.0",
|
|
29
|
+
"updates": "17.15.2",
|
|
30
|
+
"updates-config-silverwind": "3.0.0",
|
|
31
|
+
"versions": "15.0.0",
|
|
32
|
+
"vitest": "4.1.4",
|
|
33
|
+
"vitest-config-silverwind": "11.3.3"
|
|
32
34
|
}
|
|
33
35
|
}
|