Add u.in and rework readme

This commit is contained in:
Aaron Jensen 2015-08-04 22:28:31 -07:00
parent f062562a64
commit 2ae6f0a113
5 changed files with 101 additions and 17 deletions

View File

@ -2,6 +2,7 @@
## [unreleased] ## [unreleased]
* Add `u.freeze` to freeze an object deeply. (https://github.com/substantial/updeep/issues/7) * Add `u.freeze` to freeze an object deeply. (https://github.com/substantial/updeep/issues/7)
* Add `u.in` to update a single value in an object with a specified path. (https://github.com/substantial/updeep/issues/6)
## [0.2.3] ## [0.2.3]
* Fix cannot update value to null (https://github.com/substantial/updeep/issues/8) * Fix cannot update value to null (https://github.com/substantial/updeep/issues/8)

View File

@ -64,21 +64,25 @@ var newPerson = u({
// } // }
``` ```
### Simple update **NOTE**: All functions are curried, so if you see `f(x(, y))`, it can be called with either `f(x, y)` or `f(x)(y)`.
### `u(updates(, object))`
#### Simple update
```js ```js
u({ x: { b: 3 } }, { x: { a: 0, b: 0 } }); u({ x: { b: 3 } }, { x: { a: 0, b: 0 } });
// => { x: { a: 0, b: 3 } } // => { x: { a: 0, b: 3 } }
``` ```
### Multiple updates, including an array #### Multiple updates, including an array
```js ```js
u({ x: { b: 3 }, y: { 1: 4 } }, { x: { a: 0, b: 0 }, y: [0, 0] }); u({ x: { b: 3 }, y: { 1: 4 } }, { x: { a: 0, b: 0 }, y: [0, 0] });
// => { x: { a: 0, b: 3 }, y: [0, 4] } // => { x: { a: 0, b: 3 }, y: [0, 4] }
``` ```
### Use a function #### Use a function
```js ```js
function inc(i) { return i + 1; } function inc(i) { return i + 1; }
@ -86,7 +90,7 @@ u({ x: { b: inc } }, { x: { a: 0, b: 0 } });
// => { x: { a: 0, b: 1 } } // => { x: { a: 0, b: 1 } }
``` ```
### Partial application #### Partial application
```js ```js
var setBTo3 = u({ b: 3 }); var setBTo3 = u({ b: 3 });
@ -94,14 +98,53 @@ setBTo3({ a: 0, b: 0 });
// => { a: 0, b: 3 }) // => { a: 0, b: 3 })
``` ```
### Remove a property #### ES6 computed properties
```js ```js
u({ x: u.omit('b') }, { x: { a: 0, b: 0 } }); var key = 'b';
u({ x: { [key]: 3 } }, { x: { a: 0, b: 0 } });
// => { x: { a: 0, b: 3 } }
```
### `u.in(path(, value)(, object))`
Update a single value with a simple string or array path.
```js
u.in('a.b', 3, { a: { b: 0 } });
// => { a: { b: 3 } };
```
```js
function inc(i) { return i + 1; }
u.in('a.b', inc, { a: { b: 0 } });
// => { a: { b: 1 } };
```
```js
u({
x: u.in(['a', 'b'], 3)
}, { x: { a: { b: 0 } } });
// => { x: { a: { b: 3 } } };
```
### `u.omit(predicate(, object))`
Remove properties. See [`_.omit`](https://lodash.com/docs#omit).
```js
u({ x: u.omit('b') }, { x: { a: 0, b: 0, c: 0 } });
// => { x: { a: 0, c: 0 } }
```
```js
u({ x: u.omit(['b', 'c']) }, { x: { a: 0, b: 0, c: 0 } });
// => { x: { a: 0 } } // => { x: { a: 0 } }
``` ```
### Reject an item from an array ### `u.reject(predicate(, object))`
Reject items from an array. See [`_.reject`](https://lodash.com/docs#reject).
```js ```js
function even(i) { return i % 2 === 0 }; function even(i) { return i % 2 === 0 };
@ -109,21 +152,15 @@ u({ x: u.reject(even) }, { x: [1, 2, 3, 4] });
// => { x: [1, 3] } // => { x: [1, 3] }
``` ```
### With a default ### `u.withDefault(default(, updates)(, object))`
Like `u()`, but start with the default value if the original value is undefined.
```js ```js
u({ x: withDefault([], { 0: 3 }) }, {}); u({ x: withDefault([], { 0: 3 }) }, {});
// => { x: [3] } // => { x: [3] }
``` ```
### ES6 computed properties
```js
var key = 'b';
u({ x: { [key]: 3 } }, { x: { a: 0, b: 0 } });
// => { x: { a: 0, b: 3 } }
```
See the [tests] for more examples. See the [tests] for more examples.
## Install ## Install

11
lib/updateIn.js Normal file
View File

@ -0,0 +1,11 @@
import curry from 'lodash/function/curry';
import update from './update';
function updateIn(path, value, obj) {
const parts = Array.isArray(path) ? path : path.split('.');
const updates = parts.reduceRight((acc, key) => ({ [key]: acc }), value);
return update(updates, obj);
}
export default curry(updateIn);

View File

@ -1,7 +1,7 @@
import { expect } from 'chai'; import { expect } from 'chai';
import freeze from '../lib/freeze'; import freeze from '../lib/freeze';
describe('freeze', () => { describe('u.freeze', () => {
afterEach(() => { afterEach(() => {
delete process.env.NODE_ENV; delete process.env.NODE_ENV;
}); });

35
test/in-spec.js Normal file
View File

@ -0,0 +1,35 @@
import { expect } from 'chai';
import updateIn from '../lib/updateIn';
describe('u.in', () => {
it('can update a single path described with a string', () => {
const obj = { a: { b: 0 } };
const result = updateIn('a.b', 3, obj);
expect(result).to.eql({ a: { b: 3 } });
});
it('can update a single path described with a string with a function', () => {
const inc = x => x + 1;
const obj = { a: { b: 0 } };
const result = updateIn('a.b', inc, obj);
expect(result).to.eql({ a: { b: 1 } });
});
it('can update a single path described with an array', () => {
const obj = { a: { b: 0 } };
const result = updateIn(['a', 'b'], 3, obj);
expect(result).to.eql({ a: { b: 3 } });
});
it('can update arrays', () => {
const obj = { a: [0, 0, 0] };
const result = updateIn('a.1', 3, obj);
expect(result).to.eql({ a: [0, 3, 0] });
});
it('can be partially applied', () => {
const obj = { a: { b: 0 } };
const result = updateIn('a.b')(3)(obj);
expect(result).to.eql({ a: { b: 3 } });
});
});