Rename obj to object

This commit is contained in:
Aaron Jensen 2015-08-05 00:27:56 -07:00
parent 1a1e2b313a
commit 78c30e038a
10 changed files with 116 additions and 116 deletions

View File

@ -24,7 +24,7 @@ functions with their parameters reversed (as [lodash-fp] do).
Note that the parameters may be backwards from what you are used to. updeep Note that the parameters may be backwards from what you are used to. updeep
supports [partial application][currying], so the parameter order is: supports [partial application][currying], so the parameter order is:
`updeep(updates, obj)`. `updeep(updates, object)`.
## API and Examples ## API and Examples
@ -134,13 +134,13 @@ Apply updates only if `predicate` is truthy or, if `predicate` is a function,
if it evaluates to truthy when called with `object`. if it evaluates to truthy when called with `object`.
```js ```js
var obj = { a: 2 }; var object = { a: 2 };
function isEven(x) { return x % 2 === 0; } function isEven(x) { return x % 2 === 0; }
function inc(x) { return x + 1; } function inc(x) { return x + 1; }
u({ u({
a: u.if(isEven, inc), a: u.if(isEven, inc),
}, obj); }, object);
// => { a: 3 } // => { a: 3 }
``` ```
@ -148,7 +148,7 @@ u({
If iteratee is a function, map it over the values in `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`, If it is an object, apply it as updates to each value in `object`,
which is equivalent to `u.map(u(...), obj)`). which is equivalent to `u.map(u(...), object)`).
```js ```js
function inc(x) { return x + 1; } function inc(x) { return x + 1; }

View File

@ -1,35 +1,35 @@
function isFreezable(obj) { function isFreezable(object) {
if (obj === null) return false; if (object === null) return false;
return Array.isArray(obj) || return Array.isArray(object) ||
typeof obj === 'object'; typeof object === 'object';
} }
function needsFreezing(obj) { function needsFreezing(object) {
return isFreezable(obj) && !Object.isFrozen(obj); return isFreezable(object) && !Object.isFrozen(object);
} }
function recur(obj) { function recur(object) {
Object.freeze(obj); Object.freeze(object);
Object.keys(obj).forEach((key) => { Object.keys(object).forEach((key) => {
const value = obj[key]; const value = object[key];
if (needsFreezing(value)) { if (needsFreezing(value)) {
recur(value); recur(value);
} }
}); });
return obj; return object;
} }
export default function freeze(obj) { export default function freeze(object) {
if (process.env.NODE_ENV === 'production') { if (process.env.NODE_ENV === 'production') {
return obj; return object;
} }
if (needsFreezing(obj)) { if (needsFreezing(object)) {
recur(obj); recur(object);
} }
return obj; return object;
} }

View File

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

View File

@ -9,8 +9,8 @@ import update from './update';
import curry from 'lodash/function/curry'; import curry from 'lodash/function/curry';
function updateAndFreeze(updates, obj) { function updateAndFreeze(updates, object) {
return freeze(update(updates, obj)); return freeze(update(updates, object));
} }
const updeep = curry(updateAndFreeze); const updeep = curry(updateAndFreeze);

View File

@ -2,16 +2,16 @@ import reduce from 'lodash/collection/reduce';
import isEmpty from 'lodash/lang/isEmpty'; import isEmpty from 'lodash/lang/isEmpty';
import assign from 'lodash/object/assign'; import assign from 'lodash/object/assign';
function resolveUpdates(updates, obj = {}) { function resolveUpdates(updates, object = {}) {
return reduce(updates, (acc, value, key) => { return reduce(updates, (acc, value, key) => {
let updatedValue = value; let updatedValue = value;
if (!Array.isArray(value) && value !== null && typeof value === 'object') { if (!Array.isArray(value) && value !== null && typeof value === 'object') {
updatedValue = update(value, obj[key]); updatedValue = update(value, object[key]);
} else if (typeof value === 'function') { } else if (typeof value === 'function') {
updatedValue = value(obj[key]); updatedValue = value(object[key]);
} }
if (obj[key] !== updatedValue) { if (object[key] !== updatedValue) {
acc[key] = updatedValue; acc[key] = updatedValue;
} }
@ -19,8 +19,8 @@ function resolveUpdates(updates, obj = {}) {
}, {}); }, {});
} }
function updateArray(updates, obj) { function updateArray(updates, object) {
const newObj = [...obj]; const newObj = [...object];
return reduce(updates, (acc, value, index) => { return reduce(updates, (acc, value, index) => {
acc[index] = value; acc[index] = value;
@ -43,22 +43,22 @@ function updateArray(updates, obj) {
* @param {Object|Array} object to update * @param {Object|Array} object to update
* @return {Object|Array} new object with modifications * @return {Object|Array} new object with modifications
*/ */
function update(updates, obj) { function update(updates, object) {
if (typeof updates === 'function') { if (typeof updates === 'function') {
return updates(obj); return updates(object);
} }
const resolvedUpdates = resolveUpdates(updates, obj); const resolvedUpdates = resolveUpdates(updates, object);
if (isEmpty(resolvedUpdates)) { if (isEmpty(resolvedUpdates)) {
return obj; return object;
} }
if (Array.isArray(obj)) { if (Array.isArray(object)) {
return updateArray(resolvedUpdates, obj); return updateArray(resolvedUpdates, object);
} }
return assign({}, obj, resolvedUpdates); return assign({}, object, resolvedUpdates);
} }
export default update; export default update;

View File

@ -7,59 +7,59 @@ describe('u.freeze', () => {
}); });
it('freezes objects', () => { it('freezes objects', () => {
const obj = {}; const object = {};
u.freeze(obj); u.freeze(object);
expect(Object.isFrozen(obj)).to.be.true; expect(Object.isFrozen(object)).to.be.true;
}); });
it('freezes nested objects', () => { it('freezes nested objects', () => {
const obj = { foo: { bar: 3 } }; const object = { foo: { bar: 3 } };
u.freeze(obj); u.freeze(object);
expect(Object.isFrozen(obj.foo)).to.be.true; expect(Object.isFrozen(object.foo)).to.be.true;
}); });
it('freezes nested arrays', () => { it('freezes nested arrays', () => {
const obj = [[0]]; const object = [[0]];
u.freeze(obj); u.freeze(object);
expect(Object.isFrozen(obj)).to.be.true; expect(Object.isFrozen(object)).to.be.true;
expect(Object.isFrozen(obj[0])).to.be.true; expect(Object.isFrozen(object[0])).to.be.true;
}); });
it('ignores functions', () => { it('ignores functions', () => {
const obj = { foo: () => 1 }; const object = { foo: () => 1 };
u.freeze(obj); u.freeze(object);
expect(Object.isFrozen(obj.foo)).to.be.false; expect(Object.isFrozen(object.foo)).to.be.false;
}); });
it('does not freeze children if the parent is already frozen', () => { it('does not freeze children if the parent is already frozen', () => {
const obj = { foo: {} }; const object = { foo: {} };
Object.freeze(obj); Object.freeze(object);
u.freeze(obj); u.freeze(object);
expect(Object.isFrozen(obj.foo)).to.be.false; expect(Object.isFrozen(object.foo)).to.be.false;
}); });
it('does not freeze in production', () => { it('does not freeze in production', () => {
process.env.NODE_ENV = 'production'; process.env.NODE_ENV = 'production';
const obj = {}; const object = {};
u.freeze(obj); u.freeze(object);
expect(Object.isFrozen(obj)).to.be.false; expect(Object.isFrozen(object)).to.be.false;
}); });
it('handles null objects', () => { it('handles null objects', () => {
const obj = { foo: null }; const object = { foo: null };
u.freeze(obj); u.freeze(object);
expect(Object.isFrozen(obj)).to.be.true; expect(Object.isFrozen(object)).to.be.true;
}); });
it('returns the same object', () => { it('returns the same object', () => {
const obj = {}; const object = {};
const result = u.freeze(obj); const result = u.freeze(object);
expect(result).to.equal(obj); expect(result).to.equal(object);
}); });
}); });

View File

@ -3,33 +3,33 @@ import u from '../lib';
describe('u.if', () => { describe('u.if', () => {
it('does not update if the predicate is false', () => { it('does not update if the predicate is false', () => {
const obj = { a: 0 }; const object = { a: 0 };
const result = u.if(false, { b: 1 }, obj); const result = u.if(false, { b: 1 }, object);
expect(result).to.eql(obj); expect(result).to.eql(object);
}); });
it('does update if the predicate is true', () => { it('does update if the predicate is true', () => {
const obj = { a: 0 }; const object = { a: 0 };
const result = u.if(true, { b: 1 }, obj); const result = u.if(true, { b: 1 }, object);
expect(result).to.eql({ a: 0, b: 1 }); expect(result).to.eql({ a: 0, b: 1 });
}); });
it('will use the result of a function passed as a predicate', () => { it('will use the result of a function passed as a predicate', () => {
const obj = { a: 0 }; const object = { a: 0 };
const aIsThree = x => x.a === 3; const aIsThree = x => x.a === 3;
const result = u.if(aIsThree, { b: 1 }, obj); const result = u.if(aIsThree, { b: 1 }, object);
expect(result).to.eql({ a: 0 }); expect(result).to.eql({ a: 0 });
}); });
it('can be partially applied', () => { it('can be partially applied', () => {
const obj = { a: 2 }; const object = { a: 2 };
const isEven = x => x % 2 === 0; const isEven = x => x % 2 === 0;
const inc = x => x + 1; const inc = x => x + 1;
const result = u({ const result = u({
a: u.if(isEven, inc), a: u.if(isEven, inc),
}, obj); }, object);
expect(result).to.eql({ a: 3 }); expect(result).to.eql({ a: 3 });
}); });

View File

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

View File

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

View File

@ -3,84 +3,84 @@ import u from '../lib';
describe('updeep', () => { describe('updeep', () => {
it('does not change anything if no updates are specified', () => { it('does not change anything if no updates are specified', () => {
const obj = { foo: 3, bar: [7, 5] }; const object = { foo: 3, bar: [7, 5] };
const result = u({}, obj); const result = u({}, object);
expect(result).to.equal(obj); expect(result).to.equal(object);
}); });
it('can update with fixed values', () => { it('can update with fixed values', () => {
const obj = { foo: 3, bar: [7, 5] }; const object = { foo: 3, bar: [7, 5] };
const result = u({ foo: 4 }, obj); const result = u({ foo: 4 }, object);
expect(result).to.deep.equal({ foo: 4, bar: [7, 5] }); expect(result).to.deep.equal({ foo: 4, bar: [7, 5] });
}); });
it('returns the same instance if an update doesn\'t make changes', () => { it('returns the same instance if an update doesn\'t make changes', () => {
const obj = { foo: 3 }; const object = { foo: 3 };
const result = u({ foo: 3 }, obj); const result = u({ foo: 3 }, object);
expect(result).to.equal(obj); expect(result).to.equal(object);
}); });
it('can update a nested structure', () => { it('can update a nested structure', () => {
const obj = { foo: { bar: 7, bam: 3 }, baz: 32 }; const object = { foo: { bar: 7, bam: 3 }, baz: 32 };
const result = u({ foo: { bar: 8 } }, obj); const result = u({ foo: { bar: 8 } }, object);
expect(result).to.deep.equal({ foo: { bar: 8, bam: 3 }, baz: 32 }); expect(result).to.deep.equal({ foo: { bar: 8, bam: 3 }, baz: 32 });
}); });
it('can update arrays', () => { it('can update arrays', () => {
const obj = [1, 2, 3]; const object = [1, 2, 3];
const result = u({ 1: 7 }, obj); const result = u({ 1: 7 }, object);
expect(result).to.deep.equal([1, 7, 3]); expect(result).to.deep.equal([1, 7, 3]);
}); });
it('can add an element to an array', () => { it('can add an element to an array', () => {
const obj = []; const object = [];
const result = u({ 0: 3 }, obj); const result = u({ 0: 3 }, object);
expect(result).to.eql([3]); expect(result).to.eql([3]);
}); });
it('can update nested arrays', () => { it('can update nested arrays', () => {
const obj = { foo: [1, 2, 3], bar: 9 }; const object = { foo: [1, 2, 3], bar: 9 };
const result = u({ foo: { 1: 7 } }, obj); const result = u({ foo: { 1: 7 } }, object);
expect(result).to.deep.equal({ foo: [1, 7, 3], bar: 9 }); expect(result).to.deep.equal({ foo: [1, 7, 3], bar: 9 });
}); });
it('can use functions to update values', () => { it('can use functions to update values', () => {
const inc = (i) => i + 1; const inc = (i) => i + 1;
const obj = { foo: 3, bar: 4, baz: 7 }; const object = { foo: 3, bar: 4, baz: 7 };
const result = u({ foo: inc, bar: inc }, obj); const result = u({ foo: inc, bar: inc }, object);
expect(result).to.deep.equal({ foo: 4, bar: 5, baz: 7 }); expect(result).to.deep.equal({ foo: 4, bar: 5, baz: 7 });
}); });
it('can be partially applied', () => { it('can be partially applied', () => {
const inc = (i) => i + 1; const inc = (i) => i + 1;
const obj = { foo: 3 }; const object = { foo: 3 };
const incFoo = u({ foo: inc }); const incFoo = u({ foo: inc });
const result = incFoo(obj); const result = incFoo(object);
expect(result).to.deep.equal({ foo: 4 }); expect(result).to.deep.equal({ foo: 4 });
}); });
it('can update if the value is an array', () => { it('can update if the value is an array', () => {
const obj = {}; const object = {};
const result = u({ foo: [0, 1] }, obj); const result = u({ foo: [0, 1] }, object);
expect(result).to.deep.equal({ foo: [0, 1] }); expect(result).to.deep.equal({ foo: [0, 1] });
}); });
it('can use withDefault to default things', () => { it('can use withDefault to default things', () => {
const obj = {}; const object = {};
const result = u({ const result = u({
foo: u.withDefault([], { 0: 'bar' }), foo: u.withDefault([], { 0: 'bar' }),
}, obj); }, object);
expect(result).to.eql({ foo: ['bar'] }); expect(result).to.eql({ foo: ['bar'] });
}); });