diff --git a/src/array/concat.js b/src/array/concat.js index 913d1870..11198823 100644 --- a/src/array/concat.js +++ b/src/array/concat.js @@ -1,5 +1,5 @@ import _concat from 'lodash/concat' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Replaces an array concatenating the former array with additional arrays and/or values. diff --git a/src/array/convertArrayMethod.js b/src/array/convertArrayMethod.js index ec15d6b3..43883faf 100644 --- a/src/array/convertArrayMethod.js +++ b/src/array/convertArrayMethod.js @@ -1,20 +1,22 @@ -import { convert } from 'util/convert' -import isArray from 'lodash/isArray' -import toArray from 'lodash/toArray' +import { convert } from 'core/convert' + +const copyArray = array => { + if (array === undefined || array === null) return [] + if (Array.isArray(array)) return [...array] + return [array] +} /** * Converts an Array method. * @memberof array * @param {string} method Array method name. * @return {function} Returns the wrapped function. - * @see {@link util.convert|convert} for more information. - * @see {@link https://lodash.com/docs#isArray|lodash.isArray} for more information. - * @see {@link https://lodash.com/docs#toArray|lodash.toArray} for more information. + * @see {@link core.convert|convert} for more information. * @since 0.2.0 * @private */ const convertArrayMethod = method => convert((array, ...args) => { - const newArray = isArray(array) ? array : toArray(array) + const newArray = copyArray(array) newArray[method](...args) return newArray }) diff --git a/src/array/difference.js b/src/array/difference.js index 7a197570..ac2f1c72 100644 --- a/src/array/difference.js +++ b/src/array/difference.js @@ -1,5 +1,5 @@ import _difference from 'lodash/difference' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Replaces an array removing values in the other given arrays from the former array. diff --git a/src/array/differenceBy.js b/src/array/differenceBy.js index 84db85e3..84165c9a 100644 --- a/src/array/differenceBy.js +++ b/src/array/differenceBy.js @@ -1,5 +1,5 @@ import _differenceBy from 'lodash/differenceBy' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * This method is like {@link array.difference} except that it uses iteratee to generate the value to be compared for each element. diff --git a/src/array/differenceWith.js b/src/array/differenceWith.js index eef4b812..cb758ca0 100644 --- a/src/array/differenceWith.js +++ b/src/array/differenceWith.js @@ -1,5 +1,5 @@ import _differenceWith from 'lodash/differenceWith' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * This method is like {@link array.difference} except that it uses comparator to compare elements of the former array to values. diff --git a/src/array/drop.js b/src/array/drop.js index 76fa4f78..a2b2dd25 100644 --- a/src/array/drop.js +++ b/src/array/drop.js @@ -1,5 +1,5 @@ import _drop from 'lodash/drop' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Replaces an array dropping one or several elements at the start of the former array. diff --git a/src/array/dropRight.js b/src/array/dropRight.js index 28a63d94..57660fcb 100644 --- a/src/array/dropRight.js +++ b/src/array/dropRight.js @@ -1,5 +1,5 @@ import _dropRight from 'lodash/dropRight' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Replaces an array dropping one or several elements at the end of the former array. diff --git a/src/array/dropRightWhile.js b/src/array/dropRightWhile.js index 2c0107bc..483af12f 100644 --- a/src/array/dropRightWhile.js +++ b/src/array/dropRightWhile.js @@ -1,5 +1,5 @@ import _dropRightWhile from 'lodash/dropRightWhile' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Replaces an array excluding elements dropped from the end. Elements are dropped until predicate returns falsey. diff --git a/src/array/dropWhile.js b/src/array/dropWhile.js index 6f1e2eb8..27ca4ccf 100644 --- a/src/array/dropWhile.js +++ b/src/array/dropWhile.js @@ -1,5 +1,5 @@ import _dropWhile from 'lodash/dropWhile' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Replaces an array excluding elements dropped from the beginning. Elements are dropped until predicate returns falsey. diff --git a/src/array/fill.js b/src/array/fill.js index 04a03ae0..4491abf0 100644 --- a/src/array/fill.js +++ b/src/array/fill.js @@ -1,5 +1,5 @@ -import _fill from 'lodash/fill.js' -import { convert } from 'util/convert' +import _fill from 'lodash/fp/fill' +import { convertLodashFp } from 'util/convert' /** * Replaces by an array filled with value from start up to, but not including, end. @@ -15,5 +15,5 @@ import { convert } from 'util/convert' * @see {@link https://lodash.com/docs#fill|lodash.fill} for more information. * @since 0.3.0 */ -const fill = convert(_fill) +const fill = convertLodashFp(_fill) export { fill } diff --git a/src/array/intersection.js b/src/array/intersection.js index bb72e1dd..b8013e55 100644 --- a/src/array/intersection.js +++ b/src/array/intersection.js @@ -1,5 +1,5 @@ import _intersection from 'lodash/intersection' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Replaces by an array of unique values that are included in th former array and all given arrays. diff --git a/src/array/intersectionBy.js b/src/array/intersectionBy.js index dcc0bc66..49a0550e 100644 --- a/src/array/intersectionBy.js +++ b/src/array/intersectionBy.js @@ -1,5 +1,5 @@ import _intersectionBy from 'lodash/intersectionBy' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * This method is like {@link array.intersection} except that it uses iteratee to generate the value to be compared for each element. diff --git a/src/array/intersectionWith.js b/src/array/intersectionWith.js index 37368ce3..51e395d9 100644 --- a/src/array/intersectionWith.js +++ b/src/array/intersectionWith.js @@ -1,5 +1,5 @@ import _intersectionWith from 'lodash/intersectionWith' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * This method is like {@link array.intersection} except that it uses comparator to compare elements of the former array to arrays. diff --git a/src/array/pull.js b/src/array/pull.js index 066775de..96700003 100644 --- a/src/array/pull.js +++ b/src/array/pull.js @@ -1,5 +1,5 @@ -import _pull from 'lodash/pull' -import { convert } from 'util/convert' +import _pull from 'lodash/fp/pull' +import { convertLodashFp } from 'util/convert' /** * Replaces an array removing all given values from the former array. @@ -13,5 +13,5 @@ import { convert } from 'util/convert' * @see {@link https://lodash.com/docs#pull|lodash.pull} for more information. * @since 0.2.0 */ -const pull = convert(_pull) +const pull = convertLodashFp(_pull) export { pull } diff --git a/src/array/pullAll.js b/src/array/pullAll.js index 15ca7d9f..8a064043 100644 --- a/src/array/pullAll.js +++ b/src/array/pullAll.js @@ -1,5 +1,5 @@ -import _pullAll from 'lodash/pullAll' -import { convert } from 'util/convert' +import _pullAll from 'lodash/fp/pullAll' +import { convertLodashFp } from 'util/convert' /** * This method is like {@link array.pull} except that it accepts an array of values to remove. @@ -13,5 +13,5 @@ import { convert } from 'util/convert' * @see {@link https://lodash.com/docs#pullAll|lodash.pullAll} for more information. * @since 0.3.0 */ -const pullAll = convert(_pullAll) +const pullAll = convertLodashFp(_pullAll) export { pullAll } diff --git a/src/array/pullAllBy.js b/src/array/pullAllBy.js index 263d6bbe..121323b5 100644 --- a/src/array/pullAllBy.js +++ b/src/array/pullAllBy.js @@ -1,5 +1,5 @@ -import _pullAllBy from 'lodash/pullAllBy' -import { convert } from 'util/convert' +import _pullAllBy from 'lodash/fp/pullAllBy' +import { convertLodashFp } from 'util/convert' /** * This method is like {@link array.pullAll} except that it accepts iteratee to generate the criterion by which each element is compared. @@ -14,5 +14,5 @@ import { convert } from 'util/convert' * @see {@link https://lodash.com/docs#pullAllBy|lodash.pullAllBy} for more information. * @since 0.3.0 */ -const pullAllBy = convert(_pullAllBy) +const pullAllBy = convertLodashFp(_pullAllBy) export { pullAllBy } diff --git a/src/array/pullAllWith.js b/src/array/pullAllWith.js index d5534f8b..54956fcf 100644 --- a/src/array/pullAllWith.js +++ b/src/array/pullAllWith.js @@ -1,5 +1,5 @@ -import _pullAllWith from 'lodash/pullAllWith' -import { convert } from 'util/convert' +import _pullAllWith from 'lodash/fp/pullAllWith' +import { convertLodashFp } from 'util/convert' /** * This method is like {@link array.pullAll} except that it accepts comparator to compare elements. @@ -14,5 +14,5 @@ import { convert } from 'util/convert' * @see {@link https://lodash.com/docs#pullAllWith|lodash.pullAllWith} for more information. * @since 0.3.0 */ -const pullAllWith = convert(_pullAllWith) +const pullAllWith = convertLodashFp(_pullAllWith) export { pullAllWith } diff --git a/src/array/pullAt.js b/src/array/pullAt.js index 1dc66516..61773cbc 100644 --- a/src/array/pullAt.js +++ b/src/array/pullAt.js @@ -1,5 +1,5 @@ -import _pullAt from 'lodash/pullAt' -import { convert } from 'util/convert' +import _pullAt from 'lodash/fp/pullAt' +import { convertLodashFp } from 'util/convert' /** * Replaces an array removing the specified indexes from the former array. @@ -13,8 +13,5 @@ import { convert } from 'util/convert' * @see {@link https://lodash.com/docs#pullAt|lodash.pullAt} for more information. * @since 0.3.0 */ -const pullAt = convert((array, predicate) => { - _pullAt(array, predicate) - return array -}) +const pullAt = convertLodashFp(_pullAt) export { pullAt } diff --git a/src/array/push.spec.js b/src/array/push.spec.js index 967017b6..ee898eed 100644 --- a/src/array/push.spec.js +++ b/src/array/push.spec.js @@ -39,4 +39,12 @@ describe('Push', () => { return output }, undefined, 'nested.prop') }) + + it('should wrap value in an array', () => { + immutaTest((input, path) => { + const output = push(input, path, 2) + expect(output).toEqual({ nested: { prop: [1, 2] } }) + return output + }, { nested: { prop: 1 } }, 'nested.prop') + }) }) diff --git a/src/array/remove.js b/src/array/remove.js index 1d5dd3fc..83b2fcfb 100644 --- a/src/array/remove.js +++ b/src/array/remove.js @@ -1,5 +1,5 @@ -import _remove from 'lodash/remove' -import { convert } from 'util/convert' +import _remove from 'lodash/fp/remove' +import { convertLodashFp } from 'util/convert' /** * Replaces an array removing elements that predicate returns truthy for from the former array. @@ -13,8 +13,5 @@ import { convert } from 'util/convert' * @see {@link https://lodash.com/docs#remove|lodash.remove} for more information. * @since 0.2.0 */ -const remove = convert((array, predicate) => { - _remove(array, predicate) - return array -}) +const remove = convertLodashFp(_remove) export { remove } diff --git a/src/array/reverse.js b/src/array/reverse.js index aa1d3f67..d9639b5c 100644 --- a/src/array/reverse.js +++ b/src/array/reverse.js @@ -1,5 +1,5 @@ -import _reverse from 'lodash/reverse' -import { convert } from 'util/convert' +import _reverse from 'lodash/fp/reverse' +import { convertLodashFp } from 'util/convert' /** * Replaces an array reversing the elements from the former array. @@ -12,5 +12,5 @@ import { convert } from 'util/convert' * @see {@link https://lodash.com/docs#reverse|lodash.reverse} for more information. * @since 0.3.0 */ -const reverse = convert(_reverse) +const reverse = convertLodashFp(_reverse) export { reverse } diff --git a/src/array/slice.js b/src/array/slice.js index 59327e9a..c8df8d57 100644 --- a/src/array/slice.js +++ b/src/array/slice.js @@ -1,5 +1,5 @@ import _slice from 'lodash/slice' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Replaces an array by a slice of the former array from start up to, but not including, end. diff --git a/src/array/take.js b/src/array/take.js index c5b71978..b7a62128 100644 --- a/src/array/take.js +++ b/src/array/take.js @@ -1,5 +1,5 @@ import _take from 'lodash/take' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Creates a slice of array with n elements taken from the beginning. diff --git a/src/array/takeRight.js b/src/array/takeRight.js index c5c20e28..67154a0c 100644 --- a/src/array/takeRight.js +++ b/src/array/takeRight.js @@ -1,5 +1,5 @@ import _takeRight from 'lodash/takeRight' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Creates a slice of array with n elements taken from the end. diff --git a/src/array/takeRightWhile.js b/src/array/takeRightWhile.js index 778f75e8..abaa0e85 100644 --- a/src/array/takeRightWhile.js +++ b/src/array/takeRightWhile.js @@ -1,5 +1,5 @@ import _takeRightWhile from 'lodash/takeRightWhile' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Creates a slice of array with elements taken from the end. diff --git a/src/array/takeWhile.js b/src/array/takeWhile.js index 839fe009..5f69bd10 100644 --- a/src/array/takeWhile.js +++ b/src/array/takeWhile.js @@ -1,5 +1,5 @@ import _takeWhile from 'lodash/takeWhile' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Creates a slice of array with elements taken from the beginning. diff --git a/src/array/union.js b/src/array/union.js index ad907e83..37d15170 100644 --- a/src/array/union.js +++ b/src/array/union.js @@ -1,5 +1,5 @@ import _union from 'lodash/union' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Replaces an array by an array of unique values, in order, from the former array and the given arrays. diff --git a/src/array/unionBy.js b/src/array/unionBy.js index d30527d9..df9d6975 100644 --- a/src/array/unionBy.js +++ b/src/array/unionBy.js @@ -1,5 +1,5 @@ import _unionBy from 'lodash/unionBy' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * This method is like {@link array.union} except that it accepts iteratee to generate the criterion by which elements are compared. diff --git a/src/array/unionWith.js b/src/array/unionWith.js index 3573a72e..51c08cd0 100644 --- a/src/array/unionWith.js +++ b/src/array/unionWith.js @@ -1,5 +1,5 @@ import _unionWith from 'lodash/unionWith' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * This method is like {@link array.union} except that it accepts comparator to compare elements. diff --git a/src/array/xor.js b/src/array/xor.js index dc08d08d..6234262a 100644 --- a/src/array/xor.js +++ b/src/array/xor.js @@ -1,5 +1,5 @@ import _xor from 'lodash/xor' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Replaces an array by the symmetric difference of the former array and the given arrays. diff --git a/src/array/xorBy.js b/src/array/xorBy.js index c4cda37e..46928d1e 100644 --- a/src/array/xorBy.js +++ b/src/array/xorBy.js @@ -1,5 +1,5 @@ import _xorBy from 'lodash/xorBy' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * This method is like {@link array.xor} except that it accepts iteratee to generate the criterion by which elements are compared. diff --git a/src/array/xorWith.js b/src/array/xorWith.js index 982e89f1..9e4f61d3 100644 --- a/src/array/xorWith.js +++ b/src/array/xorWith.js @@ -1,5 +1,5 @@ import _xorWith from 'lodash/xorWith' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * This method is like {@link array.xor} except that it accepts comparator to compare elements. diff --git a/src/collection/filter.js b/src/collection/filter.js index 9d771157..599ca5e7 100644 --- a/src/collection/filter.js +++ b/src/collection/filter.js @@ -1,5 +1,5 @@ import _filter from 'lodash/filter' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Replaces by an array of elements predicate returns truthy for. diff --git a/src/collection/map.js b/src/collection/map.js index 51ece32b..af3d154e 100644 --- a/src/collection/map.js +++ b/src/collection/map.js @@ -1,5 +1,5 @@ import _map from 'lodash/map' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Replaces by an array of values by running each element in the former collection thru iteratee. diff --git a/src/collection/orderBy.js b/src/collection/orderBy.js index 2fc6ef09..84032a93 100644 --- a/src/collection/orderBy.js +++ b/src/collection/orderBy.js @@ -1,5 +1,5 @@ import _orderBy from 'lodash/orderBy' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Replaces by an array of sorted by iteratees in specified orders. diff --git a/src/collection/reject.js b/src/collection/reject.js index fb0d6c9b..4cf434bc 100644 --- a/src/collection/reject.js +++ b/src/collection/reject.js @@ -1,5 +1,5 @@ import _reject from 'lodash/reject' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Replaces by an array of elements predicate returns falsy for. diff --git a/src/collection/shuffle.js b/src/collection/shuffle.js index bf64fef4..9f42f690 100644 --- a/src/collection/shuffle.js +++ b/src/collection/shuffle.js @@ -1,5 +1,5 @@ import _shuffle from 'lodash/shuffle' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Replaces by an array of shuffled elements. diff --git a/src/collection/sortBy.js b/src/collection/sortBy.js index 531a35c6..5e427d93 100644 --- a/src/collection/sortBy.js +++ b/src/collection/sortBy.js @@ -1,5 +1,5 @@ import _sortBy from 'lodash/sortBy' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Replaces by an array of sorted by iteratees. diff --git a/src/core/convert.js b/src/core/convert.js new file mode 100644 index 00000000..8cea8656 --- /dev/null +++ b/src/core/convert.js @@ -0,0 +1,21 @@ +import { update } from 'core/update' + +/** + * Wraps an updater function, returning a new function taking object, path and …args as parameters.
+ * The updater function is invoked with value and …args.
+ * Be carefull, the updater function must not mutate its value argument. + * @memberof core + * @param {function} updater The updater function. + * @return {function} Returns the wrapped function. + * @example Wrapping an updater + * const inc = (v, i = 1) => v + i // this function increments a number with an optional value which defaults to 1 + * const incProp = convert(inc) + * const object = { nested: { prop: 4 } } + * incProp(object, 'nested.prop') // => { nested: { prop: 5 } } + * incProp(object, 'nested.prop', 2) // => { nested: { prop: 6 } } + * @see {@link core.update|update} for more information. + * @since 0.4.0 + */ +const convert = updater => (obj, path, ...rest) => update(obj, path, updater, ...rest) + +export { convert } diff --git a/src/util/convert.spec.js b/src/core/convert.spec.js similarity index 96% rename from src/util/convert.spec.js rename to src/core/convert.spec.js index 2335bff2..57cfdd21 100644 --- a/src/util/convert.spec.js +++ b/src/core/convert.spec.js @@ -1,5 +1,5 @@ /* eslint-env jest */ -import { convert } from 'util' +import { convert } from 'core' import { immutaTest } from 'test.utils' describe('Convert', () => { diff --git a/src/core/index.js b/src/core/index.js index ba19cc18..2cfa97f5 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -1,3 +1,4 @@ +import { convert } from './convert' import { set } from './set' import { toPath } from './toPath' import { update } from './update' @@ -8,6 +9,7 @@ import { update } from './update' * @since 0.4.0 */ export { + convert, set, toPath, update, diff --git a/src/core/update.js b/src/core/update.js index 42f58fbd..3c66d727 100644 --- a/src/core/update.js +++ b/src/core/update.js @@ -19,10 +19,10 @@ import { unsafeToPath } from './toPath' * update(object, 'nested.prop', inc, 2) // => { nested: { prop: 6 } } * @since 0.4.0 */ -const update = (obj, path, updater, ...param) => apply( +const update = (obj, path, updater, ...args) => apply( obj, unsafeToPath(path), - curObj => updater(curObj, ...param), + curObj => updater(curObj, ...args), ) export { update } diff --git a/src/lang/toggle.js b/src/lang/toggle.js index 192376a5..94be34f1 100644 --- a/src/lang/toggle.js +++ b/src/lang/toggle.js @@ -1,4 +1,4 @@ -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Applies ! to the property. diff --git a/src/math/add.js b/src/math/add.js index 4eef0d4e..76ea2619 100644 --- a/src/math/add.js +++ b/src/math/add.js @@ -1,5 +1,5 @@ import _add from 'lodash/add' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Replaces by the addition of the former number and the given number. diff --git a/src/math/divide.js b/src/math/divide.js index 8f9b003a..978af972 100644 --- a/src/math/divide.js +++ b/src/math/divide.js @@ -1,5 +1,5 @@ import _divide from 'lodash/divide' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Replaces by the division of the former number and the given number. diff --git a/src/math/multiply.js b/src/math/multiply.js index d9553e4e..58cd26dc 100644 --- a/src/math/multiply.js +++ b/src/math/multiply.js @@ -1,5 +1,5 @@ import _multiply from 'lodash/multiply' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Replaces by the multiplication of the former number and the given number. diff --git a/src/math/subtract.js b/src/math/subtract.js index 26cae1cc..e32f29e8 100644 --- a/src/math/subtract.js +++ b/src/math/subtract.js @@ -1,5 +1,5 @@ import _subtract from 'lodash/subtract' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Replaces by the subtraction of the former number by the given number. diff --git a/src/object/assign.js b/src/object/assign.js index ef3ee76e..46d99664 100644 --- a/src/object/assign.js +++ b/src/object/assign.js @@ -1,5 +1,5 @@ -import _assign from 'lodash/assign' -import { convert } from 'util/convert' +import _assign from 'lodash/fp/assign' +import { convertLodashFp } from 'util/convert' /** * Replaces by an object assigning own enumerable string keyed properties of source objects to the destination object.
@@ -14,5 +14,5 @@ import { convert } from 'util/convert' * @see {@link https://lodash.com/docs#assign|lodash.assign} for more information. * @since 0.1.12 */ -const assign = convert(_assign) +const assign = convertLodashFp(_assign) export { assign } diff --git a/src/object/defaults.js b/src/object/defaults.js index 889a1f6c..5fe72031 100644 --- a/src/object/defaults.js +++ b/src/object/defaults.js @@ -1,5 +1,5 @@ -import _defaults from 'lodash/defaults' -import { convert } from 'util/convert' +import _defaults from 'lodash/fp/defaults' +import { convertLodashFp } from 'util/convert' /** * Replaces by an object assigning own and inherited enumerable string keyed properties of source objects to the destination object for all destination properties that resolve to undefined.
@@ -14,5 +14,5 @@ import { convert } from 'util/convert' * @see {@link https://lodash.com/docs#defaults|lodash.defaults} for more information. * @since 0.3.0 */ -const defaults = convert(_defaults) +const defaults = convertLodashFp(_defaults) export { defaults } diff --git a/src/object/mapKeys.js b/src/object/mapKeys.js index 9566ec18..a9eb069c 100644 --- a/src/object/mapKeys.js +++ b/src/object/mapKeys.js @@ -1,5 +1,5 @@ import _mapKeys from 'lodash/mapKeys' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Replaces by an object with the same values as the former object and values generated by running each own enumerable string keyed property of the former object thru iteratee. diff --git a/src/object/mapValues.js b/src/object/mapValues.js index 732773a5..6b074161 100644 --- a/src/object/mapValues.js +++ b/src/object/mapValues.js @@ -1,5 +1,5 @@ import _mapValues from 'lodash/mapValues' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Replaces by an object with the same keys as the former object and values generated by running each own enumerable string keyed property of object thru iteratee. diff --git a/src/object/omit.js b/src/object/omit.js index cc166913..2b990df6 100644 --- a/src/object/omit.js +++ b/src/object/omit.js @@ -1,5 +1,5 @@ import _omit from 'lodash/omit' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Replaces by an object omitting specified properties. diff --git a/src/object/omitBy.js b/src/object/omitBy.js index 85978276..8582f4e7 100644 --- a/src/object/omitBy.js +++ b/src/object/omitBy.js @@ -1,5 +1,5 @@ import _omitBy from 'lodash/omitBy' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Replaces by an object omitting properties that predicate doesn't return truthy for. diff --git a/src/object/pick.js b/src/object/pick.js index 2d7dff64..d5d3f1f6 100644 --- a/src/object/pick.js +++ b/src/object/pick.js @@ -1,5 +1,5 @@ import _pick from 'lodash/pick' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Replaces by an object picking specified properties. diff --git a/src/object/pickBy.js b/src/object/pickBy.js index d2a8df56..4b8d10f3 100644 --- a/src/object/pickBy.js +++ b/src/object/pickBy.js @@ -1,5 +1,5 @@ import _pickBy from 'lodash/pickBy' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Replaces by an object picking properties that predicate returns truthy for. diff --git a/src/object/update.js b/src/object/update.js index 300c3474..ba7fa100 100644 --- a/src/object/update.js +++ b/src/object/update.js @@ -1,30 +1,5 @@ -import _update from 'lodash/fp/update' -import { lodashFpConvert } from 'util/lodashFpConvert' import { update } from 'core/update' -const convertedUpdate = lodashFpConvert(_update) - -/** - * Updates the value at path of object using the updater function.
- * The updater is invoked with value and …args.
- * Be carefull, the updater function must not mutate its value argument. - * @function - * @memberof object - * @param {Object} object The object to modify. - * @param {Array|string} path The path of the property to set. - * @param {function} updater The function to produce the updated value. - * @param {...*} args The remaining args. - * @return {Object} Returns the updated object. - * @example Updating a prop - * const inc = (v, i = 1) => v + i // this function increments a number with an optional value which defaults to 1 - * const object = { nested: { prop: 4 } } - * update(object, 'nested.prop', inc) // => { nested: { prop: 5 } } - * update(object, 'nested.prop', inc, 2) // => { nested: { prop: 6 } } - * @see {@link https://lodash.com/docs#update|lodash.update} for more information. - * @since 0.1.5 - */ -const formerUpdate = (object, path, updater, ...args) => convertedUpdate(object, path, v => updater(v, ...args)) - /** * This is an alias for {@link core.update}. * @function update @@ -32,4 +7,4 @@ const formerUpdate = (object, path, updater, ...args) => convertedUpdate(object, * @since 0.1.5 * @deprecated Use {@link core.update} */ -export { formerUpdate, update } +export { update } diff --git a/src/object/update.spec.js b/src/object/update.spec.js deleted file mode 100644 index 310905cd..00000000 --- a/src/object/update.spec.js +++ /dev/null @@ -1,36 +0,0 @@ -/* eslint-env jest */ -import { formerUpdate } from './update' -import { immutaTest } from 'test.utils' - -describe('Former Update', () => { - - const inc = (v, i = 1) => v + i - - it('should update a prop', () => { - immutaTest((input, path) => { - const output = formerUpdate(input, path, inc) - expect(output).toEqual({ - nested: { prop: 6 }, - other: {}, - }) - return output - }, { - nested: { prop: 5 }, - other: {}, - }, 'nested.prop') - }) - - it('should update a prop with a param', () => { - immutaTest((input, path) => { - const output = formerUpdate(input, path, inc, 2) - expect(output).toEqual({ - nested: { prop: 7 }, - other: {}, - }) - return output - }, { - nested: { prop: 5 }, - other: {}, - }, 'nested.prop') - }) -}) diff --git a/src/seq/ChainWrapper.js b/src/seq/ChainWrapper.js index 09b9c899..433ceb4f 100644 --- a/src/seq/ChainWrapper.js +++ b/src/seq/ChainWrapper.js @@ -1,5 +1,6 @@ import * as array from 'array' import * as collection from 'collection' +import * as core from 'core' import * as lang from 'lang' import * as math from 'math' import * as object from 'object' @@ -8,6 +9,7 @@ import * as string from 'string' import concat from 'lodash/concat' import flow from 'lodash/flow' import mapValues from 'lodash/mapValues' +import omit from 'lodash/omit' import toPath from 'lodash/toPath' /** @@ -124,9 +126,10 @@ class ChainWrapper { [ array, collection, + omit(core, ['convert', 'toPath']), lang, math, - object, + omit(object, ['set', 'update']), string, ].forEach(namespace => Object.assign( ChainWrapper.prototype, diff --git a/src/string/capitalize.js b/src/string/capitalize.js index 14d66cc7..7d8a5065 100644 --- a/src/string/capitalize.js +++ b/src/string/capitalize.js @@ -1,5 +1,5 @@ import _capitalize from 'lodash/capitalize' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Converts the first character of string to upper case and the remaining to lower case. diff --git a/src/string/replace.js b/src/string/replace.js index e443ca84..c05e5ea6 100644 --- a/src/string/replace.js +++ b/src/string/replace.js @@ -1,5 +1,5 @@ import _replace from 'lodash/replace' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Replaces matches for pattern in string with replacement. diff --git a/src/string/toLower.js b/src/string/toLower.js index 5d8634df..c6082143 100644 --- a/src/string/toLower.js +++ b/src/string/toLower.js @@ -1,5 +1,5 @@ import _toLower from 'lodash/toLower' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Converts string, as a whole, to lower case just like String#toLowerCase. diff --git a/src/string/toUpper.js b/src/string/toUpper.js index 8329a0fe..eb55b267 100644 --- a/src/string/toUpper.js +++ b/src/string/toUpper.js @@ -1,5 +1,5 @@ import _toUpper from 'lodash/toUpper' -import { convert } from 'util/convert' +import { convert } from 'core/convert' /** * Converts string, as a whole, to upper case just like String#toUpperCase. diff --git a/src/util/UsingWrapper.js b/src/util/UsingWrapper.js index 4d8826e4..b348ce91 100644 --- a/src/util/UsingWrapper.js +++ b/src/util/UsingWrapper.js @@ -1,5 +1,6 @@ import * as array from 'array' import * as collection from 'collection' +import * as core from 'core' import * as lang from 'lang' import * as math from 'math' import * as object from 'object' @@ -71,10 +72,10 @@ class UsingWrapper { [ array, collection, - { ...omit(object, ['unset']) }, + omit(core, ['convert', 'toPath']), lang, math, - object, + omit(object, ['set', 'unset', 'update']), string, ].forEach(namespace => Object.assign( UsingWrapper.prototype, diff --git a/src/util/convert.js b/src/util/convert.js index 8cc2f04e..509bda54 100644 --- a/src/util/convert.js +++ b/src/util/convert.js @@ -1,24 +1,6 @@ -import { formerUpdate } from 'object/update' +import { convert } from 'core/convert' import { lodashFpConvert } from './lodashFpConvert' -/** - * Wraps an updater function, returning a new function taking object, path and …args as parameters.
- * The updater function is invoked with value and …args.
- * Be carefull, the updater function must not mutate its value argument. - * @memberof util - * @param {function} updater The updater function. - * @return {function} Returns the wrapped function. - * @example Wrapping an updater - * const inc = (v, i = 1) => v + i // this function increments a number with an optional value which defaults to 1 - * const incProp = convert(inc) - * const object = { nested: { prop: 4 } } - * incProp(object, 'nested.prop') // => { nested: { prop: 5 } } - * incProp(object, 'nested.prop', 2) // => { nested: { prop: 6 } } - * @see {@link object.update|update} for more information. - * @since 0.2.0 - */ -const convert = updater => (obj, path, ...rest) => formerUpdate(obj, path, updater, ...rest) - /** * Converts and wraps a lodash/fp function. * @memberof util @@ -30,4 +12,13 @@ const convert = updater => (obj, path, ...rest) => formerUpdate(obj, path, updat */ const convertLodashFp = fn => convert(lodashFpConvert(fn)) -export { convert, convertLodashFp } +/** + * This is an alias for {@link core.convert}. + * @memberof object + * @function convert + * @deprecated Use {@link core.convert} + * @since 0.2.0 + */ +export { + convertLodashFp, +} diff --git a/src/util/lodashFpConvert.js b/src/util/lodashFpConvert.js index daf3fb06..7dcc5f2c 100644 --- a/src/util/lodashFpConvert.js +++ b/src/util/lodashFpConvert.js @@ -1,6 +1,7 @@ const lodashFpConvertOptions = { - rearg: false, curry: false, + fixed: false, + rearg: false, } /**