diff --git a/lib/update.js b/lib/update.js index 507bff9..72c85c9 100644 --- a/lib/update.js +++ b/lib/update.js @@ -1,8 +1,10 @@ import wrap from './wrap'; -import isEmpty from './util/isEmpty'; -import defaultObject from './util/defaultObject'; import isPlainObject from 'lodash/lang/isPlainObject'; +function isEmpty(object) { + return !Object.keys(object).length; +} + function reduce(object, callback, initialValue) { return Object.keys(object).reduce((acc, key) => { return callback(acc, object[key], key); @@ -63,7 +65,9 @@ function update(updates, object, ...args) { return updates; } - const defaultedObject = defaultObject(object, updates); + const defaultedObject = (typeof object === 'undefined' || object === null) ? + {} : + object; const resolvedUpdates = resolveUpdates(updates, defaultedObject); diff --git a/lib/util/defaultObject.js b/lib/util/defaultObject.js deleted file mode 100644 index be6286f..0000000 --- a/lib/util/defaultObject.js +++ /dev/null @@ -1,35 +0,0 @@ -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; diff --git a/lib/util/isEmpty.js b/lib/util/isEmpty.js deleted file mode 100644 index 4e27ba2..0000000 --- a/lib/util/isEmpty.js +++ /dev/null @@ -1,5 +0,0 @@ -function isEmpty(object) { - return !Object.keys(object).length; -} - -export default isEmpty; diff --git a/test/updateIn-spec.js b/test/updateIn-spec.js index b7c9cc4..a89ca97 100644 --- a/test/updateIn-spec.js +++ b/test/updateIn-spec.js @@ -27,12 +27,6 @@ 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); diff --git a/test/updeep-spec.js b/test/updeep-spec.js index 17ece4b..22ea60f 100644 --- a/test/updeep-spec.js +++ b/test/updeep-spec.js @@ -42,18 +42,6 @@ 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);