Freeze results by default
This commit is contained in:
parent
3ef66e17e4
commit
772e92789b
22
README.md
22
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]
|
# 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
|
> Easily update nested frozen objects and arrays in a declarative and immutable
|
||||||
> and immutable way.
|
> manner.
|
||||||
|
|
||||||
## About
|
## About
|
||||||
|
|
||||||
Updating deeply nested objects/arrays is a bit of a pain.
|
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
|
updeep makes it painless by allowing you to declare the updates you would like
|
||||||
to make and it will take care of the rest.
|
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,
|
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
|
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
|
updeep requires [lodash], but works very well with [lodash-fp] or [Ramda]. As a
|
||||||
fact, many of the helpers functions are [curried][currying] [lodash] functions with their
|
matter of fact, many of the helpers functions are [curried][currying] [lodash]
|
||||||
parameters reversed.
|
functions with their parameters reversed (as [lodash-fp] do).
|
||||||
|
|
||||||
Note that the parameters may be backwards from what you are used to. updeep
|
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
|
### Full example
|
||||||
```js
|
```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-image]: https://david-dm.org/aaronjensen/updeep.svg?theme=shields.io
|
||||||
[daviddm-url]: https://david-dm.org/aaronjensen/updeep
|
[daviddm-url]: https://david-dm.org/aaronjensen/updeep
|
||||||
[lodash]: http://lodash.com
|
[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
|
[PureRenderMixin]: https://facebook.github.io/react/docs/pure-render-mixin.html
|
||||||
[redux]: https://github.com/gaearon/redux
|
[redux]: https://github.com/gaearon/redux
|
||||||
[immutablejs]: https://github.com/facebook/immutable-js
|
[immutablejs]: https://github.com/facebook/immutable-js
|
||||||
|
16
lib/index.js
16
lib/index.js
@ -2,9 +2,17 @@ import update from './update';
|
|||||||
import omit from './omit';
|
import omit from './omit';
|
||||||
import reject from './reject';
|
import reject from './reject';
|
||||||
import withDefault from './withDefault';
|
import withDefault from './withDefault';
|
||||||
|
import freeze from './freeze';
|
||||||
|
import curry from 'lodash/function/curry';
|
||||||
|
|
||||||
update.omit = omit;
|
function updateAndFreeze(updates, obj) {
|
||||||
update.reject = reject;
|
return freeze(update(updates, obj));
|
||||||
update.withDefault = withDefault;
|
}
|
||||||
|
|
||||||
export default update;
|
const curried = curry(updateAndFreeze);
|
||||||
|
|
||||||
|
curried.omit = omit;
|
||||||
|
curried.reject = reject;
|
||||||
|
curried.withDefault = withDefault;
|
||||||
|
|
||||||
|
export default curried;
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import reduce from 'lodash/collection/reduce';
|
import reduce from 'lodash/collection/reduce';
|
||||||
import isEmpty from 'lodash/lang/isEmpty';
|
import isEmpty from 'lodash/lang/isEmpty';
|
||||||
import curry from 'lodash/function/curry';
|
|
||||||
import assign from 'lodash/object/assign';
|
import assign from 'lodash/object/assign';
|
||||||
|
|
||||||
function resolveUpdates(updates, obj = {}) {
|
function resolveUpdates(updates, obj = {}) {
|
||||||
@ -62,4 +61,4 @@ function update(updates, obj) {
|
|||||||
return assign({}, obj, resolvedUpdates);
|
return assign({}, obj, resolvedUpdates);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default curry(update);
|
export default update;
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "updeep",
|
"name": "updeep",
|
||||||
"version": "0.1.3",
|
"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",
|
"homepage": "https://github.com/aaronjensen/updeep",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
@ -59,7 +59,7 @@ describe('updeep', () => {
|
|||||||
expect(result).to.deep.equal({ foo: 4, bar: 5, baz: 7 });
|
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 inc = (i) => i + 1;
|
||||||
const obj = { foo: 3 };
|
const obj = { foo: 3 };
|
||||||
const incFoo = u({ foo: inc });
|
const incFoo = u({ foo: inc });
|
||||||
@ -102,4 +102,11 @@ describe('updeep', () => {
|
|||||||
|
|
||||||
expect(result).to.eql({ foo: {} });
|
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;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user