Rename u.in to u.updateIn

With u.if and u.is, it was too confusing. Originally I thought that because
u stood for update, it would make sense, but as I add more functions,
I think that was wrong.
This commit is contained in:
Aaron Jensen 2015-08-06 20:52:06 -07:00
parent 5c0855b9df
commit 333ca47cfd
5 changed files with 16 additions and 15 deletions

View File

@ -2,6 +2,7 @@
## [unreleased]
* Add `u.is` to test predicates in a single path. (https://github.com/substantial/updeep/issues/13)
* Rename `u.in` to `u.updateIn`. With `u.is` and `u.if` it was too confusing.
## [0.4.0]
* Add `u.if` and `u.ifElse` to conditionally update objects. (https://github.com/substantial/updeep/issues/12)

View File

@ -117,24 +117,24 @@ u({ x: { [key]: 3 } }, { x: { a: 0, b: 0 } });
// => { x: { a: 0, b: 3 } }
```
### `u.in(path(, value)(, object))`
### `u.updateIn(path(, value)(, object))`
Update a single value with a simple string or array path.
```js
u.in('a.b', 3, { a: { b: 0 } });
u.updateIn('a.b', 3, { a: { b: 0 } });
// => { a: { b: 3 } };
```
```js
function inc(i) { return i + 1; }
u.in('a.b', inc, { a: { b: 0 } });
u.updateIn('a.b', inc, { a: { b: 0 } });
// => { a: { b: 1 } };
```
```js
u({
x: u.in(['a', 'b'], 3)
x: u.updateIn(['a', 'b'], 3)
}, { x: { a: { b: 0 } } });
// => { x: { a: { b: 3 } } };
```
@ -253,7 +253,7 @@ u.is('a.b', isEven, { a: { b: 4 } });
```js
u({
person: u.if(u.is('name.first', 'Jen'), u.in('name.last', 'Simpson'))
person: u.if(u.is('name.first', 'Jen'), u.updateIn('name.last', 'Simpson'))
}, { person: { name: { first: 'Jen', last: 'Matthews' } } });
// => { person: { name: { first: 'Jen', last: 'Simpson' } } }
```

View File

@ -1,5 +1,5 @@
import freeze from './freeze';
import _in from './in';
import updateIn from './updateIn';
import is from './is';
import ifElse from './ifElse';
import map from './map';
@ -14,7 +14,7 @@ const u = update;
u.if = ifElse(_, _, {});
u.ifElse = ifElse;
u.in = _in;
u.updateIn = updateIn;
u.is = is;
u.freeze = freeze;
u.map = map;

View File

@ -1,45 +1,45 @@
import { expect } from 'chai';
import u from '../lib';
describe('u.in', () => {
describe('u.updateIn', () => {
it('can update a single path described with a string', () => {
const object = { a: { b: 0 } };
const result = u.in('a.b', 3, object);
const result = u.updateIn('a.b', 3, object);
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 object = { a: { b: 0 } };
const result = u.in('a.b', inc, object);
const result = u.updateIn('a.b', inc, object);
expect(result).to.eql({ a: { b: 1 } });
});
it('can update a single path described with an array', () => {
const object = { a: { b: 0 } };
const result = u.in(['a', 'b'], 3, object);
const result = u.updateIn(['a', 'b'], 3, object);
expect(result).to.eql({ a: { b: 3 } });
});
it('can update arrays', () => {
const object = { a: [0, 0, 0] };
const result = u.in('a.1', 3, object);
const result = u.updateIn('a.1', 3, object);
expect(result).to.eql({ a: [0, 3, 0] });
});
it('can be partially applied', () => {
const object = { a: { b: 0 } };
const result = u.in('a.b')(3)(object);
const result = u.updateIn('a.b')(3)(object);
expect(result).to.eql({ a: { b: 3 } });
});
it('replaces the object outright if the path is empty', () => {
const object = {};
const result = u.in('', 3, object);
const result = u.updateIn('', 3, object);
expect(result).to.equal(3);
});
it('freezes the result', () => {
expect(Object.isFrozen(u.in('a', 0, {}))).to.be.true;
expect(Object.isFrozen(u.updateIn('a', 0, {}))).to.be.true;
});
});