Skip to content

Commit

Permalink
🐛 fix #72 unnecessary object copies
Browse files Browse the repository at this point in the history
  • Loading branch information
nlepage committed Aug 14, 2017
1 parent 3264962 commit b501453
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/array/convertArrayMethod.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import convert from '../util/convert'
import isArray from 'lodash/isArray'
import toArray from 'lodash/toArray'

/**
Expand All @@ -8,12 +9,13 @@ import toArray from 'lodash/toArray'
* @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.
* @since 0.2.0
* @private
*/
export default method => convert((array, ...args) => {
const newArray = toArray(array)
const newArray = isArray(array) ? array : toArray(array)
newArray[method](...args)
return newArray
})
9 changes: 6 additions & 3 deletions src/array/remove.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _remove from 'lodash/fp/remove'
import { convertLodashFp } from '../util/convert'
import _remove from 'lodash/remove'
import convert from '../util/convert'

/**
* Replaces an array removing elements that predicate returns truthy for from the former array.
Expand All @@ -13,5 +13,8 @@ import { convertLodashFp } from '../util/convert'
* @see {@link https://lodash.com/docs#remove|lodash.remove} for more information.
* @since 0.2.0
*/
const remove = convertLodashFp(_remove)
const remove = convert((array, predicate) => {
_remove(array, predicate)
return array
})
export default remove
4 changes: 0 additions & 4 deletions src/array/remove.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,4 @@ describe('Remove', () => {
expect(final).toEqual({ nested: { prop: [1, 2] } })
expect(original).toEqual({ nested: { prop: [1, 2, 3] } })
})

it('should replace deep undefined with array', () => {
expect(remove(undefined, 'nested.prop', () => true)).toEqual({ nested: { prop: [] } })
})
})
6 changes: 3 additions & 3 deletions src/object/assign.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _assign from 'lodash/fp/assign'
import { convertLodashFp } from '../util/convert'
import _assign from 'lodash/assign'
import convert from '../util/convert'

/**
* Assigns own enumerable string keyed properties of source objects to the
Expand All @@ -15,5 +15,5 @@ import { convertLodashFp } from '../util/convert'
* @see {@link https://lodash.com/docs#assign|lodash.assign} for more information.
* @since 0.1.12
*/
const assign = convertLodashFp(_assign)
const assign = convert(_assign)
export default assign
1 change: 1 addition & 0 deletions src/util/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default convert
* @return {function} Returns the wrapped function.
* @see {@link util.convert|convert} for more information.
* @since 0.2.0
* @deprecated Unused since 0.3.0 and {@link fix #72|https://github.com/Zenika/immutadot/issues/72}, to be removed ?
* @private
*/
export const convertLodashFp = fn => convert(lodashFpConvert(fn))

0 comments on commit b501453

Please sign in to comment.