Version 4 is ESM.
Add new fields/properties to an object based on the values of existing properties.
Composable utility functions that make it easy to edit/replace properties of objects. For best results learn about _.flow()
and read the Lodash FP Guide.
All functions have been curried so they can be called with one argument at a time.
If you have used _.set()
and _.update()
but want more this is the library for you!
- createObj
- toObject
- setIn
- setVal
- setState
- setField
- addField
- setFieldHas
- setFieldWhen
- replaceField
- updateToWhen
- updateTo
- setFieldWith
- mergeFields
- mergeWith
- mergeFieldsWith
- copy
- move
- moveAll
- renameFields
- findValueAt
- findAt
- getFields
- doProp
- propDo
- doPropOf
- hasMethodAt
- hasMethodOf
- transformHas
Create a new object based path and value. Dot notation or an array of strings will result in nested objects.
createObj('foo.bar', 'happy') // => { foo: { bar: 'happy' } }
createObj('foo', 'bar') // => { foo: 'bar' }
createObj('foo')('bar') // => { foo: 'bar' }
createObj('baz', { a: 1 }) // => { baz: { a: 1 } }
Returns Object New object with value
placed on the last key of path
.
Convert a collection into new object defined by path for key and value.
getKey
(string | Array | Function) The path used for key creation.getValue
(string | Array | Function) The path used for key creation.collection
any The thing used for value of key.
toObject('a', 'b', [{a: 'a1', b: 'b2'}]) // => { a1: 'b2' }
Returns Object New object that is similar to map(getValue), keyBy(getKey).
Rearranged _.set
args to setIn(path, object, value)
Type: function
path
string The path of the property to replace.object
Object The object that to set value on.value
any The value to place on path.
setIn('foo', {}, 'bar') // => { foo: 'bar' }
setIn('a', { b: 1 }, 2) // => { a: 2, b: 1 }
Returns Object New object with value
set at path
.
Rearranged _.set
args to setVal(value, object, path)
Type: function
setVal(value, object, path)
Normal lodash _.set with no rearg. setVal(object, path, value)
setVal(object, path, value)
Set field. Like _.update
but transformer is given the entire item instead of only the field.
path
string The path of the property to replace.transformer
Function Transformer given entire item. Return value set at path.item
Object The item to add or replace field on.
Returns Object Item with path
updated with result of transformer
.
Set field like setField
but only if it's value is empty.
path
string The path of the property to set.transformer
Function Transformer given entire item. Return value set at path.item
Object The item to update field on.
Replace field only if it is already set. Transformer given entire item.
path
string The path of the property to replace.transformer
Function Transformer given entire item. Return value set at path.item
Object The item to update field on.
Returns Object Item with path
updated with result of transformer
.
Set field when boolCheck is true. Otherwise return item untouched.
path
string The path of the property to set.transformer
Function Transformer given entire item. Should return value of path.boolCheck
Function A function that returns true when field should be set.item
Object The item to update field on.
Returns Object Item with path
updated with result of transformer
.
Replace field only if found. Transformer gets field value. Probably just use _.update() unless you want the check beforehand.
Replace field with result of transformer when boolCheck return true.
transformer
Function Transformer given value at path of item. Return replacement value.boolCheck
Function A function that returns true when field should be replaced.path
string The path of the property to update.item
Object The item to conditionally update field on.
const toArray = updateToWhen(Array, _.isPlainObject, 'foo')
toArray({ foo: { a: 'happy' } }) // => { foo: [{ a: 'happy' }] }
Returns Object Item with conditional transformer applied to path
.
Rearranged _.update args to transformer, path, item
transformer
Function Transformer given value at path of item. Return replacement value.path
string The path of the property to get.item
Object The item to update field on.
Returns Object Item with transformer applied to property at path
.
Set field on item. Transformer given value of withId property.
path
string The path of the property to get.withId
string The path of the property to send totransformer
.transformer
Function Transformer given value of withId property.
Returns ItemTransformer Result of transformer set at field
item
.
Replace item with result of transformer.
mergeFields(({ a, b }) => ({ a4: a * 4, b3: b * 3 }), { a: 2, b: 3 });
// => { a: 2, b: 3, a4: 8, b3: 9 }
Returns Object Merged result of transformer on top of item
.
Merge source on top of item.
source
Object Object to apply on top of item.item
Object Object that values of source will be applied.
mergeWith({ a: 1 })({ a: 2, b: 4 });
// => { a: 1, b: 4 }
Returns Object Merged result of surce
on top of item
.
Replace item. Transformer given value of withId property.
withId
string The path of the property to send totransformer
.transformer
Function Sent item property at path ofwithId
. Should return new Object.item
Object The object to work with.
Returns Object Result of transformer set at field
item
.
Copy value of getPath to setPath only if getPath finds something. Otherwise item left untouched.
getPath
string The source path.setPath
string The destination path.item
Object The object to work with.
Move property from one names to another.
getPath
string The source path.setPath
(string | Function) The destination path.item
Object The object to work with.
move('foo', 'bar', { foo: 1, baz: 2 }) // => { bar: 1, baz: 2 }
Returns Object Result after the move. Value at getPath
removed and added to setPath
.
Map some keys.
renamer
Function The function to send each key. Should return new key string.renameKeys
Array An array of source paths.item
Object The object to work with.
move('foo', 'bar', { foo: 1, baz: 2 }) // => { bar: 1, baz: 2 }
Returns Object Result after the move. Value at getPath
removed and added to setPath
.
Move property from one names to another.
renameObj
Object Object where each key will be moved to the value path. If value is a function it is sent the old key and will return the new one.item
Object The object to work with.
const rename = renameFields({ foo: 'bar', bin_baz: _.camelCase })
rename({ foo: 1, bin_baz: 2, bar: 3, other: 4 })
// => { bar: 1, binBaz: 2, other: 4 }
Returns Object Result after the renames.
Return the first value of paths. 0, null, and false are valid values.
findAt(['c', 'b', 'a'])({ a: 'foo', b: 'bar', c: null }) // => null
findAt(['c', 'b', 'a'])({ a: 'foo', b: false, c: '' }) // => false
Returns any The first truthy value found at one of the getPaths
.
Return the first truthy value of paths.
findAt(['c', 'b', 'a'])({ a: 'foo', b: 'bar', c: null }) // => 'bar'
findAt(['c', 'b', 'a'])({ a: 'foo', b: false, c: '' }) // => 'foo'
Returns any The first truthy value found at one of the getPaths
.
Return an object with same keys as object argument. Values replaced with result of value selector.
structuredSelector
Object Object where each value is a selector accepting item.item
Object The object to work with.
getFields({bar: _.get('foo')}, { foo: 'happy'}) // => { bar: 'happy' }
getFields({bar: 'foo'})({ foo: 'happy'}) // => { bar: 'happy' }
Returns Object2 Result after each value is passed the item.
Return result of calling transformer with property value at path.
transformer
Function Transformer given value at path of item.path
string The path of the property to get.item
Object The item to get property value on.
doProp(_.isString, 'foo')({ foo: 'bar' }) // => true
doProp(_.isString, 'foo')({ foo: 2 }) // => false
Return result of calling transformer with property value at path.
path
string The path of the property to get.transformer
Function Transformer given value at path of item.item
Object The item to get property value on.
propDo('foo', _.isString)({ foo: 'bar' }) // => true
propDo('foo', _.isString)({ foo: 2 }) // => false
Create a function that will accept a path string and send its value of object to transformer.
Type: Function
Check if property has a method at path. An example of using doProp()
.
Type: Function
hasMethodAt(path)(object)
Check if property at path is a function. An example of using doPropOf()
.
Type: Function
hasMethodAt(path)(object)
Replace entire item if has field. Transformer given value at path.
Type: Function