Skip to content

Commit

Permalink
🎉 Remove lodash from immutadot package (#143)
Browse files Browse the repository at this point in the history
* 🔨 Remove lodash from fill

* 🔨 Remove lodash from reverse

* 🔨 Remove lodash from slice

* 🔨 Remove lodash from concat

* 🔨 Remove lodash from add

* 🔨 Remove lodash from divide

* 🔨 Remove lodash from multiply

* 🔨 Remove lodash from subtract

* 🔨 Remove lodash from replace

* 🔨 Remove lodash from assign

* 🚚 Move convertLodashFp in immutadot-lodash package

* 🚧 Remove concat from ChainWrapper

* 🚧 Remove omit from ChainWrapper

* 🚧 Remove mapValues from ChainWrapper

* 🚧 Remove flow from ChainWrapper

* 🚧 Remove concat from protect

* 🚧 Remove get from protect

* 🚧 Remove isObject from protect

* 🚧 Remove isEmpty from protect

* 🚧 Remove map from using

* 🚧 Remove head from using

* 🚧 Remove concat from using

* 🚧 Remove drop from using

* 🚧 Remove get from using

* 🚧 Remove mapValues from using

* 🚧 Remove omit from using

* 🚧 Remove isSymbol from using

* 🐛 toPath: Return empty path for nil values

* 🚧 Remove _.toPath from seq.ChainWrapper

* ♻ Use module resolver for convertLodashFp imports

* ♻ Manage non mutating methods in convertArrayMethod

* ♻ toPath: Move nil arg concern from allowingArrays to stringToPath

* 🚨 fix lint after rebase

* 🔥 Remove util namespace from lodash plugin

* 🔨 Remove concat from empty array when uneccessary

* 🔨 Use isSymbol from util/lang

* 🔨 Replace drop by Array.slice

* ⏪ revert convertLodashFp deletion

* 🚚 Rename convert.js to convertLodashFp.js

* ➖ Remove lodash from immutadot package

* ♻ Replace concat one element by destructuring

* ♻ Inline flow in ChainWrapper

* ♻ Replace omit by destructuring in ChainWrapper

* ♻ Replace mapValues by a for in in ChainWrapper

* ♻️ Inline get and isEmpty in protect

* 🚚 Move isObject to util/lang

* ♻️ Use instanceof instead of typeof for isObject implementation

* ♻ Replace omit by destructuring in UsingWrapper

* ♻ Add get to core for usage in UsingWrapper

* 💡 core.get documentation

* ✏️ Fix typo in isObject jsdoc
  • Loading branch information
frinyvonnick authored and nlepage committed Dec 7, 2017
1 parent cbeda1d commit 79d0b59
Show file tree
Hide file tree
Showing 34 changed files with 222 additions and 134 deletions.
2 changes: 1 addition & 1 deletion packages/immutadot-lodash/src/array/pull.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _pull from 'lodash/fp/pull'
import { convertLodashFp } from 'immutadot/util/convert'
import { convertLodashFp } from 'util/convertLodashFp'

/**
* Replaces an array removing all given values from the former array.
Expand Down
2 changes: 1 addition & 1 deletion packages/immutadot-lodash/src/array/pullAll.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _pullAll from 'lodash/fp/pullAll'
import { convertLodashFp } from 'immutadot/util/convert'
import { convertLodashFp } from 'util/convertLodashFp'

/**
* This method is like {@link array.pull} except that it accepts an array of values to remove.
Expand Down
2 changes: 1 addition & 1 deletion packages/immutadot-lodash/src/array/pullAllBy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _pullAllBy from 'lodash/fp/pullAllBy'
import { convertLodashFp } from 'immutadot/util/convert'
import { convertLodashFp } from 'util/convertLodashFp'

/**
* 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 Down
2 changes: 1 addition & 1 deletion packages/immutadot-lodash/src/array/pullAllWith.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _pullAllWith from 'lodash/fp/pullAllWith'
import { convertLodashFp } from 'immutadot/util/convert'
import { convertLodashFp } from 'util/convertLodashFp'

/**
* This method is like {@link array.pullAll} except that it accepts <code>comparator</code> to compare elements.
Expand Down
2 changes: 1 addition & 1 deletion packages/immutadot-lodash/src/array/pullAt.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _pullAt from 'lodash/fp/pullAt'
import { convertLodashFp } from 'immutadot/util/convert'
import { convertLodashFp } from 'util/convertLodashFp'

/**
* Replaces an array removing the specified indexes from the former array.
Expand Down
2 changes: 1 addition & 1 deletion packages/immutadot-lodash/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 'immutadot/util/convert'
import { convertLodashFp } from 'util/convertLodashFp'

/**
* Replaces an array removing elements that predicate returns truthy for from the former array.
Expand Down
2 changes: 1 addition & 1 deletion packages/immutadot-lodash/src/object/defaults.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _defaults from 'lodash/fp/defaults'
import { convertLodashFp } from 'immutadot/util/convert'
import { convertLodashFp } from 'util/convertLodashFp'

/**
* 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 <code>undefined</code>.<br >
Expand Down
2 changes: 1 addition & 1 deletion packages/immutadot-lodash/src/object/merge.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _merge from 'lodash/fp/merge'
import { convertLodashFp } from 'immutadot/util/convert'
import { convertLodashFp } from 'util/convertLodashFp'

/**
* Replaces by an object deeply merging own enumerable string keyed properties of source objects to the former object.<br />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { convert } from 'core/convert'
import { convert } from 'immutadot/core/convert'
import { lodashFpConvert } from './lodashFpConvert'

/**
Expand Down
3 changes: 0 additions & 3 deletions packages/immutadot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@
"jsdoc": "^3.5.5"
},
"dependencies": {},
"peerDependencies": {
"lodash": "^4.17.4"
},
"scripts": {
"generate:flow": "node ../../misc/generate-flow.js",
"prebuild": "yarn generate:flow",
Expand Down
5 changes: 2 additions & 3 deletions packages/immutadot/src/array/concat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import _concat from 'lodash/concat'
import { convert } from 'core/convert'
import { convertArrayMethod } from './convertArrayMethod'

/**
* Replaces an array concatenating the former array with additional arrays and/or values.
Expand All @@ -14,5 +13,5 @@ import { convert } from 'core/convert'
* @since 0.2.0
* @flow
*/
const concat = convert(_concat)
const concat = convertArrayMethod('concat', false, true)
export { concat }
26 changes: 20 additions & 6 deletions packages/immutadot/src/array/convertArrayMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,39 @@ import {
isNil,
} from 'util/lang'

const copyArray = array => {
const toArray = array => {
if (isNil(array)) return []
if (Array.isArray(array)) return array
return [array]
}

const toArrayCopy = array => {
if (isNil(array)) return []
if (Array.isArray(array)) return [...array]
return [array]
}

const callMethodReturnResult = (array, method, args) => array[method](...args)

const callMethodReturnArray = (array, method, args) => {
array[method](...args)
return array
}

/**
* Converts an Array method.
* @memberof array
* @param {string} method Array method name.
* @param {boolean} [mutating=true] Whether the method mutates the array.
* @return {function} Returns the wrapped function.
* @see {@link core.convert|convert} for more information.
* @since 0.2.0
* @private
*/
const convertArrayMethod = method => convert((array, ...args) => {
const newArray = copyArray(array)
newArray[method](...args)
return newArray
})
const convertArrayMethod = (method, mutating = true) => {
const getArray = mutating ? toArrayCopy : toArray
const callMethod = mutating ? callMethodReturnArray : callMethodReturnResult
return convert((value, ...args) => callMethod(getArray(value), method, args))
}

export { convertArrayMethod }
5 changes: 2 additions & 3 deletions packages/immutadot/src/array/fill.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import _fill from 'lodash/fp/fill'
import { convertLodashFp } from 'util/convert'
import { convertArrayMethod } from './convertArrayMethod'

/**
* Replaces by an array filled with value from start up to, but not including, end.
Expand All @@ -16,5 +15,5 @@ import { convertLodashFp } from 'util/convert'
* @since 0.3.0
* @flow
*/
const fill = convertLodashFp(_fill)
const fill = convertArrayMethod('fill')
export { fill }
5 changes: 2 additions & 3 deletions packages/immutadot/src/array/reverse.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import _reverse from 'lodash/fp/reverse'
import { convertLodashFp } from 'util/convert'
import { convertArrayMethod } from './convertArrayMethod'

/**
* Replaces an array reversing the elements from the former array.
Expand All @@ -13,5 +12,5 @@ import { convertLodashFp } from 'util/convert'
* @since 0.3.0
* @flow
*/
const reverse = convertLodashFp(_reverse)
const reverse = convertArrayMethod('reverse')
export { reverse }
5 changes: 2 additions & 3 deletions packages/immutadot/src/array/slice.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import _slice from 'lodash/slice'
import { convert } from 'core/convert'
import { convertArrayMethod } from './convertArrayMethod'

/**
* Replaces an array by a slice of the former array from <code>start</code> up to, but not including, <code>end</code>.
Expand All @@ -15,5 +14,5 @@ import { convert } from 'core/convert'
* @since 0.3.0
* @flow
*/
const slice = convert(_slice)
const slice = convertArrayMethod('slice', false, true)
export { slice }
23 changes: 23 additions & 0 deletions packages/immutadot/src/core/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { isNil } from 'util/lang'
import { unsafeToPath } from './toPath'

/**
* Gets the value at <code>path</code> of <code>obj</code>.
* @memberof core
* @param {*} obj The object.
* @param {string|Array} path The path of the property to get.
* @param {*} defaultValue The default value.
* @return {*} Returns the value or <code>defaultValue</code>.
* @example get({ nested: { prop: 'val' } }, 'nested.prop') // => 'val'
* @example get({ nested: { prop: 'val' } }, 'nested.unknown', 'default') // => 'default'
* @since 1.0.0
*/
export function get(obj, path, defaultValue) {
function walkPath(curObj, remPath) {
if (remPath.length === 0) return curObj === undefined ? defaultValue : curObj
if (isNil(curObj)) return defaultValue
const [prop, ...pathRest] = remPath
return walkPath(curObj[prop], pathRest)
}
return walkPath(obj, unsafeToPath(path))
}
28 changes: 28 additions & 0 deletions packages/immutadot/src/core/get.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* eslint-env jest */
import { get } from 'core'

describe('Get', () => {
const obj = {
nested1: { prop: 'val' },
nested2: { arr: [{ val: 'arrVal' }] },
}

it('should get a prop', () => {
expect(get(obj, 'nested1.prop')).toBe('val')
expect(get(obj, 'nested1.prop.length')).toBe(3)
expect(get(obj, 'nested2.arr.length')).toBe(1)
expect(get(obj, 'nested2.arr[0].val')).toBe('arrVal')
})

it('should return undefined for unexisting path', () => {
expect(get(obj, 'nested1.foo')).toBe(undefined)
expect(get(obj, 'nested3.val')).toBe(undefined)
expect(get(obj, 'nested2.arr[1].val')).toBe(undefined)
})

it('should return defaultValue for unexisting path', () => {
expect(get(obj, 'nested1.foo', 'defaultValue')).toBe('defaultValue')
expect(get(obj, 'nested3.val', 'defaultValue')).toBe('defaultValue')
expect(get(obj, 'nested2.arr[1].val', 'defaultValue')).toBe('defaultValue')
})
})
28 changes: 11 additions & 17 deletions packages/immutadot/src/core/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import { convert } from './convert'
import { set } from './set'
import { toPath } from './toPath'
import { unset } from './unset'
import { update } from './update'

/**
* Core functions.
* @namespace core
* @since 1.0.0
*/
export {
convert,
set,
toPath,
unset,
update,
}
* Core functions.
* @namespace core
* @since 1.0.0
*/

export { convert } from './convert'
export { get } from './get'
export { set } from './set'
export { toPath } from './toPath'
export { unset } from './unset'
export { update } from './update'
2 changes: 1 addition & 1 deletion packages/immutadot/src/core/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { apply } from './apply'
const setOperation = (obj, prop, _, value) => { obj[prop] = value }

/**
* Sets the value at <code>path</code> of <code>object</code>.
* Sets the value at <code>path</code> of <code>obj</code>.
* @function
* @memberof core
* @param {*} obj The object to modify.
Expand Down
29 changes: 17 additions & 12 deletions packages/immutadot/src/core/toPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from './parser.utils'

import {
isNil,
isSymbol,
toString,
} from 'util/lang'
Expand Down Expand Up @@ -91,8 +92,7 @@ const isSliceIndexString = arg => isSliceIndex(arg ? Number(arg) : undefined)
*/
const allowingArrays = fn => arg => {
if (Array.isArray(arg)) return arg.map(toKey)

return fn(toString(arg))
return fn(arg)
}

const emptyStringParser = str => str.length === 0 ? [] : null
Expand Down Expand Up @@ -139,16 +139,7 @@ const pathSegmentEndedByBracketParser = map(
([beforeBracket, atBracket]) => [beforeBracket, ...stringToPath(atBracket)],
)

/**
* Converts <code>str</code> to a path represented as an array of keys.
* @function
* @param {string} str The string to convert
* @return {Array<string|number|Array>} The path represented as an array of keys
* @memberof core
* @private
* @since 1.0.0
*/
const stringToPath = race([
const applyParsers = race([
emptyStringParser,
quotedBracketNotationParser,
incompleteQuotedBracketNotationParser,
Expand All @@ -160,6 +151,20 @@ const stringToPath = race([
str => [str],
])

/**
* Converts <code>arg</code> to a path represented as an array of keys.
* @function
* @param {*} arg The value to convert
* @return {Array<string|number|Array>} The path represented as an array of keys
* @memberof core
* @private
* @since 1.0.0
*/
const stringToPath = arg => {
if (isNil(arg)) return []
return applyParsers(toString(arg))
}

const MAX_CACHE_SIZE = 1000
const cache = new Map()

Expand Down
5 changes: 5 additions & 0 deletions packages/immutadot/src/core/toPath.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,9 @@ describe('ToPath', () => {
'1,2',
])
})

it('should give empty path for nil values', () => {
expect(toPath(null)).toEqual([])
expect(toPath(undefined)).toEqual([])
})
})
3 changes: 1 addition & 2 deletions packages/immutadot/src/math/add.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import _add from 'lodash/add'
import { convert } from 'core/convert'

/**
Expand All @@ -14,5 +13,5 @@ import { convert } from 'core/convert'
* @since 0.1.7
* @flow
*/
const add = convert(_add)
const add = convert((value, addition) => value + addition)
export { add }
3 changes: 1 addition & 2 deletions packages/immutadot/src/math/divide.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import _divide from 'lodash/divide'
import { convert } from 'core/convert'

/**
Expand All @@ -14,5 +13,5 @@ import { convert } from 'core/convert'
* @since 0.3.0
* @flow
*/
const divide = convert(_divide)
const divide = convert((value, divider) => value / divider)
export { divide }
3 changes: 1 addition & 2 deletions packages/immutadot/src/math/multiply.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import _multiply from 'lodash/multiply'
import { convert } from 'core/convert'

/**
Expand All @@ -14,5 +13,5 @@ import { convert } from 'core/convert'
* @since 0.3.0
* @flow
*/
const multiply = convert(_multiply)
const multiply = convert((value, multiplier) => value * multiplier)
export { multiply }
3 changes: 1 addition & 2 deletions packages/immutadot/src/math/subtract.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import _subtract from 'lodash/subtract'
import { convert } from 'core/convert'

/**
Expand All @@ -14,5 +13,5 @@ import { convert } from 'core/convert'
* @since 0.3.0
* @flow
*/
const subtract = convert(_subtract)
const subtract = convert((value, subtraction) => value - subtraction)
export { subtract }
5 changes: 2 additions & 3 deletions packages/immutadot/src/object/assign.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import _assign from 'lodash/fp/assign'
import { convertLodashFp } from 'util/convert'
import { convert } from 'core/convert'

/**
* Replaces by an object assigning own enumerable string keyed properties of source objects to the destination object.<br />
Expand All @@ -15,5 +14,5 @@ import { convertLodashFp } from 'util/convert'
* @since 0.1.12
* @flow
*/
const assign = convertLodashFp(_assign)
const assign = convert((obj, ...args) => Object.assign({ ...obj }, ...args))
export { assign }

0 comments on commit 79d0b59

Please sign in to comment.