Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default to array #34

Merged
merged 1 commit into from
Aug 31, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions lib/update.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import wrap from './wrap';

function isEmpty(object) {
return !Object.keys(object).length;
}
import isEmpty from './util/isEmpty';
import defaultObject from './util/defaultObject';

function reduce(object, callback, initialValue) {
return Object.keys(object).reduce((acc, key) => {
Expand Down Expand Up @@ -64,9 +62,7 @@ function update(updates, object, ...args) {
return updates;
}

const defaultedObject = (typeof object === 'undefined' || object === null) ?
{} :
object;
const defaultedObject = defaultObject(object, updates);

const resolvedUpdates = resolveUpdates(updates, defaultedObject);

Expand Down
35 changes: 35 additions & 0 deletions lib/util/defaultObject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import isEmpty from './isEmpty';

function isInt(value) {
if (isNaN(value)) {
return false;
}
const x = parseFloat(value);
return (x | 0) === x;
}

function isArrayUpdate(updates) {
for (const updateKey of Object.keys(updates)) {
if (!isInt(updateKey)) { return false; }
}

return true;
}

function arrayOrObject(updates) {
if (!isEmpty(updates) && isArrayUpdate(updates)) {
return [];
}

return {};
}

function defaultObject(object, updates) {
if (typeof object === 'undefined' || object === null) {
return arrayOrObject(updates);
}

return object;
}

export default defaultObject;
5 changes: 5 additions & 0 deletions lib/util/isEmpty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function isEmpty(object) {
return !Object.keys(object).length;
}

export default isEmpty;
6 changes: 6 additions & 0 deletions test/updateIn-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ describe('u.updateIn', () => {
expect(result).to.eql({ a: [0, 3, 0] });
});

it('can create array if all keys are numbers', () => {
const result = u.updateIn('a.0', 3, null);

expect(result).to.eql({ a: [3] });
});

it('can be partially applied', () => {
const object = { a: { b: 0 } };
const result = u.updateIn('a.b')(3)(object);
Expand Down
12 changes: 12 additions & 0 deletions test/updeep-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ describe('updeep', () => {
expect(u(null, {})).to.be.null;
});

it('can create array if all keys are numbers', () => {
const result = u({ 0: 'hi', '1': 'world' }, null);

expect(result).to.eql(['hi', 'world']);
});

it('does not create array if any key is not number', () => {
const result = u({ 0: 'hi', '1a': 'world' }, null);

expect(result).to.eql({ 0: 'hi', '1a': 'world' });
});

it('can add an element to an array', () => {
const object = [];
const result = u({ 0: 3 }, object);
Expand Down