Fork of updeep using Remeda
Go to file
Aaron Jensen 7a84944f99 Add u.freeze
Fixes #7
2015-08-04 21:56:01 -07:00
lib Add u.freeze 2015-08-04 21:56:01 -07:00
test Add u.freeze 2015-08-04 21:56:01 -07:00
.babelrc Enable loose mode 2015-08-01 09:09:21 -07:00
.editorconfig Initial commit w/ yo node 2015-07-31 08:53:25 -07:00
.eslintignore Add .eslintignore 2015-08-04 03:54:02 -07:00
.eslintrc use airbnb's eslint 2015-08-01 23:25:08 -07:00
.gitattributes Initial commit w/ yo node 2015-07-31 08:53:25 -07:00
.gitignore Initial commit w/ yo node 2015-07-31 08:53:25 -07:00
.jshintrc add jshintrc for codeclimate 2015-08-02 07:48:51 -07:00
.travis.yml Update travis notification settings 2015-08-01 09:28:52 -07:00
CHANGELOG.md Add u.freeze 2015-08-04 21:56:01 -07:00
createWebpackConfig.js Add karma testing 2015-08-04 10:39:56 -07:00
gulpfile.js Add karma testing 2015-08-04 10:39:56 -07:00
karma.conf.js Add karma testing 2015-08-04 10:39:56 -07:00
LICENSE.txt Initial commit w/ yo node 2015-07-31 08:53:25 -07:00
package.json 0.2.3 2015-08-04 21:35:14 -07:00
README.md Add release instructions 2015-08-04 21:46:52 -07:00
webpack.config.js Add umd distribution builds via webpack 2015-08-04 10:13:34 -07:00

updeep

NPM version Build Status Code Climate Dependency Status peerDependency Status

Easily update nested frozen objects and arrays in a declarative and immutable manner.

About

Updating deeply nested objects/arrays is a bit of a pain. updeep makes it painless by allowing you to declare the updates you would like to make and it will take care of the rest. It will recursively return the same instance if no changes have been made, making it ideal for using reference equality checks to detect changes (like PureRenderMixin). Because of this, everything returned by updeep is frozen.

updeep requires lodash, but works very well with lodash-fp or Ramda. As a matter of fact, many of the helpers functions are curried lodash functions with their parameters reversed (as lodash-fp do).

Note that the parameters may be backwards from what you are used to. updeep supports partial application, so the parameter order is: updeep(updates, obj).

API and Examples

Full example

var u = require('updeep');

var person = {
  name: {
    first: 'Bill',
    last: 'Sagat',
  },
  children: ['Marty', 'Ashley'],
  email: 'bill@example.com',
  version: 1
};

var inc = function(i) { return i + 1; }

var newPerson = u({
  name: {
    first: 'Bob',
  },
  children: { 0: 'Mary-Kate' },
  email: 'bob@example.com',
  version: inc
}, person);

// => {
//      name: {
//        first: 'Bob',
//        last: 'Sagat',
//      },
//      children: ['Mary-Kate', 'Ashley'],
//      email: 'bob@example.com',
//      version: 2
//    }

Simple update

u({ x: { b: 3 } }, { x: { a: 0, b: 0 } });
// => { x: { a: 0, b: 3 } }

Multiple updates, including an array

u({ x: { b: 3 }, y: { 1: 4 } }, { x: { a: 0, b: 0 }, y: [0, 0] });
// => { x: { a: 0, b: 3 }, y: [0, 4] }

Use a function

function inc(i) { return i + 1; }
u({ x: { b: inc } }, { x: { a: 0, b: 0 } });
// => { x: { a: 0, b: 1 } }

Partial application

var setBTo3 = u({ b: 3 });
setBTo3({ a: 0, b: 0 });
// => { a: 0, b: 3 })

Remove a property

u({ x: u.omit('b') }, { x: { a: 0, b: 0 } });
// => { x: { a: 0 } }

Reject an item from an array

function even(i) { return i % 2 === 0 };
u({ x: u.reject(even) }, { x: [1, 2, 3, 4] });
// => { x: [1, 3] }

With a default

u({ x: withDefault([], { 0: 3 }) }, {});
// => { x: [3] }

ES6 computed properties

var key = 'b';
u({ x: { [key]: 3 } }, { x: { a: 0, b: 0 } });
// => { x: { a: 0, b: 3 } }

See the tests for more examples.

Install

$ npm install --save updeep

Requires lodash as a peer dependency, so make sure you have it installed as well.

Configuration

If NODE_ENV is "production", updeep will not attempt to freeze objects. This may yield a slight performance gain.

Motivation

While creating reducers for use with redux, I wanted something that made it easy to work with frozen objects. Native javascript objects have some nice advantages over things like Immutable.js such as debugging and destructuring. I wanted something more powerful than icepick and more composable than React.addons.update.

If you're manipulating massive amounts of data frequently, you may want to benchmark, as Immutable.js should be more efficient in that case.

Contributing

  1. Fork it.
  2. Create your feature branch (git checkout -b my-new-feature).
  3. Run gulp to run tests and lint.
  4. Commit your changes (git commit -am 'Added some feature').
  5. Push to the branch (git push origin my-new-feature).
  6. Create new Pull Request.

Releasing New Version

  1. Login to npm, if you don't have access to the package, ask for it.

    $ npm login
    
  2. Make sure the build passes (best to let it pass on travis, but you can run it locally):

    $ gulp
    
  3. Bump the version:

    $ npm version major|minor|patch
    
  4. Update the CHANGELOG.md.

  5. Add the new version and corresponding notes.

  6. Add a link to the new version.

  7. Update the unreleased link compare to be based off of the new version.

  8. Publish and push:

    $ npm publish
    $ git push master --follow-tags
    

License

MIT ©2015 Aaron Jensen