deepmerge-json
Advanced tools
Comparing version 1.4.0 to 1.5.0
# Changelog | ||
## [1.5.0] - 2022-02-08 | ||
### Added | ||
- `merge` operation. It should be used instead of `$replace`. It works like `$replace` was before `1.4.0`. It merges objects when the index is found and add them when the index is missing. | ||
-- | ||
## [1.4.0] - 2022-02-08 | ||
@@ -105,2 +113,3 @@ | ||
[unreleased]: https://github.com/kleber-swf/deepmerge-json/tree/develop | ||
[1.5.0]: https://github.com/kleber-swf/deepmerge-json/tree/v1.5.0 | ||
[1.4.0]: https://github.com/kleber-swf/deepmerge-json/tree/v1.4.0 | ||
@@ -107,0 +116,0 @@ [1.3.2]: https://github.com/kleber-swf/deepmerge-json/tree/v1.3.2 |
@@ -18,21 +18,5 @@ export default merge; | ||
declare namespace merge { | ||
/** | ||
* Deeply clones an object. This method is an alias to {@linkcode merge} with a single parameter. | ||
* | ||
* @template T | ||
* @param {T} obj The object to be cloned | ||
* @returns {T} The object passed as parameter deeply cloned. | ||
*/ | ||
function clone<T>(obj: T): T; | ||
/** | ||
* Merges multiple objects. It respects the order of the parameters and the operations just like | ||
* expected if you call {@linkcode merge} multiple times passing the last result as the first | ||
* parameter to the next call. | ||
* | ||
* @param {any} pre The required first object | ||
* @param {...any} args Subsequential objects. Keep in mind that their order matters. | ||
* @returns {any} A deeply merged object containing all of the parameters | ||
*/ | ||
function multi(pre: any, ...args: any[]): any; | ||
} | ||
//# sourceMappingURL=deepmerge-json.d.ts.map |
@@ -58,2 +58,13 @@ (function (global, factory) { | ||
const indexedMerge = function (pre, pos) { | ||
pre = pre.slice(); | ||
let kn; | ||
Object.keys(pos).forEach(k => { | ||
kn = Number.parseInt(k); | ||
if (kn < 0 || Number.isNaN(kn)) throw Error(`Invalid index for $merge: ${k}`); | ||
pre[kn] = merge(pre[kn], pos[k]); | ||
}); | ||
return pre; | ||
}; | ||
const insert = function (pre, pos) { | ||
@@ -75,2 +86,3 @@ pre = shallowCopyArray(undefined, pre); | ||
$replace: indexedReplace, | ||
$merge: indexedMerge, | ||
$insert: insert, | ||
@@ -148,2 +160,7 @@ $set: shallowCopyArray, // TODO clone | ||
// TODO Test the following approach: clone both objects at the start so we won't need to | ||
// keep cloning the objects individually during the process. | ||
// 1. we can clone with JSON.parse(JSON.stringify) and check the __proto__ during the merge | ||
// 2. we can clone manually and check for __proto__ during this phase | ||
return merge; | ||
@@ -150,0 +167,0 @@ |
@@ -1,2 +0,2 @@ | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(e="undefined"!=typeof globalThis?globalThis:e||self).merge=r()}(this,(function(){"use strict";const e="__proto__",r=(e,r)=>r,t=(e,r)=>r.map((e=>c(e))),n=(r,t)=>{if(t&&t.hasOwnProperty("__proto__")){const r={};for(let n in t)n!==e&&(r[n]=t[n]);return r}return Object.assign({},t)},o={$push:(e,r)=>e.concat(r),$append:(e,r)=>e.concat(r),$prepend:(e,r)=>r.concat(e),$replace:function(e,r){let n;return e=t(0,e),Object.keys(r).forEach((t=>{if(n=Number.parseInt(t),n<0||Number.isNaN(n))throw Error(`Invalid index for $replace: ${t}`);e[n]=c.clone(r[t])})),e},$insert:function(e,r){let n;return e=t(0,e),Object.keys(r).forEach((t=>{if(n=Number.parseInt(t),Number.isNaN(n))throw Error(`Invalid index for $insert: ${t}`);e.splice(n,0,r[t])})),e},$set:t},i={oo:function(r,t){return r=n(0,r),Object.keys(t).forEach((n=>{n!==e&&(r[n]=c(r[n],t[n]))})),r},oa:n,ob:r,aa:function(e,r){return e=t(0,e),r.forEach(((r,t)=>e[t]=c(e[t],r))),e},ao:function(r,n){return r=t(0,r),Object.keys(n).forEach((t=>{t!==e&&(r=t in o?o[t](r,n[t]):n)})),r},ab:r,bb:r,bo:n,ba:t};function c(e,r){if(void 0===r){if(null==e)return e;r=Array.isArray(e)?[]:"object"==typeof e?{}:e}else if(null===r)return null;const t=Array.isArray(e)?"a":"object"==typeof e?"o":"b",n=Array.isArray(r)?"a":"object"==typeof r?"o":"b";return i[t+n](e,r)}return c.clone=e=>c(e),c.multi=(e,...r)=>{if(!r)return c(e);for(let t=0;t<r.length;t++)e=c(e,r[t]);return e},c})); | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(e="undefined"!=typeof globalThis?globalThis:e||self).merge=r()}(this,(function(){"use strict";const e="__proto__",r=(e,r)=>r,t=(e,r)=>r.map((e=>c(e))),n=(r,t)=>{if(t&&t.hasOwnProperty("__proto__")){const r={};for(let n in t)n!==e&&(r[n]=t[n]);return r}return Object.assign({},t)},o={$push:(e,r)=>e.concat(r),$append:(e,r)=>e.concat(r),$prepend:(e,r)=>r.concat(e),$replace:function(e,r){let n;return e=t(0,e),Object.keys(r).forEach((t=>{if(n=Number.parseInt(t),n<0||Number.isNaN(n))throw Error(`Invalid index for $replace: ${t}`);e[n]=c.clone(r[t])})),e},$merge:function(e,r){let t;return e=e.slice(),Object.keys(r).forEach((n=>{if(t=Number.parseInt(n),t<0||Number.isNaN(t))throw Error(`Invalid index for $merge: ${n}`);e[t]=c(e[t],r[n])})),e},$insert:function(e,r){let n;return e=t(0,e),Object.keys(r).forEach((t=>{if(n=Number.parseInt(t),Number.isNaN(n))throw Error(`Invalid index for $insert: ${t}`);e.splice(n,0,r[t])})),e},$set:t},i={oo:function(r,t){return r=n(0,r),Object.keys(t).forEach((n=>{n!==e&&(r[n]=c(r[n],t[n]))})),r},oa:n,ob:r,aa:function(e,r){return e=t(0,e),r.forEach(((r,t)=>e[t]=c(e[t],r))),e},ao:function(r,n){return r=t(0,r),Object.keys(n).forEach((t=>{t!==e&&(r=t in o?o[t](r,n[t]):n)})),r},ab:r,bb:r,bo:n,ba:t};function c(e,r){if(void 0===r){if(null==e)return e;r=Array.isArray(e)?[]:"object"==typeof e?{}:e}else if(null===r)return null;const t=Array.isArray(e)?"a":"object"==typeof e?"o":"b",n=Array.isArray(r)?"a":"object"==typeof r?"o":"b";return i[t+n](e,r)}return c.clone=e=>c(e),c.multi=(e,...r)=>{if(!r)return c(e);for(let t=0;t<r.length;t++)e=c(e,r[t]);return e},c})); | ||
//# sourceMappingURL=deepmerge-json.min.js.map |
{ | ||
"name": "deepmerge-json", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"description": "A library to deep merge json files with array operations", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -6,2 +6,3 @@ # Deepmerge Json | ||
![npm](https://img.shields.io/npm/v/deepmerge-json) | ||
![Snyk Vulnerabilities for GitHub Repo](https://img.shields.io/snyk/vulnerabilities/github/kleber-swf/deepmerge-json) | ||
@@ -248,5 +249,7 @@ # What | ||
> **Note for users of version `< 1.4.0`**: if you want the old operation functionality, replace `$replace` for [`$merge`](#merge). | ||
```js | ||
const left = [{ a: 1, b: 1 }, { c: 1 }]; | ||
const right = [{ a: 2 }]; | ||
const right = { $merge: [{ a: 2 }] }; | ||
const result = merge(left, right); | ||
@@ -258,2 +261,38 @@ | ||
### $merge | ||
Use `$merge` to merge or add indexed elements by their indexes. Indexes can be numbers or strings and cannot be less than 0 or `NaN` values. It's similar to [`$replace`](#replace) but instead of replacing the values when found, it merges them with the new values. | ||
#### With valid indexes: | ||
```js | ||
const left = [10, 20, 30]; | ||
const right = { $merge: { 0: 100, 2: 300, 4: 400 } }; | ||
const result = merge(left, right); | ||
// Result (note that the element with index 3 was never given) | ||
[100, 20, 300, , 400]; | ||
``` | ||
#### With invalid indexes: | ||
```js | ||
const left = [10, 20, 30]; | ||
const right = { $merge: { null: 0, foo: 0, true: 0, '-1': 0 } }; | ||
const result = merge(left, right); | ||
// throws an Error | ||
``` | ||
#### With objects | ||
```js | ||
const left = [{ a: 1, b: 1 }, { c: 1 }]; | ||
const right = { $merge: [{ a: 2 }] }; | ||
const result = merge(left, right); | ||
// Result | ||
[{ a: 2, b: 1 }, { c: 1 }]; | ||
``` | ||
### $insert | ||
@@ -260,0 +299,0 @@ |
@@ -52,2 +52,13 @@ const FK = '__proto__'; | ||
const indexedMerge = function (pre, pos) { | ||
pre = pre.slice(); | ||
let kn; | ||
Object.keys(pos).forEach(k => { | ||
kn = Number.parseInt(k); | ||
if (kn < 0 || Number.isNaN(kn)) throw Error(`Invalid index for $merge: ${k}`); | ||
pre[kn] = merge(pre[kn], pos[k]); | ||
}); | ||
return pre; | ||
}; | ||
const insert = function (pre, pos) { | ||
@@ -69,2 +80,3 @@ pre = shallowCopyArray(undefined, pre); | ||
$replace: indexedReplace, | ||
$merge: indexedMerge, | ||
$insert: insert, | ||
@@ -71,0 +83,0 @@ $set: shallowCopyArray, // TODO clone |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
47895
364
418
19148