prairie v4.1.2
Prairie
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!
Table of Contents
- 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
createObj
Create a new object based path and value. Dot notation or an array of strings will result in nested objects.
Parameters
Examples
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.
toObject
Convert a collection into new object defined by path for key and value.
Parameters
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.
Examples
toObject('a', 'b', [{a: 'a1', b: 'b2'}]) // => { a1: 'b2' }Returns Object New object that is similar to map(getValue), keyBy(getKey).
setIn
Rearranged _.set args to setIn(path, object, value)
Type: function
Parameters
pathstring The path of the property to replace.objectObject The object that to set value on.valueany The value to place on path.
Examples
setIn('foo', {}, 'bar') // => { foo: 'bar' }setIn('a', { b: 1 }, 2) // => { a: 2, b: 1 }Returns Object New object with value set at path.
setVal
Rearranged _.set args to setVal(value, object, path)
Type: function
Examples
setVal(value, object, path)setState
Normal lodash _.set with no rearg. setVal(object, path, value)
Examples
setVal(object, path, value)setField
Set field. Like _.update but transformer is given the entire item instead of only the field.
Parameters
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.
addField
Set field like setField but only if it's value is empty.
Parameters
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.
setFieldHas
Replace field only if it is already set. Transformer given entire item.
Parameters
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.
setFieldWhen
Set field when boolCheck is true. Otherwise return item untouched.
Parameters
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.
replaceField
Replace field only if found. Transformer gets field value. Probably just use _.update() unless you want the check beforehand.
updateToWhen
Replace field with result of transformer when boolCheck return true.
Parameters
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.
Examples
const toArray = updateToWhen(Array, _.isPlainObject, 'foo')
toArray({ foo: { a: 'happy' } }) // => { foo: [{ a: 'happy' }] }Returns Object Item with conditional transformer applied to path.
updateTo
Rearranged _.update args to transformer, path, item
Parameters
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.
setFieldWith
Set field on item. Transformer given value of withId property.
Parameters
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.
mergeFields
Replace item with result of transformer.
Parameters
Examples
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.
mergeWith
Merge source on top of item.
Parameters
sourceObject Object to apply on top of item.itemObject Object that values of source will be applied.
Examples
mergeWith({ a: 1 })({ a: 2, b: 4 });
// => { a: 1, b: 4 }Returns Object Merged result of surce on top of item.
mergeFieldsWith
Replace item. Transformer given value of withId property.
Parameters
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
Copy value of getPath to setPath only if getPath finds something. Otherwise item left untouched.
Parameters
getPathstring The source path.setPathstring The destination path.itemObject The object to work with.
move
Move property from one names to another.
Parameters
getPathstring The source path.setPath(string | Function) The destination path.itemObject The object to work with.
Examples
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.
moveAll
Map some keys.
Parameters
renamerFunction The function to send each key. Should return new key string.renameKeysArray An array of source paths.itemObject The object to work with.
Examples
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.
renameFields
Move property from one names to another.
Parameters
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.
Examples
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.
findValueAt
Return the first value of paths. 0, null, and false are valid values.
Parameters
Examples
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.
findAt
Return the first truthy value of paths.
Parameters
Examples
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.
getFields
Return an object with same keys as object argument. Values replaced with result of value selector.
Parameters
structuredSelectorObject Object where each value is a selector accepting item.itemObject The object to work with.
Examples
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.
doProp
Return result of calling transformer with property value at path.
Parameters
transformerFunction Transformer given value at path of item.pathstring The path of the property to get.itemObject The item to get property value on.
Examples
doProp(_.isString, 'foo')({ foo: 'bar' }) // => truedoProp(_.isString, 'foo')({ foo: 2 }) // => falsepropDo
Return result of calling transformer with property value at path.
Parameters
pathstring The path of the property to get.transformerFunction Transformer given value at path of item.itemObject The item to get property value on.
Examples
propDo('foo', _.isString)({ foo: 'bar' }) // => truepropDo('foo', _.isString)({ foo: 2 }) // => falsedoPropOf
Create a function that will accept a path string and send its value of object to transformer.
Type: Function
hasMethodAt
Check if property has a method at path. An example of using doProp().
Type: Function
Examples
hasMethodAt(path)(object)hasMethodOf
Check if property at path is a function. An example of using doPropOf().
Type: Function
Examples
hasMethodAt(path)(object)transformHas
Replace entire item if has field. Transformer given value at path.
Type: Function
2 years ago
2 years ago
4 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago