Skip to content

Commit

Permalink
✨ core.unset without _ (#123)
Browse files Browse the repository at this point in the history
* 🔨 Give more control to apply operation

* ✨ add core.unset
  • Loading branch information
nlepage committed Oct 31, 2017
1 parent 8e6e3ec commit 7d95006
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 23 deletions.
18 changes: 11 additions & 7 deletions src/core/apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ const callback = (obj, prop) => {
* Operation to apply on a nested property of an object, to be called by {@link core.apply|apply}.
* @memberof core
* @callback operation
* @param {*} value The resolved unwrapped object
* @param {*} obj The last nested object
* @param {string|number|Array<number>} prop The prop of the last nested object
* @param {*} value The value of the prop
* @returns {*} Result of the operation
* @private
* @since 0.4.0
Expand All @@ -50,16 +52,18 @@ const callback = (obj, prop) => {
*/
const apply = (obj, path, operation) => {
const walkPath = (curObj, curPath) => {
if (curPath.length === 0) return operation(curObj)

const [prop] = curPath
const [prop, ...pathRest] = curPath

const value = callback(curObj, prop)

const newValue = walkPath(value, curPath.slice(1))

const newObj = copy(curObj, isArrayProp(prop))
newObj[prop] = newValue

if (curPath.length === 1) {
operation(newObj, prop, value)
return newObj
}

newObj[prop] = walkPath(value, pathRest)

return newObj
}
Expand Down
2 changes: 2 additions & 0 deletions src/core/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { convert } from './convert'
import { set } from './set'
import { toPath } from './toPath'
import { unset } from './unset'
import { update } from './update'

/**
Expand All @@ -12,5 +13,6 @@ export {
convert,
set,
toPath,
unset,
update,
}
4 changes: 3 additions & 1 deletion src/core/set.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { apply } from './apply'
import { unsafeToPath } from './toPath'

const makeSetOperation = value => (obj, prop) => { obj[prop] = value }

/**
* Sets the value at <code>path</code> of <code>object</code>.
* @function
Expand All @@ -12,6 +14,6 @@ import { unsafeToPath } from './toPath'
* @example set({ nested: { prop: 'old' } }, 'nested.prop', 'new') // => { nested: { prop: 'new' } }
* @since 0.4.0
*/
const set = (obj, path, value) => apply(obj, unsafeToPath(path), () => value)
const set = (obj, path, value) => apply(obj, unsafeToPath(path), makeSetOperation(value))

export { set }
19 changes: 19 additions & 0 deletions src/core/unset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { apply } from './apply'
import { unsafeToPath } from './toPath'

const unsetOperation = (obj, prop) => { delete obj[prop] }

/**
* Removes the property at <code>path</code> of <code>object</code>.
* This is the <code>lodash/fp</code> <code>unset</code>, with no arguments rearranging and no currying.
* @function
* @memberof core
* @param {Object} obj The object to modify.
* @param {Array|string} path The path of the property to unset.
* @return {Object} Returns the updated object.
* @example unset({ nested: { prop: 'value' } }, 'nested.prop') // => { nested: {} }
* @since 0.4.0
*/
const unset = (obj, path) => apply(obj, unsafeToPath(path), unsetOperation)

export { unset }
2 changes: 1 addition & 1 deletion src/object/unset.spec.js → src/core/unset.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-env jest */
import { immutaTest } from 'test.utils'
import { unset } from 'object'
import { unset } from 'core'

describe('Unset', () => {

Expand Down
4 changes: 3 additions & 1 deletion src/core/update.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { apply } from './apply'
import { unsafeToPath } from './toPath'

const makeUpdateOperation = (updater, args) => (obj, prop, value) => { obj[prop] = updater(value, ...args) }

/**
* Updates the value at <code>path</code> of <code>object</code> using the <code>updater</code> function.<br/>
* The updater is invoked with <code>value</code> and <code>…args</code>.<br/>
Expand All @@ -22,7 +24,7 @@ import { unsafeToPath } from './toPath'
const update = (obj, path, updater, ...args) => apply(
obj,
unsafeToPath(path),
curObj => updater(curObj, ...args),
makeUpdateOperation(updater, args),
)

export { update }
15 changes: 4 additions & 11 deletions src/object/unset.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import _unset from 'lodash/fp/unset'
import { lodashFpConvert } from 'util/lodashFpConvert'
import { unset } from 'core/unset'

/**
* Removes the property at <code>path</code> of <code>object</code>.
* This is the <code>lodash/fp</code> <code>unset</code>, with no arguments rearranging and no currying.
* @function
* This is an alias for {@link core.unset}.
* @function unset
* @memberof object
* @param {Object} object The object to modify.
* @param {Array|string} path The path of the property to unset.
* @return {Object} Returns the updated object.
* @example unset({ nested: { prop: 'value' } }, 'nested.prop') // => { nested: {} }
* @see {@link https://lodash.com/docs#unset|lodash.unset} for more information.
* @since 0.1.5
* @deprecated Use {@link core.unset}
*/
const unset = lodashFpConvert(_unset)
export { unset }
2 changes: 1 addition & 1 deletion src/seq/ChainWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class ChainWrapper {
omit(core, ['convert', 'toPath']),
lang,
math,
omit(object, ['set', 'update']),
omit(object, ['set', 'unset', 'update']),
string,
].forEach(namespace => Object.assign(
ChainWrapper.prototype,
Expand Down
2 changes: 1 addition & 1 deletion src/util/UsingWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class UsingWrapper {
[
array,
collection,
omit(core, ['convert', 'toPath']),
omit(core, ['convert', 'unset', 'toPath']),
lang,
math,
omit(object, ['set', 'unset', 'update']),
Expand Down

0 comments on commit 7d95006

Please sign in to comment.