Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

🚧 Remove lodash from immutadot package #143

Merged
merged 51 commits into from
Dec 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
c7efb84
:hammer: Remove lodash from fill
frinyvonnick Nov 28, 2017
8fe9856
:hammer: Remove lodash from reverse
frinyvonnick Nov 28, 2017
acd2cc8
:hammer: Remove lodash from slice
frinyvonnick Nov 28, 2017
3efc8e1
:hammer: Remove lodash from concat
frinyvonnick Nov 28, 2017
6ebca21
:hammer: Remove lodash from add
frinyvonnick Nov 28, 2017
b7ba98c
:hammer: Remove lodash from divide
frinyvonnick Nov 28, 2017
d41a0a3
:hammer: Remove lodash from multiply
frinyvonnick Nov 28, 2017
96f33b3
:hammer: Remove lodash from subtract
frinyvonnick Nov 28, 2017
ca4e577
:hammer: Remove lodash from replace
frinyvonnick Nov 28, 2017
0cc83b1
:hammer: Remove lodash from assign
frinyvonnick Nov 28, 2017
1660d86
:truck: Move convertLodashFp in immutadot-lodash package
frinyvonnick Nov 28, 2017
fbce11d
:construction: Remove concat from ChainWrapper
frinyvonnick Dec 5, 2017
e663e45
:construction: Remove omit from ChainWrapper
frinyvonnick Dec 5, 2017
57b749f
:construction: Remove mapValues from ChainWrapper
frinyvonnick Dec 5, 2017
189f790
:construction: Remove flow from ChainWrapper
frinyvonnick Dec 5, 2017
5024a7b
:construction: Remove concat from protect
frinyvonnick Dec 5, 2017
b009fb8
:construction: Remove get from protect
frinyvonnick Dec 5, 2017
e8fa63c
:construction: Remove isObject from protect
frinyvonnick Dec 5, 2017
9c754d0
:construction: Remove isEmpty from protect
frinyvonnick Dec 5, 2017
6eedc10
:construction: Remove map from using
frinyvonnick Dec 5, 2017
ea43778
:construction: Remove head from using
frinyvonnick Dec 5, 2017
9f38048
:construction: Remove concat from using
frinyvonnick Dec 5, 2017
8eac66b
:construction: Remove drop from using
frinyvonnick Dec 5, 2017
12bcc25
:construction: Remove get from using
frinyvonnick Dec 5, 2017
fee0325
:construction: Remove mapValues from using
frinyvonnick Dec 5, 2017
dc051d6
:construction: Remove omit from using
frinyvonnick Dec 5, 2017
ef53036
:construction: Remove isSymbol from using
frinyvonnick Dec 5, 2017
f9dc7be
🐛 toPath: Return empty path for nil values
nlepage Dec 6, 2017
e9a0cf5
🚧 Remove _.toPath from seq.ChainWrapper
nlepage Dec 6, 2017
25e6f70
♻ Use module resolver for convertLodashFp imports
nlepage Dec 7, 2017
b2c4219
♻ Manage non mutating methods in convertArrayMethod
nlepage Dec 7, 2017
68d4380
♻ toPath: Move nil arg concern from allowingArrays to stringToPath
nlepage Dec 7, 2017
b9ea095
:rotating_light: fix lint after rebase
nlepage Dec 7, 2017
c089577
:fire: Remove util namespace from lodash plugin
frinyvonnick Dec 7, 2017
e66179a
:hammer: Remove concat from empty array when uneccessary
frinyvonnick Dec 7, 2017
618b76d
:hammer: Use isSymbol from util/lang
frinyvonnick Dec 7, 2017
11b8249
:hammer: Replace drop by Array.slice
frinyvonnick Dec 7, 2017
ab822e0
:rewind: revert convertLodashFp deletion
nlepage Dec 7, 2017
2a64f35
🚚 Rename convert.js to convertLodashFp.js
nlepage Dec 7, 2017
e88d826
:heavy_minus_sign: Remove lodash from immutadot package
frinyvonnick Dec 7, 2017
175f1bc
♻ Replace concat one element by destructuring
nlepage Dec 7, 2017
5a9b131
♻ Inline flow in ChainWrapper
nlepage Dec 7, 2017
ad75038
♻ Replace omit by destructuring in ChainWrapper
nlepage Dec 7, 2017
df9c8b2
♻ Replace mapValues by a for in in ChainWrapper
nlepage Dec 7, 2017
5e1a98b
:recycle: Inline get and isEmpty in protect
frinyvonnick Dec 7, 2017
c3cf55a
:truck: Move isObject to util/lang
frinyvonnick Dec 7, 2017
39c03d1
:recycle: Use instanceof instead of typeof for isObject implementation
frinyvonnick Dec 7, 2017
ac0288d
♻ Replace omit by destructuring in UsingWrapper
nlepage Dec 7, 2017
93c2b20
♻ Add get to core for usage in UsingWrapper
nlepage Dec 7, 2017
a3232f3
💡 core.get documentation
nlepage Dec 7, 2017
a85d358
:pencil2: Fix typo in isObject jsdoc
frinyvonnick Dec 7, 2017
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 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 }
Loading