cape-lodash
v2.4.0
Published
A few lodash/fp inspired helper utility functions.
Downloads
288
Readme
cape-lodash v2.4.0
A few lodash inspired utility functions. For best results learn about _.flow()
and read the Lodash FP Guide.
Install ESLint rules for lodash/fp by extending eslint with plugin:lodash-fp/recommended
and including the lodash-fp
plugin.
Bool
isFalse()
- Returns true if sent a value that is exactlyfalse
.isTrue()
- Returns true if sent a value that is exactlytrue
.hasSize()
- Opposite ofisEmpty
.parseBoolean()
- Takes a string and returns false unless word is in word list webmasterkai/affirmativetoBool({})
- Turn empty objects and arrays to false. Sends strings toparseBoolean
. Coerce other values into a boolean.
Changes
changeChecker(initValue)
- Returns function that accepts new val and return true if changed.handleChanges(getValue, onChange)
- Give it a getter and an onChange callback. It will return a function. Call returned function on every possible change and it will conditionally call onChange when it finds a change.
Cond
branch(bool, trueVal, falseVal)
Ternary (bool ? trueVal : falseVal
) in function form.fpBranch(trueVal, falseVal, boolVal)
Same as above with arguments order changed andcurry
applied.condId
- Each arg should be an array pair. Applied to_.cond
with[ stubTrue, identity ]
added as last option.overBranch(boolCheck, getTrue, getFalse)
Dots
copy(getKey, setKey, source, target)
- Value of getKey within source is added to target at setKey unless it is undefined.move(getKey, setKey, object)
Place value of getKey path within object to setKey path.rename(renameObj, source)
- renameObj = { getKey: 'setKey' }renamePick(renameObj, source)
- Likerename
but creates a new objected limited to values of renameObj.renameValues(renameObj)(strArray)
- renameObj = { find: 'replace' } Similar torename
but find/replace strings in array.keyWithField('id', 'value')(dataArray)
- Creates a key/value index from an array of items. It's like_.keyBy
but uses the field value ofvalue
property instead of the entire item.
Merge
merge(object, ...sources)
Set
When you want to edit a property and return a new object instead of mutating.
setSimple(state, key, value)
-_fp.set
with normal argument order.setKey(path, state, value)
-set
but with different arg order.setKeyVal(key, value, state)
-set
but with different arg order.setVal(value, state, key)
setField(path, transformer)(item)
- Thetransformer
function accepts item and should return the new value ofpath
.setFieldHas(path, transformer)(item)
- Only updates if item has path. Transformer is sent entire item.replaceField(path, transformer)(item)
- Similar tosetFieldHas
. Thetransformer
gets the value of path. Transformer must return new value that will set to path.setWith(fieldId, withId, transformer)
- Thetransformer
gets the value ofwithId
. Must return new value that will set tofieldId
.
Transform
transformProp(transformer, path)
- curried. Creates a function that returns transformation of object property at path.transformPropOf(transformer, object)
- curried. Same astransformProp
but swap place of object/path.hasMethodAt(path)(object)
Returns function that evaluates if passed object has a function at path.hasMethodOf(object)(path)
Same ashasMethodAt
with object/path flip.
Utils
Please view source for explanation and tests for usage.
createObj(key, value)
- curried. Creates a new object with one key/val.invokeArg
invokes the first argument as a function.invokeNthArg
invokes the function at index.firstValArg(0, '', 'foo')
- Find the first truthy argument value.getDefault('default.path', 'check.path.first')
- Select two paths, send to_fp.defaultTo
. Default path first.oneOf([ 'array', 'of', 'options' ])
- Returns func that will return true if array contains argument.divideBy
divideBy(2)(4) = 2isLt
isLt(4)(2) = trueisGt
isGt(2)(3) = truehasOf
hasOf({foo: true, bar: false})('foo') = true