2.4.0 • Published 7 years ago
cape-lodash v2.4.0
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 andcurryapplied.condId- Each arg should be an array pair. Applied to_.condwith[ 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)- Likerenamebut creates a new objected limited to values of renameObj.renameValues(renameObj)(strArray)- renameObj = { find: 'replace' } Similar torenamebut find/replace strings in array.keyWithField('id', 'value')(dataArray)- Creates a key/value index from an array of items. It's like_.keyBybut uses the field value ofvalueproperty 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.setwith normal argument order.setKey(path, state, value)-setbut with different arg order.setKeyVal(key, value, state)-setbut with different arg order.setVal(value, state, key)setField(path, transformer)(item)- Thetransformerfunction 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. Thetransformergets the value of path. Transformer must return new value that will set to path.setWith(fieldId, withId, transformer)- Thetransformergets 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 astransformPropbut swap place of object/path.hasMethodAt(path)(object)Returns function that evaluates if passed object has a function at path.hasMethodOf(object)(path)Same ashasMethodAtwith 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.invokeArginvokes the first argument as a function.invokeNthArginvokes 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.divideBydivideBy(2)(4) = 2isLtisLt(4)(2) = trueisGtisGt(2)(3) = truehasOfhasOf({foo: true, bar: false})('foo') = true
2.4.0
7 years ago
2.3.0
7 years ago
2.2.1
7 years ago
2.2.0
7 years ago
2.1.0
7 years ago
1.13.2
8 years ago
1.13.1
8 years ago
1.13.0
8 years ago
1.12.1
8 years ago
1.12.0
9 years ago
1.11.0
9 years ago
1.10.0
9 years ago
1.9.1
9 years ago
1.9.0
9 years ago
1.8.0
9 years ago
1.7.0
9 years ago
1.6.0
9 years ago
1.5.0
9 years ago
1.4.1
9 years ago
1.4.0
9 years ago
1.3.0
9 years ago
1.2.1
9 years ago
1.2.0
9 years ago
1.1.1
9 years ago
1.1.0
9 years ago
1.0.0
9 years ago