Merges and patches a target object. By default the returning object is a shallow merge of unpatched and deep merge of patched target's properties. There is an option to clone unpatched target properties as well.
npm install json-deep-merge-patchMerges and patches a target object. By default the returning object is a shallow merge of unpatched and deep merge of patched target's properties. There is an option to clone unpatched target properties as well.
* As described above, the returning object is a clone, so target object is left intact. If you need RFC 7396 compliance and mutating the target object, use json-merge-patch.
npm:
``sh`
npm i`
yarn:sh`
yarn
- Open ./bench.js in your favourite code editor and replace the target and patch values with the desired ones.
- Run the benchmark.
npm:
`sh`
npm run bench`
yarn:sh`
yarn bench
npm:
`sh`
npm i json-deep-merge-patch --save`
yarn:sh`
yarn add json-deep-merge-patch
`js
const jsonDeepMergePatch = require('json-deep-merge-patch')
const target = {
title: 'Goodbye!',
author: {
givenName: 'John',
familyName: 'Doe'
},
tags: ['example', 'sample'],
content: 'This will be unchanged'
}
const patch = {
title: 'Hello!',
phoneNumber: '+01-123-456-7890',
author: {
familyName: null
},
tags: ['example']
}
const result = jsonDeepMergePatch(target, patch)
/*
result = {
title: 'Hello!',
author: {
givenName: 'John'
},
tags: ['example'],
content: 'This will be unchanged',
phoneNumber: '+01-123-456-7890'
}
*/
`
`ts`
jsonDeepMergePatch(target: Object, patch: Object, opts?: Options)
- depth (null|Number, default: null)null
- Specifies the number of times to recurse while merging the object, if set to (default) it will recurse up to the maximum call stack.keepNulls
- (Boolean, default: false)cloneUnpatchedProps
- As the name suggests properties with null values in patch set matching target properties to null, instead of deleting them.
- (Boolean, default: false)false
- If set to (default), the returning object is a shallow merge of unpatched and deep merge of patched target's properties.true`, the returning object is a deep merge of unpatched and patched target's properties.
- If set to