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
supports [partial application][currying], so the parameter order is:
`updeep(updates, obj)`.
`updeep(updates, object)`.
## 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`.
```js
var obj = { a: 2 };
var object = { a: 2 };
function isEven(x) { return x % 2 === 0; }
function inc(x) { return x + 1; }
u({
a: u.if(isEven, inc),
}, obj);
}, object);
// => { a: 3 }
```
@ -148,7 +148,7 @@ u({
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)`).
which is equivalent to `u.map(u(...), object)`).
```js
function inc(x) { return x + 1; }

View File

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

View File

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

View File

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

View File

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

View File

@ -7,59 +7,59 @@ describe('u.freeze', () => {
});
it('freezes objects', () => {
const obj = {};
u.freeze(obj);
const object = {};
u.freeze(object);
expect(Object.isFrozen(obj)).to.be.true;
expect(Object.isFrozen(object)).to.be.true;
});
it('freezes nested objects', () => {
const obj = { foo: { bar: 3 } };
u.freeze(obj);
const object = { foo: { bar: 3 } };
u.freeze(object);
expect(Object.isFrozen(obj.foo)).to.be.true;
expect(Object.isFrozen(object.foo)).to.be.true;
});
it('freezes nested arrays', () => {
const obj = [[0]];
u.freeze(obj);
const object = [[0]];
u.freeze(object);
expect(Object.isFrozen(obj)).to.be.true;
expect(Object.isFrozen(obj[0])).to.be.true;
expect(Object.isFrozen(object)).to.be.true;
expect(Object.isFrozen(object[0])).to.be.true;
});
it('ignores functions', () => {
const obj = { foo: () => 1 };
u.freeze(obj);
const object = { foo: () => 1 };
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', () => {
const obj = { foo: {} };
Object.freeze(obj);
u.freeze(obj);
const object = { foo: {} };
Object.freeze(object);
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', () => {
process.env.NODE_ENV = 'production';
const obj = {};
u.freeze(obj);
const object = {};
u.freeze(object);
expect(Object.isFrozen(obj)).to.be.false;
expect(Object.isFrozen(object)).to.be.false;
});
it('handles null objects', () => {
const obj = { foo: null };
u.freeze(obj);
expect(Object.isFrozen(obj)).to.be.true;
const object = { foo: null };
u.freeze(object);
expect(Object.isFrozen(object)).to.be.true;
});
it('returns the same object', () => {
const obj = {};
const result = u.freeze(obj);
expect(result).to.equal(obj);
const object = {};
const result = u.freeze(object);
expect(result).to.equal(object);
});
});

View File

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

View File

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

View File

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

View File

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