Add u.map

main
Aaron Jensen 2015-08-05 00:25:34 -07:00
parent 750200df76
commit 1a1e2b313a
5 changed files with 106 additions and 2 deletions

View File

@ -2,6 +2,7 @@
## [unreleased]
* Add `u.if` to conditionally update objects.
* Add `u.map` to update all values in an array or object.
## [0.3.1]
* Actually expose `u.in`.

View File

@ -144,6 +144,34 @@ u({
// => { a: 3 }
```
### `u.map(iteratee(, object))`
If iteratee is a function, map it over the values in `object`.
If it is an object, apply it as updates to each value in `object`,
which is equivalent to `u.map(u(...), obj)`).
```js
function inc(x) { return x + 1; }
u({
a: u.map(inc)
}, { a: [0, 1] });
// => { a: [1, 2] }
```
```js
function inc(x) { return x + 1; }
u.map(inc, [0, 1, 2]);
// => [1, 2, 3]
u.map(inc, { a: 0, b: 1, c: 2});
// => { a: 1, b: 2, c: 3}
```
```js
u.map({ a: 2 }, [{ a: 0 }, { a: 1 }]);
// => [{ a: 2 }, { a: 2 }]
```
### `u.omit(predicate(, object))`
Remove properties. See [`_.omit`](https://lodash.com/docs#omit).

View File

@ -1,10 +1,12 @@
import update from './update';
import freeze from './freeze';
import _in from './in';
import _if from './if';
import map from './map';
import omit from './omit';
import reject from './reject';
import withDefault from './withDefault';
import freeze from './freeze';
import update from './update';
import curry from 'lodash/function/curry';
function updateAndFreeze(updates, obj) {
@ -16,6 +18,7 @@ const updeep = curry(updateAndFreeze);
updeep.if = _if;
updeep.in = _in;
updeep.freeze = freeze;
updeep.map = map;
updeep.omit = omit;
updeep.reject = reject;
updeep.withDefault = withDefault;

17
lib/map.js Normal file
View File

@ -0,0 +1,17 @@
import curry from 'lodash/function/curry';
import mapValues from 'lodash/object/mapValues';
import update from './update';
function map(iteratee, object) {
const updater = typeof iteratee === 'function' ?
iteratee :
val => update(iteratee, val);
if (Array.isArray(object)) {
return object.map(updater);
}
return mapValues(object, updater);
}
export default curry(map);

55
test/map-spec.js Normal file
View File

@ -0,0 +1,55 @@
import { expect } from 'chai';
import u from '../lib';
describe('u.map', () => {
it('applies updates to each item in an array', () => {
const obj = [0, 1, 2];
const inc = x => x + 1;
const result = u.map(inc, obj);
expect(result).to.eql([1, 2, 3]);
});
it('applies updates to each value in an object', () => {
const obj = { a: 0, b: 1, c: 2 };
const inc = x => x + 1;
const result = u.map(inc, obj);
expect(result).to.eql({ a: 1, b: 2, c: 3 });
});
it('can update with a regular updates object', () => {
const obj = [{ a: 0 }, { a: 0 }];
const result = u.map({ a: 1 }, obj);
expect(result).to.eql([{ a: 1 }, { a: 1 }]);
});
it('passes the key or index as the second parameter to the iteratee', () => {
const obj = {
a: { x: 0 },
b: [3, 3],
};
const setToKey = (_, key) => key;
const result = u.map(u.map(setToKey), obj);
expect(result).to.eql( {
a: { x: 'x' },
b: [0, 1],
});
});
it('can be partially applied', () => {
const obj = {
b: [3, 3],
};
const setToKey = (_, key) => key;
const result = u({
b: u.map(setToKey),
}, obj);
expect(result).to.eql( {
b: [0, 1],
});
});
});