From 772e92789b82a53ff642b582c4bb1291e97b6a5d Mon Sep 17 00:00:00 2001 From: Aaron Jensen Date: Sun, 2 Aug 2015 00:32:32 -0700 Subject: [PATCH] Freeze results by default --- README.md | 22 ++++++++++++---------- lib/index.js | 16 ++++++++++++---- lib/update.js | 3 +-- package.json | 2 +- test/index.js | 9 ++++++++- 5 files changed, 34 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 2a6af26..45be631 100644 --- a/README.md +++ b/README.md @@ -1,25 +1,25 @@ -# updeep [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url] -> Easily update nested objects and arrays (frozen or not) in a declarative -> and immutable way. +# updeep [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url] +> 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. -You can use it on frozen objects, as updeep never mutates. 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]). +[PureRenderMixin]). Because of this, everything returned by updeep is frozen. -updeep requires [lodash], but works very well with `lodash-fp`. As a matter of -fact, many of the helpers functions are [curried][currying] [lodash] functions with their -parameters reversed. +updeep requires [lodash], but works very well with [lodash-fp] or [Ramda]. As a +matter of fact, many of the helpers functions are [curried][currying] [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][currying], so the parameter order is: `updeep(updates, obj)`. +supports [partial application][currying], so the parameter order is: +`updeep(updates, obj)`. -## Examples +## API and Examples ### Full example ```js @@ -148,6 +148,8 @@ MIT ©2015 [Aaron Jensen](https://twitter.com/aaronjensen) [daviddm-image]: https://david-dm.org/aaronjensen/updeep.svg?theme=shields.io [daviddm-url]: https://david-dm.org/aaronjensen/updeep [lodash]: http://lodash.com +[lodash-fp]: https://github.com/lodash/lodash-fp +[Ramda]: http://ramdajs.com/ [PureRenderMixin]: https://facebook.github.io/react/docs/pure-render-mixin.html [redux]: https://github.com/gaearon/redux [immutablejs]: https://github.com/facebook/immutable-js diff --git a/lib/index.js b/lib/index.js index 74747d5..b824fbb 100644 --- a/lib/index.js +++ b/lib/index.js @@ -2,9 +2,17 @@ import update from './update'; import omit from './omit'; import reject from './reject'; import withDefault from './withDefault'; +import freeze from './freeze'; +import curry from 'lodash/function/curry'; -update.omit = omit; -update.reject = reject; -update.withDefault = withDefault; +function updateAndFreeze(updates, obj) { + return freeze(update(updates, obj)); +} -export default update; +const curried = curry(updateAndFreeze); + +curried.omit = omit; +curried.reject = reject; +curried.withDefault = withDefault; + +export default curried; diff --git a/lib/update.js b/lib/update.js index 9c9f470..e7e6187 100644 --- a/lib/update.js +++ b/lib/update.js @@ -1,6 +1,5 @@ import reduce from 'lodash/collection/reduce'; import isEmpty from 'lodash/lang/isEmpty'; -import curry from 'lodash/function/curry'; import assign from 'lodash/object/assign'; function resolveUpdates(updates, obj = {}) { @@ -62,4 +61,4 @@ function update(updates, obj) { return assign({}, obj, resolvedUpdates); } -export default curry(update); +export default update; diff --git a/package.json b/package.json index 8d51d62..7687dc5 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "updeep", "version": "0.1.3", - "description": "Easily update nested objects and arrays (frozen or not) in a declarative and immutable way.", + "description": "Easily update nested frozen objects and arrays in a declarative and immutable manner.", "homepage": "https://github.com/aaronjensen/updeep", "repository": { "type": "git", diff --git a/test/index.js b/test/index.js index d1dc76c..ca27f91 100644 --- a/test/index.js +++ b/test/index.js @@ -59,7 +59,7 @@ describe('updeep', () => { expect(result).to.deep.equal({ foo: 4, bar: 5, baz: 7 }); }); - it('is curryable', () => { + it('can be partially applied', () => { const inc = (i) => i + 1; const obj = { foo: 3 }; const incFoo = u({ foo: inc }); @@ -102,4 +102,11 @@ describe('updeep', () => { expect(result).to.eql({ foo: {} }); }); + + it('deeply freezes the result', () => { + const result = u({ foo: { bar: 3 } }, { foo: { bar: 0 } }); + + expect(Object.isFrozen(result)).to.be.true; + expect(Object.isFrozen(result.foo)).to.be.true; + }); });