@komed3/deepmerge 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/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2026 Paul Köhler
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Paul Köhler
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -36,7 +36,7 @@ Include the script in your HTML:
36
36
  ```html
37
37
  <script src="node_modules/@komed3/deepmerge/dist/index.umd.min.js"></script>
38
38
  <script>
39
- const { factory } = deepmerge;
39
+ const { factory } = deepmerge;
40
40
  </script>
41
41
  ```
42
42
 
@@ -183,6 +183,7 @@ Control how arrays are handled during a merge using the built-in `ArrayMode` enu
183
183
  - `Keep`: Retains the target array and ignores the source.
184
184
  - `Concat`: Appends source elements to the target array.
185
185
  - `Unique`: Combines both arrays and removes duplicates.
186
+ - `Reference`: Reference-based array merging (process objects by reference).
186
187
 
187
188
  ```ts
188
189
  import { factory, ArrayMode } from '@komed3/deepmerge';
@@ -196,5 +197,5 @@ console.log( target.ids ); // [ 1, 2, 3 ]
196
197
 
197
198
  ----
198
199
 
199
- Copyright (c) 2026 Paul Köhler (komed3). All rights reserved.
200
- Released under the MIT license. See LICENSE file in the project root for details.
200
+ Copyright (c) 2026 [Paul Köhler](https://komed3.de) (komed3). All rights reserved.
201
+ Released under the MIT license. See [LICENSE](./LICENSE) file in the project root for details.
@@ -158,12 +158,29 @@
158
158
  return (t, _) => t;
159
159
  case 'concat':
160
160
  return (t, s) => t.concat(s);
161
- case 'unique':
161
+ case 'reference':
162
162
  return (t, s) => {
163
163
  const set = new Set(t);
164
164
  for (let i = 0; i < s.length; i++) set.add(s[i]);
165
165
  return Array.from(set);
166
166
  };
167
+ case 'unique':
168
+ return (t, s) => {
169
+ const map = new Map(),
170
+ add = (array) => {
171
+ for (let i = 0; i < array.length; i++) {
172
+ const item = array[i];
173
+ map.set(
174
+ item && typeof item === 'object'
175
+ ? JSON.stringify(item)
176
+ : item,
177
+ item
178
+ );
179
+ }
180
+ };
181
+ (add(t), add(s));
182
+ return [...map.values()];
183
+ };
167
184
  }
168
185
  if (typeof mode === 'function') return mode;
169
186
  else throw new Error(`Invalid array merge mode: ${mode}`);
@@ -1 +1 @@
1
- !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).deepmerge={})}(this,function(e){"use strict";class t{cache;maxCacheSize;constructor(e={}){!1!==e.cache&&(this.cache=new Map),this.maxCacheSize=e.maxCacheSize??1e3}compile(e){if(!e)return Object.freeze({tokens:[]});if(this.cache){const t=this.cache.get(e);if(t)return t}const t=[];let r=0,n="";for(;r<e.length;){const s=e.charCodeAt(r);if(46!==s){if(91===s){n&&(t.push(n),n=""),r++;const s=r;for(;r<e.length&&93!==e.charCodeAt(r);)r++;const i=e.slice(s,r),c=i>>>0;String(c)===i?t.push(c):t.push(i),r++;continue}n+=e[r],r++}else n&&(t.push(n),n=""),r++}n&&t.push(n);const s=Object.freeze({tokens:t});return this.cache&&(this.cache.size>=this.maxCacheSize&&this.cache.clear(),this.cache.set(e,s)),s}isCompiled(e){return e&&Array.isArray(e.tokens)}normalize(e){return"string"==typeof e?this.compile(e):e}clearCache(){this.cache?.clear()}get size(){return this.cache?.size??0}}class r{path;constructor(e={}){this.path=new t(e)}isUnsafeKey(e){return"__proto__"===e||"constructor"===e||"prototype"===e}walk(e,t,r){const n=this.path.normalize(t).tokens;let s=e;for(let e=0;e<n.length-1;e++){const t=n[e];if(this.isUnsafeKey(t))return;let i=s?.[t];if(null==i){if(!r)return;i="number"==typeof n[e+1]?[]:{},s[t]=i}s=i}const i=n[n.length-1];return{parent:s,key:i,value:s?.[i]}}has(e,t){const r=this.path.normalize(t).tokens;let n=e;for(let e=0;e<r.length;e++){if(null==n)return!1;n=n[r[e]]}return!0}get(e,t){const r=this.path.normalize(t).tokens;let n=e;for(let e=0;e<r.length;e++){if(null==n)return;n=n[r[e]]}return n}set(e,t,r){const n=this.walk(e,t,!0);n&&(n.parent[n.key]=r)}delete(e,t){const r=this.walk(e,t,!1);r&&null!=r.parent&&delete r.parent[r.key]}update(e,t,r){const n=this.walk(e,t,!0);n&&(n.parent[n.key]=r(n.value))}}class n{protect;deep;mergeUndefined;strict;createObject;valueFn;arrayFn;path;constructor(e={}){this.protect=!!e.protect,this.deep=!1!==e.deep,this.mergeUndefined=!!e.mergeUndefined,this.strict=!!e.strict,this.createObject=e.createObject??(()=>Object.create(null)),this.valueFn=e.valueFn,this.arrayFn=this.compileArrayFn(e.arrayMode),this.path=new t(e.pathOptions)}compileArrayFn(e){switch(e??"replace"){case"replace":return(e,t)=>t;case"keep":return(e,t)=>e;case"concat":return(e,t)=>e.concat(t);case"unique":return(e,t)=>{const r=new Set(e);for(let e=0;e<t.length;e++)r.add(t[e]);return Array.from(r)}}if("function"==typeof e)return e;throw new Error(`Invalid array merge mode: ${e}`)}isUnsafeKey(e){return"__proto__"===e||"constructor"===e||"prototype"===e}mergeInto(e,t){if(null==t)return;const r=[[e,t]];for(;r.length;){const[e,t]=r.pop();for(const n in t){if(this.isUnsafeKey(n))continue;const s=t[n],i=e[n];if((void 0!==s||this.mergeUndefined)&&(!this.protect||!(n in e))){if(this.valueFn){const t=this.valueFn(n,i,s);if(void 0!==t){e[n]=t;continue}}if(Array.isArray(s))Array.isArray(i)?e[n]=this.arrayFn(i,s):e[n]=this.arrayFn([],s);else if(this.deep&&s&&"object"==typeof s)if(i&&"object"==typeof i)r.push([i,s]);else{if(this.strict)continue;const t=this.createObject();e[n]=t,r.push([t,s])}else e[n]=s}}}}merge(e,...t){for(let r=0;r<t.length;r++)this.mergeInto(e,t[r]);return e}mergeAt(e,t,...r){const n=this.path.normalize(t).tokens;let s=e;for(let t=0;t<n.length;t++){const r=n[t];if(this.isUnsafeKey(r))return e;let i=s[r];if(null==i){if(this.strict)return e;i="number"==typeof n[t+1]?[]:this.createObject(),s[r]=i}s=i}for(let e=0;e<r.length;e++)this.mergeInto(s,r[e]);return e}}e.Accessor=r,e.Merger=n,e.Path=t,e.factory=e=>Object.freeze({accessor:new r(e?.pathOptions),merger:new n(e),path:new t(e?.pathOptions)})});
1
+ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).deepmerge={})}(this,function(e){"use strict";class t{cache;maxCacheSize;constructor(e={}){!1!==e.cache&&(this.cache=new Map),this.maxCacheSize=e.maxCacheSize??1e3}compile(e){if(!e)return Object.freeze({tokens:[]});if(this.cache){const t=this.cache.get(e);if(t)return t}const t=[];let r=0,n="";for(;r<e.length;){const s=e.charCodeAt(r);if(46!==s){if(91===s){n&&(t.push(n),n=""),r++;const s=r;for(;r<e.length&&93!==e.charCodeAt(r);)r++;const i=e.slice(s,r),c=i>>>0;String(c)===i?t.push(c):t.push(i),r++;continue}n+=e[r],r++}else n&&(t.push(n),n=""),r++}n&&t.push(n);const s=Object.freeze({tokens:t});return this.cache&&(this.cache.size>=this.maxCacheSize&&this.cache.clear(),this.cache.set(e,s)),s}isCompiled(e){return e&&Array.isArray(e.tokens)}normalize(e){return"string"==typeof e?this.compile(e):e}clearCache(){this.cache?.clear()}get size(){return this.cache?.size??0}}class r{path;constructor(e={}){this.path=new t(e)}isUnsafeKey(e){return"__proto__"===e||"constructor"===e||"prototype"===e}walk(e,t,r){const n=this.path.normalize(t).tokens;let s=e;for(let e=0;e<n.length-1;e++){const t=n[e];if(this.isUnsafeKey(t))return;let i=s?.[t];if(null==i){if(!r)return;i="number"==typeof n[e+1]?[]:{},s[t]=i}s=i}const i=n[n.length-1];return{parent:s,key:i,value:s?.[i]}}has(e,t){const r=this.path.normalize(t).tokens;let n=e;for(let e=0;e<r.length;e++){if(null==n)return!1;n=n[r[e]]}return!0}get(e,t){const r=this.path.normalize(t).tokens;let n=e;for(let e=0;e<r.length;e++){if(null==n)return;n=n[r[e]]}return n}set(e,t,r){const n=this.walk(e,t,!0);n&&(n.parent[n.key]=r)}delete(e,t){const r=this.walk(e,t,!1);r&&null!=r.parent&&delete r.parent[r.key]}update(e,t,r){const n=this.walk(e,t,!0);n&&(n.parent[n.key]=r(n.value))}}class n{protect;deep;mergeUndefined;strict;createObject;valueFn;arrayFn;path;constructor(e={}){this.protect=!!e.protect,this.deep=!1!==e.deep,this.mergeUndefined=!!e.mergeUndefined,this.strict=!!e.strict,this.createObject=e.createObject??(()=>Object.create(null)),this.valueFn=e.valueFn,this.arrayFn=this.compileArrayFn(e.arrayMode),this.path=new t(e.pathOptions)}compileArrayFn(e){switch(e??"replace"){case"replace":return(e,t)=>t;case"keep":return(e,t)=>e;case"concat":return(e,t)=>e.concat(t);case"reference":return(e,t)=>{const r=new Set(e);for(let e=0;e<t.length;e++)r.add(t[e]);return Array.from(r)};case"unique":return(e,t)=>{const r=new Map,n=e=>{for(let t=0;t<e.length;t++){const n=e[t];r.set(n&&"object"==typeof n?JSON.stringify(n):n,n)}};return n(e),n(t),[...r.values()]}}if("function"==typeof e)return e;throw new Error(`Invalid array merge mode: ${e}`)}isUnsafeKey(e){return"__proto__"===e||"constructor"===e||"prototype"===e}mergeInto(e,t){if(null==t)return;const r=[[e,t]];for(;r.length;){const[e,t]=r.pop();for(const n in t){if(this.isUnsafeKey(n))continue;const s=t[n],i=e[n];if((void 0!==s||this.mergeUndefined)&&(!this.protect||!(n in e))){if(this.valueFn){const t=this.valueFn(n,i,s);if(void 0!==t){e[n]=t;continue}}if(Array.isArray(s))Array.isArray(i)?e[n]=this.arrayFn(i,s):e[n]=this.arrayFn([],s);else if(this.deep&&s&&"object"==typeof s)if(i&&"object"==typeof i)r.push([i,s]);else{if(this.strict)continue;const t=this.createObject();e[n]=t,r.push([t,s])}else e[n]=s}}}}merge(e,...t){for(let r=0;r<t.length;r++)this.mergeInto(e,t[r]);return e}mergeAt(e,t,...r){const n=this.path.normalize(t).tokens;let s=e;for(let t=0;t<n.length;t++){const r=n[t];if(this.isUnsafeKey(r))return e;let i=s[r];if(null==i){if(this.strict)return e;i="number"==typeof n[t+1]?[]:this.createObject(),s[r]=i}s=i}for(let e=0;e<r.length;e++)this.mergeInto(s,r[e]);return e}}e.Accessor=r,e.Merger=n,e.Path=t,e.factory=e=>Object.freeze({accessor:new r(e?.pathOptions),merger:new n(e),path:new t(e?.pathOptions)})});
package/dist/merger.cjs CHANGED
@@ -29,12 +29,29 @@ class Merger {
29
29
  return (t, _) => t;
30
30
  case 'concat':
31
31
  return (t, s) => t.concat(s);
32
- case 'unique':
32
+ case 'reference':
33
33
  return (t, s) => {
34
34
  const set = new Set(t);
35
35
  for (let i = 0; i < s.length; i++) set.add(s[i]);
36
36
  return Array.from(set);
37
37
  };
38
+ case 'unique':
39
+ return (t, s) => {
40
+ const map = new Map(),
41
+ add = (array) => {
42
+ for (let i = 0; i < array.length; i++) {
43
+ const item = array[i];
44
+ map.set(
45
+ item && typeof item === 'object'
46
+ ? JSON.stringify(item)
47
+ : item,
48
+ item
49
+ );
50
+ }
51
+ };
52
+ (add(t), add(s));
53
+ return [...map.values()];
54
+ };
38
55
  }
39
56
  if (typeof mode === 'function') return mode;
40
57
  else throw new Error(`Invalid array merge mode: ${mode}`);
package/dist/merger.d.ts CHANGED
@@ -19,6 +19,8 @@ export declare const enum ArrayMode {
19
19
  Keep = "keep",
20
20
  /** Concatenate the source array to the target array. */
21
21
  Concat = "concat",
22
+ /** Reference-based array merging. */
23
+ Reference = "reference",
22
24
  /** Merge unique elements from both arrays. */
23
25
  Unique = "unique"
24
26
  }
package/dist/merger.mjs CHANGED
@@ -27,12 +27,29 @@ class Merger {
27
27
  return (t, _) => t;
28
28
  case 'concat':
29
29
  return (t, s) => t.concat(s);
30
- case 'unique':
30
+ case 'reference':
31
31
  return (t, s) => {
32
32
  const set = new Set(t);
33
33
  for (let i = 0; i < s.length; i++) set.add(s[i]);
34
34
  return Array.from(set);
35
35
  };
36
+ case 'unique':
37
+ return (t, s) => {
38
+ const map = new Map(),
39
+ add = (array) => {
40
+ for (let i = 0; i < array.length; i++) {
41
+ const item = array[i];
42
+ map.set(
43
+ item && typeof item === 'object'
44
+ ? JSON.stringify(item)
45
+ : item,
46
+ item
47
+ );
48
+ }
49
+ };
50
+ (add(t), add(s));
51
+ return [...map.values()];
52
+ };
36
53
  }
37
54
  if (typeof mode === 'function') return mode;
38
55
  else throw new Error(`Invalid array merge mode: ${mode}`);
package/package.json CHANGED
@@ -1,61 +1,61 @@
1
- {
2
- "name": "@komed3/deepmerge",
3
- "description": "Fast and efficient deep object merging and manipulation library",
4
- "license": "MIT",
5
- "version": "1.0.0",
6
- "author": {
7
- "name": "komed3 (Paul Köhler)",
8
- "email": "webmaster@komed3.de",
9
- "url": "https://komed3.de"
10
- },
11
- "homepage": "https://github.com/komed3/deepmerge",
12
- "keywords": [
13
- "merge",
14
- "object",
15
- "manipulation",
16
- "path",
17
- "accessor",
18
- "merger",
19
- "deep-merge",
20
- "deep-manipulation"
21
- ],
22
- "repository": {
23
- "type": "git",
24
- "url": "git+https://github.com/komed3/deepmerge.git"
25
- },
26
- "bugs": {
27
- "url": "https://github.com/komed3/deepmerge/issues"
28
- },
29
- "funding": {
30
- "type": "ko-fi",
31
- "url": "https://ko-fi.com/komed3"
32
- },
33
- "files": [
34
- "dist",
35
- "README",
36
- "LICENSE"
37
- ],
38
- "engines": {
39
- "node": ">=18.0.0"
40
- },
41
- "types": "dist/index.d.ts",
42
- "main": "dist/index.cjs",
43
- "module": "dist/index.mjs",
44
- "browser": "dist/index.umd.min.js",
45
- "jsdelivr": "dist/index.umd.min.js",
46
- "unpkg": "dist/index.umd.min.js",
47
- "scripts": {
48
- "build": "rollup -c && tsc --emitDeclarationOnly",
49
- "test": "node tests/access.js && node tests/merge.js && node tests/path.js"
50
- },
51
- "devDependencies": {
52
- "@rollup/plugin-commonjs": "^29.0.2",
53
- "@rollup/plugin-node-resolve": "^16.0.3",
54
- "@rollup/plugin-terser": "^1.0.0",
55
- "@rollup/plugin-typescript": "^12.3.0",
56
- "rollup-plugin-cleanup": "^3.2.1",
57
- "rollup-plugin-prettier": "^4.1.2",
58
- "tslib": "^2.8.1",
59
- "typescript": "^6.0.2"
60
- }
61
- }
1
+ {
2
+ "name": "@komed3/deepmerge",
3
+ "description": "Fast and efficient deep object merging and manipulation library",
4
+ "license": "MIT",
5
+ "version": "1.1.0",
6
+ "author": {
7
+ "name": "komed3 (Paul Köhler)",
8
+ "email": "webmaster@komed3.de",
9
+ "url": "https://komed3.de"
10
+ },
11
+ "homepage": "https://github.com/komed3/deepmerge",
12
+ "keywords": [
13
+ "merge",
14
+ "object",
15
+ "manipulation",
16
+ "path",
17
+ "accessor",
18
+ "merger",
19
+ "deep-merge",
20
+ "deep-manipulation"
21
+ ],
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "git+https://github.com/komed3/deepmerge.git"
25
+ },
26
+ "bugs": {
27
+ "url": "https://github.com/komed3/deepmerge/issues"
28
+ },
29
+ "funding": {
30
+ "type": "ko-fi",
31
+ "url": "https://ko-fi.com/komed3"
32
+ },
33
+ "files": [
34
+ "dist",
35
+ "README",
36
+ "LICENSE"
37
+ ],
38
+ "engines": {
39
+ "node": ">=18.0.0"
40
+ },
41
+ "types": "dist/index.d.ts",
42
+ "main": "dist/index.cjs",
43
+ "module": "dist/index.mjs",
44
+ "browser": "dist/index.umd.min.js",
45
+ "jsdelivr": "dist/index.umd.min.js",
46
+ "unpkg": "dist/index.umd.min.js",
47
+ "scripts": {
48
+ "build": "rollup -c && tsc --emitDeclarationOnly",
49
+ "test": "node tests/access.js && node tests/merge.js && node tests/path.js"
50
+ },
51
+ "devDependencies": {
52
+ "@rollup/plugin-commonjs": "^29.0.3",
53
+ "@rollup/plugin-node-resolve": "^16.0.3",
54
+ "@rollup/plugin-terser": "^1.0.0",
55
+ "@rollup/plugin-typescript": "^12.3.0",
56
+ "rollup-plugin-cleanup": "^3.2.1",
57
+ "rollup-plugin-prettier": "^4.2.0",
58
+ "tslib": "^2.8.1",
59
+ "typescript": "^6.0.3"
60
+ }
61
+ }