deep-patch

1.0.1 • Public • Published

Deep Patch

Build Status Dependency Status devDependency Status

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 to path.
  • {path: {$push: value}}: Push value to Array in path.
  • {path: {$unshift: value}}: Unshift value to Array in path.
  • {path: {$filter: value}}: Runs value as filter Function to Array in path.
  • {path: {$map: value}}: Runs value as map Function to Array in path.
  • {path: {$apply: value}}: Runs value as Function to any value in path. Input value in same path is provided as first argument.
  • {path: {$merge: value}}: Merges value into path 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.

Package Sidebar

Install

npm i deep-patch

Weekly Downloads

2

Version

1.0.1

License

MIT

Unpacked Size

10.6 kB

Total Files

13

Last publish

Collaborators

  • grindcode