Freeze results by default

This commit is contained in:
Aaron Jensen 2015-08-02 00:32:32 -07:00
parent 3ef66e17e4
commit 772e92789b
5 changed files with 34 additions and 18 deletions

View File

@ -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

View File

@ -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;

View File

@ -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;

View File

@ -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",

View File

@ -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;
});
});