Skip to content

zaim/immpatch

Repository files navigation

immpatch

Build Status coverage dependencies

Update immutable.js objects using JSON Patch operations

Install

$ npm install --save immpatch

Usage

var expect = require('expect.js');
var Immutable = require('immutable');
var patch = require('immpatch');

var object = Immutable.fromJS({ baz: 'qux', foo: 'bar' });

var patched = patch(object, [
  { op: 'replace', path: '/baz', value: 'boo' },
  { op: 'add', path: '/hello', value: ['world'] },
  { op: 'remove', path: '/foo'}
]);

expect(patched.toJS()).to.eql({
  baz: 'boo',
  hello: ['world']
});

For more information about JSON Patch, see:

For a diff / patch generator see immutablediff.

Another similar module is immutablepatch, immpatch is a simpler implementation that uses plain JS objects as patch operations, and has more complete tests with 100% code coverage.

API

patch

obj = patch(obj, ops);

Update an Immutable.js object using JSON Patch operations. This function is the default export, i.e., what you get when you require('immpatch') or import patch from 'immpatch'.

  • obj: Immutable.Iterable - The Immutable.js object to update.
  • ops: array|object - Either an RFC6902 JSON Patch Document (an array), or a single Patch operation object.

Example

// Array of patch objects:
obj = patch(obj, [
  { op: 'add', path: '/hello', value: ['world'] },
  { op: 'remove', path: '/foo'}
])

// Single patch object:
obj = patch(obj, { op: 'replace', path: '/baz', value: 'boo' })

Testing

$ git clone git://github.com/zaim/immpatch
$ npm install
$ npm run test

Test coverage:

$ npm run testcov

Contributing

Use Github issues for bug reports and requests.

Pull requests are actively welcomed.

License

immpatch is MIT licensed.

About

Update immutable.js objects using JSON Patch operations

Resources

License

Stars

Watchers

Forks

Packages

No packages published