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

Rewrite convert without lodash #121

Merged
merged 18 commits into from
Oct 30, 2017
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
2 changes: 1 addition & 1 deletion src/array/concat.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
16 changes: 9 additions & 7 deletions src/array/convertArrayMethod.js
Original file line number Diff line number Diff line change
@@ -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
})
Expand Down
2 changes: 1 addition & 1 deletion src/array/difference.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/array/differenceBy.js
Original file line number Diff line number Diff line change
@@ -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 <code>iteratee</code> to generate the value to be compared for each element.
Expand Down
2 changes: 1 addition & 1 deletion src/array/differenceWith.js
Original file line number Diff line number Diff line change
@@ -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 <code>comparator</code> to compare elements of the former array to <code>values</code>.
Expand Down
2 changes: 1 addition & 1 deletion src/array/drop.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/array/dropRight.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/array/dropRightWhile.js
Original file line number Diff line number Diff line change
@@ -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 <code>predicate</code> returns falsey.
Expand Down
2 changes: 1 addition & 1 deletion src/array/dropWhile.js
Original file line number Diff line number Diff line change
@@ -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 <code>predicate</code> returns falsey.
Expand Down
6 changes: 3 additions & 3 deletions src/array/fill.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 }
2 changes: 1 addition & 1 deletion src/array/intersection.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/array/intersectionBy.js
Original file line number Diff line number Diff line change
@@ -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 <code>iteratee</code> to generate the value to be compared for each element.
Expand Down
2 changes: 1 addition & 1 deletion src/array/intersectionWith.js
Original file line number Diff line number Diff line change
@@ -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 <code>comparator</code> to compare elements of the former array to <code>arrays</code>.
Expand Down
6 changes: 3 additions & 3 deletions src/array/pull.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 }
6 changes: 3 additions & 3 deletions src/array/pullAll.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 }
6 changes: 3 additions & 3 deletions src/array/pullAllBy.js
Original file line number Diff line number Diff line change
@@ -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 <code>iteratee</code> to generate the criterion by which each element is compared.
Expand All @@ -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 }
6 changes: 3 additions & 3 deletions src/array/pullAllWith.js
Original file line number Diff line number Diff line change
@@ -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 <code>comparator</code> to compare elements.
Expand All @@ -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 }
9 changes: 3 additions & 6 deletions src/array/pullAt.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 }
8 changes: 8 additions & 0 deletions src/array/push.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})
9 changes: 3 additions & 6 deletions src/array/remove.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 }
6 changes: 3 additions & 3 deletions src/array/reverse.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 }
2 changes: 1 addition & 1 deletion src/array/slice.js
Original file line number Diff line number Diff line change
@@ -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 <code>start</code> up to, but not including, <code>end</code>.
Expand Down
2 changes: 1 addition & 1 deletion src/array/take.js
Original file line number Diff line number Diff line change
@@ -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 <code>n</code> elements taken from the beginning.
Expand Down
2 changes: 1 addition & 1 deletion src/array/takeRight.js
Original file line number Diff line number Diff line change
@@ -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 <code>n</code> elements taken from the end.
Expand Down
2 changes: 1 addition & 1 deletion src/array/takeRightWhile.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/array/takeWhile.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/array/union.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/array/unionBy.js
Original file line number Diff line number Diff line change
@@ -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 <code>iteratee</code> to generate the criterion by which elements are compared.
Expand Down
2 changes: 1 addition & 1 deletion src/array/unionWith.js
Original file line number Diff line number Diff line change
@@ -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 <code>comparator</code> to compare elements.
Expand Down
2 changes: 1 addition & 1 deletion src/array/xor.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/array/xorBy.js
Original file line number Diff line number Diff line change
@@ -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 <code>iteratee</code> to generate the criterion by which elements are compared.
Expand Down
2 changes: 1 addition & 1 deletion src/array/xorWith.js
Original file line number Diff line number Diff line change
@@ -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 <code>comparator</code> to compare elements.
Expand Down
2 changes: 1 addition & 1 deletion src/collection/filter.js
Original file line number Diff line number Diff line change
@@ -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 <code>predicate</code> returns truthy for.
Expand Down
2 changes: 1 addition & 1 deletion src/collection/map.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/collection/orderBy.js
Original file line number Diff line number Diff line change
@@ -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 <code>iteratees</code> in specified <code>orders</code>.
Expand Down
2 changes: 1 addition & 1 deletion src/collection/reject.js
Original file line number Diff line number Diff line change
@@ -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 <code>predicate</code> returns falsy for.
Expand Down
2 changes: 1 addition & 1 deletion src/collection/shuffle.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/collection/sortBy.js
Original file line number Diff line number Diff line change
@@ -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 <code>iteratees</code>.
Expand Down
21 changes: 21 additions & 0 deletions src/core/convert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { update } from 'core/update'

/**
* Wraps an <code>updater</code> function, returning a new function taking <code>object</code>, <code>path</code> and <code>…args</code> as parameters.<br/>
* The <code>updater</code> function is invoked with <code>value</code> and <code>…args</code>.<br/>
* Be carefull, the <code>updater</code> function must not mutate its <code>value</code> argument.
* @memberof core
* @param {function} updater The updater function.
* @return {function} Returns the wrapped function.
* @example <caption>Wrapping an updater</caption>
* 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 }
2 changes: 1 addition & 1 deletion src/util/convert.spec.js → src/core/convert.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-env jest */
import { convert } from 'util'
import { convert } from 'core'
import { immutaTest } from 'test.utils'

describe('Convert', () => {
Expand Down
Loading