parent
6ee1e3346b
commit
ddd5c84d49
@ -1,7 +1,7 @@
|
|||||||
# Change Log
|
# Change Log
|
||||||
|
|
||||||
## [unreleased]
|
## [unreleased]
|
||||||
* Add `u.if` to conditionally update objects.
|
* Add `u.if` and `u.ifElse` to conditionally update objects.
|
||||||
* Add `u.map` to update all values in an array or object.
|
* Add `u.map` to update all values in an array or object.
|
||||||
* Replace object outright if null or constant provided as `updates`.
|
* Replace object outright if null or constant provided as `updates`.
|
||||||
* Freeze objects returned by helper methods that use `update` like `withDefault`, `map`, `in`, etc. Previously, only `u` did freezing.
|
* Freeze objects returned by helper methods that use `update` like `withDefault`, `map`, `in`, etc. Previously, only `u` did freezing.
|
||||||
|
19
README.md
19
README.md
@ -140,7 +140,7 @@ u({
|
|||||||
|
|
||||||
### `u.if(predicate(, updates)(, object))`
|
### `u.if(predicate(, updates)(, object))`
|
||||||
|
|
||||||
Apply updates only if `predicate` is truthy or, if `predicate` is a function,
|
Apply `updates` only if `predicate` is truthy or, if `predicate` is a function,
|
||||||
it evaluates to truthy when called with `object`.
|
it evaluates to truthy when called with `object`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
@ -154,6 +154,23 @@ u({
|
|||||||
// => { a: 3 }
|
// => { a: 3 }
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### `u.ifElse(predicate(, trueUpdates)(, falseUpdates)(, object))`
|
||||||
|
|
||||||
|
Apply `trueUpdates` if `predicate` is truthy or, if `predicate` is a function,
|
||||||
|
it evaluates to truthy when called with `object`. Otherwise, apply `falseUpdates`.
|
||||||
|
|
||||||
|
```js
|
||||||
|
var object = { a: 3 };
|
||||||
|
function isEven(x) { return x % 2 === 0; }
|
||||||
|
function inc(x) { return x + 1; }
|
||||||
|
function dec(x) { return x - 1; }
|
||||||
|
|
||||||
|
u({
|
||||||
|
a: u.if(isEven, inc),
|
||||||
|
}, object);
|
||||||
|
// => { a: 2 }
|
||||||
|
```
|
||||||
|
|
||||||
### `u.map(iteratee(, object))`
|
### `u.map(iteratee(, object))`
|
||||||
|
|
||||||
If iteratee is a function, map it over the values in `object`.
|
If iteratee is a function, map it over the values in `object`.
|
||||||
|
@ -1,16 +1,14 @@
|
|||||||
import update from './update';
|
import update from './update';
|
||||||
import wrap from './wrap';
|
import wrap from './wrap';
|
||||||
|
|
||||||
function updateIf(predicate, updates, object) {
|
function updateIfElse(predicate, trueUpdates, falseUpdates, object) {
|
||||||
const test = typeof predicate === 'function' ?
|
const test = typeof predicate === 'function' ?
|
||||||
predicate(object) :
|
predicate(object) :
|
||||||
predicate;
|
predicate;
|
||||||
|
|
||||||
if (!test) {
|
const updates = test ? trueUpdates : falseUpdates;
|
||||||
return object;
|
|
||||||
}
|
|
||||||
|
|
||||||
return update(updates, object);
|
return update(updates, object);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default wrap(updateIf);
|
export default wrap(updateIfElse);
|
@ -1,15 +1,18 @@
|
|||||||
import freeze from './freeze';
|
import freeze from './freeze';
|
||||||
import _in from './in';
|
import _in from './in';
|
||||||
import _if from './if';
|
import ifElse from './ifElse';
|
||||||
import map from './map';
|
import map from './map';
|
||||||
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 update from './update';
|
import update from './update';
|
||||||
|
|
||||||
|
import { placeholder as _ } from 'lodash/function/curry';
|
||||||
|
|
||||||
const u = update;
|
const u = update;
|
||||||
|
|
||||||
u.if = _if;
|
u.if = ifElse(_, _, {});
|
||||||
|
u.ifElse = ifElse;
|
||||||
u.in = _in;
|
u.in = _in;
|
||||||
u.freeze = freeze;
|
u.freeze = freeze;
|
||||||
u.map = map;
|
u.map = map;
|
||||||
|
42
test/ifElse-spec.js
Normal file
42
test/ifElse-spec.js
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
import { expect } from 'chai';
|
||||||
|
import u from '../lib';
|
||||||
|
|
||||||
|
describe('u.ifElse', () => {
|
||||||
|
it('does updates with the else if the predicate is false', () => {
|
||||||
|
const object = { a: 0 };
|
||||||
|
const result = u.ifElse(false, { b: 1 }, { b: 2 }, object);
|
||||||
|
expect(result).to.eql({ a: 0, b: 2 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('updates with the true update if the predicate is true', () => {
|
||||||
|
const object = { a: 0 };
|
||||||
|
const result = u.ifElse(true, { b: 1 }, { b: 4 }, object);
|
||||||
|
expect(result).to.eql({ a: 0, b: 1 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('will use the result of a function passed as a predicate', () => {
|
||||||
|
const object = { a: 0 };
|
||||||
|
const aIsThree = x => x.a === 3;
|
||||||
|
const result = u.ifElse(aIsThree, { b: 1 }, { b: 4 }, object);
|
||||||
|
|
||||||
|
expect(result).to.eql({ a: 0, b: 4 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can be partially applied', () => {
|
||||||
|
const object = { a: 2 };
|
||||||
|
const isEven = x => x % 2 === 0;
|
||||||
|
const inc = x => x + 1;
|
||||||
|
const dec = x => x - 1;
|
||||||
|
|
||||||
|
const result = u({
|
||||||
|
a: u.ifElse(isEven, inc, dec),
|
||||||
|
}, object);
|
||||||
|
|
||||||
|
expect(result).to.eql({ a: 3 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('freezes the result', () => {
|
||||||
|
expect(Object.isFrozen(u.ifElse(true, {}, {}, {}))).to.be.true;
|
||||||
|
expect(Object.isFrozen(u.ifElse(false, {}, {}, {}))).to.be.true;
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user