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.collectionany 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
pathstring The path of the property to replace.objectObject The object that to set value on.valueany 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.
pathstring The path of the property to replace.transformerFunction Transformer given entire item. Return value set at path.itemObject 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.
pathstring The path of the property to set.transformerFunction Transformer given entire item. Return value set at path.itemObject The item to update field on.
Replace field only if it is already set. Transformer given entire item.
pathstring The path of the property to replace.transformerFunction Transformer given entire item. Return value set at path.itemObject 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.
pathstring The path of the property to set.transformerFunction Transformer given entire item. Should return value of path.boolCheckFunction A function that returns true when field should be set.itemObject 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.
transformerFunction Transformer given value at path of item. Return replacement value.boolCheckFunction A function that returns true when field should be replaced.pathstring The path of the property to update.itemObject 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
transformerFunction Transformer given value at path of item. Return replacement value.pathstring The path of the property to get.itemObject 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.
pathstring The path of the property to get.withIdstring The path of the property to send totransformer.transformerFunction 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.
sourceObject Object to apply on top of item.itemObject 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.
withIdstring The path of the property to send totransformer.transformerFunction Sent item property at path ofwithId. Should return new Object.itemObject 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.
getPathstring The source path.setPathstring The destination path.itemObject The object to work with.
Move property from one names to another.
getPathstring The source path.setPath(string | Function) The destination path.itemObject 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.
renamerFunction The function to send each key. Should return new key string.renameKeysArray An array of source paths.itemObject 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.
renameObjObject 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.itemObject 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 }) // => nullfindAt(['c', 'b', 'a'])({ a: 'foo', b: false, c: '' }) // => falseReturns 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.
structuredSelectorObject Object where each value is a selector accepting item.itemObject 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.
transformerFunction Transformer given value at path of item.pathstring The path of the property to get.itemObject The item to get property value on.
doProp(_.isString, 'foo')({ foo: 'bar' }) // => truedoProp(_.isString, 'foo')({ foo: 2 }) // => falseReturn result of calling transformer with property value at path.
pathstring The path of the property to get.transformerFunction Transformer given value at path of item.itemObject The item to get property value on.
propDo('foo', _.isString)({ foo: 'bar' }) // => truepropDo('foo', _.isString)({ foo: 2 }) // => falseCreate 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