deepie-merge 1.0.0 → 1.1.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/README.md +15 -2
- package/index.js +5 -8
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# deepie-merge
|
|
2
2
|
[](https://www.npmjs.org/package/deepie-merge) [](https://www.npmjs.org/package/deepie-merge) [](https://packagephobia.com/result?p=deepie-merge)
|
|
3
3
|
|
|
4
|
-
>
|
|
4
|
+
> Yay, another deep merge
|
|
5
5
|
|
|
6
6
|
## Usage
|
|
7
7
|
```console
|
|
@@ -12,10 +12,23 @@ npm i deepie-merge
|
|
|
12
12
|
import {deepMerge} from "deepie-merge";
|
|
13
13
|
|
|
14
14
|
deepMerge({a: [1]}, {a: [2]});
|
|
15
|
-
// => {a: [
|
|
15
|
+
// => {a: [2]}
|
|
16
16
|
|
|
17
17
|
deepMerge({a: [1]}, {a: [2]}, {arrayExtend: true});
|
|
18
18
|
// => {a: [1, 2]}
|
|
19
|
+
|
|
20
|
+
deepMerge({a: [1], b: [1]}, {a: [2], b: [2]}, {arrayExtend: ["a"]});
|
|
21
|
+
// => {a: [1, 2], b: [2]}
|
|
19
22
|
```
|
|
20
23
|
|
|
24
|
+
## API
|
|
25
|
+
|
|
26
|
+
### deepMerge(dst, src, options)
|
|
27
|
+
|
|
28
|
+
- `dst` *any*: Destination value
|
|
29
|
+
- `src` *any*: Source value
|
|
30
|
+
- `options` *object*:
|
|
31
|
+
- `arrayExtend` *boolean* or *string[]*: Whether to extend array instead of replacing them. When passed a string array, it will only extend the object keys provided in that array
|
|
32
|
+
- `maxRecursions` *number*: Amount of nesting levels to recurse into. Default: `10`
|
|
33
|
+
|
|
21
34
|
© [silverwind](https://github.com/silverwind), distributed under BSD licence
|
package/index.js
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
const isObject = obj => Object.prototype.toString.call(obj) === "[object Object]";
|
|
2
2
|
const uniq = arr => Array.from(new Set(arr));
|
|
3
|
+
const extendArrays = (a, b) => uniq([...a, ...b]);
|
|
3
4
|
|
|
4
|
-
function
|
|
5
|
-
return uniq([...a, ...b]);
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
function type(obj) {
|
|
5
|
+
function getType(obj) {
|
|
9
6
|
if (isObject(obj)) return "object";
|
|
10
7
|
if (Array.isArray(obj)) return "array";
|
|
11
8
|
return typeof obj;
|
|
@@ -16,14 +13,14 @@ function canExtendArray(key, arrayExtend) {
|
|
|
16
13
|
}
|
|
17
14
|
|
|
18
15
|
// merge b int a, arrayExtend is either a boolean or a array of property keys to allow extension
|
|
19
|
-
export function deepMerge(a, b, {
|
|
16
|
+
export function deepMerge(a, b, {arrayExtend = false, maxRecursion = 10} = {}) {
|
|
20
17
|
if (Array.isArray(a) && Array.isArray(b)) return arrayExtend ? extendArrays(a, b) : b;
|
|
21
18
|
if (!isObject(b)) return b;
|
|
22
19
|
if (maxRecursion === 0) return a;
|
|
23
20
|
|
|
24
21
|
for (const key of Object.keys(b)) {
|
|
25
|
-
const typeA =
|
|
26
|
-
const typeB =
|
|
22
|
+
const typeA = getType(a[key]);
|
|
23
|
+
const typeB = getType(b[key]);
|
|
27
24
|
if (typeA !== typeB) { // different type, overwrite
|
|
28
25
|
a[key] = b[key];
|
|
29
26
|
} else { // same type
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "deepie-merge",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "Yay, another deep merge",
|
|
5
5
|
"author": "silverwind <me@silverwind.io>",
|
|
6
6
|
"repository": "silverwind/deepie-merge",
|
|
7
7
|
"license": "BSD-2-Clause",
|
|
@@ -16,10 +16,10 @@
|
|
|
16
16
|
],
|
|
17
17
|
"devDependencies": {
|
|
18
18
|
"eslint": "8.57.0",
|
|
19
|
-
"eslint-config-silverwind": "
|
|
20
|
-
"updates": "15.1
|
|
19
|
+
"eslint-config-silverwind": "82.0.3",
|
|
20
|
+
"updates": "15.3.1",
|
|
21
21
|
"versions": "12.0.1",
|
|
22
22
|
"vitest": "1.3.1",
|
|
23
|
-
"vitest-config-silverwind": "5.1.
|
|
23
|
+
"vitest-config-silverwind": "5.1.2"
|
|
24
24
|
}
|
|
25
25
|
}
|