deepie-merge 1.1.0 → 1.2.1

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 CHANGED
@@ -28,7 +28,7 @@ deepMerge({a: [1], b: [1]}, {a: [2], b: [2]}, {arrayExtend: ["a"]});
28
28
  - `dst` *any*: Destination value
29
29
  - `src` *any*: Source value
30
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
31
+ - `arrayExtend` *boolean* or *string[]*: Whether to extend arrays instead of replacing them. When passed a string array, it will only extend the object keys provided in that array.
32
32
  - `maxRecursions` *number*: Amount of nesting levels to recurse into. Default: `10`
33
33
 
34
34
  © [silverwind](https://github.com/silverwind), distributed under BSD licence
@@ -0,0 +1,9 @@
1
+ type ArrayExtend = boolean | string[];
2
+ type DeepieMergeOpts = {
3
+ /** Either a boolean or a array of property keys to allow extension. */
4
+ arrayExtend: ArrayExtend;
5
+ /** Maximum recursions to perform. Default: 10. */
6
+ maxRecursion?: number;
7
+ };
8
+ export declare function deepMerge(a: any, b: any, { arrayExtend, maxRecursion }?: DeepieMergeOpts): any;
9
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,40 @@
1
+ const isObject = (obj) => Object.prototype.toString.call(obj) === "[object Object]";
2
+ const uniq = (arr) => Array.from(new Set(arr));
3
+ const extendArrays = (a, b) => uniq([...a, ...b]);
4
+ function getType(obj) {
5
+ if (isObject(obj))
6
+ return "object";
7
+ if (Array.isArray(obj))
8
+ return "array";
9
+ return typeof obj;
10
+ }
11
+ function canExtendArray(key, arrayExtend) {
12
+ return Array.isArray(arrayExtend) ? arrayExtend.includes(key) : arrayExtend;
13
+ }
14
+ function deepMerge(a, b, { arrayExtend = false, maxRecursion = 10 } = { arrayExtend: false, maxRecursion: 10 }) {
15
+ if (Array.isArray(a) && Array.isArray(b))
16
+ return arrayExtend ? extendArrays(a, b) : b;
17
+ if (!isObject(b))
18
+ return b;
19
+ if (maxRecursion === 0)
20
+ return a;
21
+ for (const key of Object.keys(b)) {
22
+ const typeA = getType(a[key]);
23
+ const typeB = getType(b[key]);
24
+ if (typeA !== typeB) {
25
+ a[key] = b[key];
26
+ } else {
27
+ if (typeA === "array" && canExtendArray(key, arrayExtend)) {
28
+ a[key] = extendArrays(a[key], b[key]);
29
+ } else if (typeA === "object") {
30
+ a[key] = deepMerge(a[key], b[key], { arrayExtend, maxRecursion: maxRecursion - 1 });
31
+ } else {
32
+ a[key] = b[key];
33
+ }
34
+ }
35
+ }
36
+ return a;
37
+ }
38
+ export {
39
+ deepMerge
40
+ };
package/package.json CHANGED
@@ -1,25 +1,33 @@
1
1
  {
2
2
  "name": "deepie-merge",
3
- "version": "1.1.0",
3
+ "version": "1.2.1",
4
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",
8
- "exports": "./index.js",
9
8
  "type": "module",
10
9
  "sideEffects": false,
10
+ "main": "./dist/index.js",
11
+ "exports": "./dist/index.js",
12
+ "types": "./dist/index.d.ts",
13
+ "files": [
14
+ "dist"
15
+ ],
11
16
  "engines": {
12
17
  "node": ">=18"
13
18
  },
14
- "files": [
15
- "index.js"
16
- ],
17
19
  "devDependencies": {
20
+ "@types/node": "20.12.8",
18
21
  "eslint": "8.57.0",
19
- "eslint-config-silverwind": "82.0.3",
20
- "updates": "15.3.1",
22
+ "eslint-config-silverwind": "84.0.0",
23
+ "eslint-config-silverwind-typescript": "3.2.3",
24
+ "typescript": "5.4.5",
25
+ "typescript-config-silverwind": "4.0.1",
26
+ "updates": "16.0.1",
21
27
  "versions": "12.0.1",
22
- "vitest": "1.3.1",
23
- "vitest-config-silverwind": "5.1.2"
28
+ "vite": "5.2.11",
29
+ "vite-plugin-dts": "3.9.0",
30
+ "vitest": "1.5.3",
31
+ "vitest-config-silverwind": "9.0.4"
24
32
  }
25
33
  }
package/index.js DELETED
@@ -1,38 +0,0 @@
1
- const isObject = obj => Object.prototype.toString.call(obj) === "[object Object]";
2
- const uniq = arr => Array.from(new Set(arr));
3
- const extendArrays = (a, b) => uniq([...a, ...b]);
4
-
5
- function getType(obj) {
6
- if (isObject(obj)) return "object";
7
- if (Array.isArray(obj)) return "array";
8
- return typeof obj;
9
- }
10
-
11
- function canExtendArray(key, arrayExtend) {
12
- return Array.isArray(arrayExtend) ? arrayExtend.includes(key) : arrayExtend;
13
- }
14
-
15
- // merge b int a, arrayExtend is either a boolean or a array of property keys to allow extension
16
- export function deepMerge(a, b, {arrayExtend = false, maxRecursion = 10} = {}) {
17
- if (Array.isArray(a) && Array.isArray(b)) return arrayExtend ? extendArrays(a, b) : b;
18
- if (!isObject(b)) return b;
19
- if (maxRecursion === 0) return a;
20
-
21
- for (const key of Object.keys(b)) {
22
- const typeA = getType(a[key]);
23
- const typeB = getType(b[key]);
24
- if (typeA !== typeB) { // different type, overwrite
25
- a[key] = b[key];
26
- } else { // same type
27
- if (typeA === "array" && canExtendArray(key, arrayExtend)) {
28
- a[key] = extendArrays(a[key], b[key]);
29
- } else if (typeA === "object") {
30
- a[key] = deepMerge(a[key], b[key], {arrayExtend, maxRecursion: maxRecursion - 1});
31
- } else {
32
- a[key] = b[key];
33
- }
34
- }
35
- }
36
-
37
- return a;
38
- }