2015-08-19 07:23:18 +00:00
|
|
|
import { expect } from 'chai';
|
2015-08-05 07:25:34 +00:00
|
|
|
import u from '../lib';
|
|
|
|
|
|
|
|
describe('u.map', () => {
|
|
|
|
it('applies updates to each item in an array', () => {
|
2015-08-05 07:27:56 +00:00
|
|
|
const object = [0, 1, 2];
|
2015-08-05 07:25:34 +00:00
|
|
|
const inc = x => x + 1;
|
2015-08-05 07:27:56 +00:00
|
|
|
const result = u.map(inc, object);
|
2015-08-05 07:25:34 +00:00
|
|
|
|
2015-08-19 07:23:18 +00:00
|
|
|
expect(result).to.eql([1, 2, 3]);
|
2015-08-05 07:25:34 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('applies updates to each value in an object', () => {
|
2015-08-05 07:27:56 +00:00
|
|
|
const object = { a: 0, b: 1, c: 2 };
|
2015-08-05 07:25:34 +00:00
|
|
|
const inc = x => x + 1;
|
2015-08-05 07:27:56 +00:00
|
|
|
const result = u.map(inc, object);
|
2015-08-05 07:25:34 +00:00
|
|
|
|
2015-08-19 07:23:18 +00:00
|
|
|
expect(result).to.eql({ a: 1, b: 2, c: 3 });
|
2015-08-05 07:25:34 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('can update with a regular updates object', () => {
|
2015-08-05 07:27:56 +00:00
|
|
|
const object = [{ a: 0 }, { a: 0 }];
|
|
|
|
const result = u.map({ a: 1 }, object);
|
2015-08-05 07:25:34 +00:00
|
|
|
|
2015-08-19 07:23:18 +00:00
|
|
|
expect(result).to.eql([{ a: 1 }, { a: 1 }]);
|
2015-08-05 07:25:34 +00:00
|
|
|
});
|
|
|
|
|
2015-08-09 07:21:24 +00:00
|
|
|
it('returns the same object if no updates are made', () => {
|
|
|
|
const array = [0, 1];
|
|
|
|
const ident = x => x;
|
|
|
|
let result = u.map(ident, array);
|
|
|
|
|
2015-08-19 07:23:18 +00:00
|
|
|
expect(result).to.equal(array);
|
2015-08-09 07:21:24 +00:00
|
|
|
|
|
|
|
const object = { a: 0 };
|
|
|
|
result = u.map(ident, object);
|
|
|
|
|
2015-08-19 07:23:18 +00:00
|
|
|
expect(result).to.equal(object);
|
2015-08-09 07:21:24 +00:00
|
|
|
});
|
|
|
|
|
2015-08-05 07:25:34 +00:00
|
|
|
it('passes the key or index as the second parameter to the iteratee', () => {
|
2015-08-05 07:27:56 +00:00
|
|
|
const object = {
|
2015-08-05 07:25:34 +00:00
|
|
|
a: { x: 0 },
|
|
|
|
b: [3, 3],
|
|
|
|
};
|
|
|
|
const setToKey = (_, key) => key;
|
2015-08-05 07:27:56 +00:00
|
|
|
const result = u.map(u.map(setToKey), object);
|
2015-08-05 07:25:34 +00:00
|
|
|
|
2015-08-19 07:23:18 +00:00
|
|
|
expect(result).to.eql( {
|
2015-08-05 07:25:34 +00:00
|
|
|
a: { x: 'x' },
|
|
|
|
b: [0, 1],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can be partially applied', () => {
|
2015-08-05 07:27:56 +00:00
|
|
|
const object = {
|
2015-08-05 07:25:34 +00:00
|
|
|
b: [3, 3],
|
|
|
|
};
|
|
|
|
const setToKey = (_, key) => key;
|
|
|
|
const result = u({
|
|
|
|
b: u.map(setToKey),
|
2015-08-05 07:27:56 +00:00
|
|
|
}, object);
|
2015-08-05 07:25:34 +00:00
|
|
|
|
2015-08-19 07:23:18 +00:00
|
|
|
expect(result).to.eql( {
|
2015-08-05 07:25:34 +00:00
|
|
|
b: [0, 1],
|
|
|
|
});
|
|
|
|
});
|
2015-08-05 11:29:30 +00:00
|
|
|
|
|
|
|
it('freezes the result', () => {
|
2015-08-19 07:23:18 +00:00
|
|
|
expect(Object.isFrozen(u.map({}, {}))).to.be.true;
|
2015-08-05 11:29:30 +00:00
|
|
|
});
|
2015-08-05 07:25:34 +00:00
|
|
|
});
|