deep-patch
v1.0.1
Published
Modify an object recursively by an array of sequential patches.
Downloads
16
Maintainers
Readme
Deep Patch
Modify an object recursively by an array of sequential patches.
Get Started
npm install deep-patch
API
patch(input, patches)
Returns new Object modified recursively by mods
, an array of one or more sequential patches.
input
: Object input. (Object)patches
: Array of patches. (Array)
patch operations
If path
is not found in input
, a new value is created. Type is defined by expected value.
{path: {$set: value}}
: Set a mixed value topath
.{path: {$push: value}}
: Pushvalue
to Array inpath
.{path: {$unshift: value}}
: Unshiftvalue
to Array inpath
.{path: {$filter: value}}
: Runsvalue
as filter Function to Array inpath
.{path: {$map: value}}
: Runsvalue
as map Function to Array inpath
.{path: {$apply: value}}
: Runsvalue
as Function to any value inpath
. Input value in samepath
is provided as first argument.{path: {$merge: value}}
: Mergesvalue
intopath
Object.
Usage
var patch = require("deep-patch");
var data = {
a: { aa: { aaa: "string" } },
b: "string",
};
patch(data, [
{ a: { aa: { aaa: { $set: "modified string" } } } },
{ b: { $set: "modified string" } },
]);
// →
// {
// a: {aa: {aaa: 'modified string' }},
// b: 'modified string'
// }
Benchmark
node benchmark.js
Results
$set x 135,799 ops/sec ±0.85% (85 runs sampled)
License
See the License file.