Curried
Awesome curried standard library.
Installation
In your project folder:
npm install curried --save
In a file:
var _ = require('curried');
API
FUNCTION
Functions that produce functions - the heart of the library in many senses!
curry
Lifted directly from npm curry; this is the function this library uses to produce its own curried functions.
compose
Chains together functions from right to left, passing the value each function produces into the next:
var { return str + 'hell yea!' };var { return str + ' ' };var { return str };var hellYea = _; //= 'FUNCTIONS! hell yea!'
The above code is the same as appendWithHellYea(appendSpace(shout('functions!')))
pipe
To a whole lot of people, compose looks backwards. Pipe is compose the 'right way around', sorta like unix pipes.
var { return str + 'hell yea!' };var { return str + ' ' };var { return str };var hellYea = _; //= 'FUNCTIONS! hell yea!'
negate
Returns a function that returns false
when the original returned true
, and vice versa.
var { return !!a };var isFalsey = _; //= true //= false //= true //= false //= false
flip
Returns a function with the argument order flipped
var prependWith = _;var appendWith = _;var appendIsm = ; // 'functionalism'
identity
Returns the value passed into it.
var o = {}_ === o //= true
In the functional world - where a function is often passed in to do some processing - it's the equivalent of a no-op.
var passCollectionThrough = _; //= [1, 2, 3] var pointlesslyComplexIdentity = _; //= 'a'
tap
If you're writing composition-heavy code, sometime's it's really important to be able to inject a step in the middle for debugging purposes.
var log = _;var shout = _;var appendWith = _; _'log this' //= 'LOG THIS!'// console.log logs 'LOG THIS' - without the trailing !
constant
Creates a function that always returns the same value.
//= 'a'
This can be particularly useful if you're expected to return a function, but really just want a value. For instance, handling values in promise chains:
COLLECTION
Collection functions work on Arrays AND objects.
map
var mapInc = _; //= [2, 3, 4] //= { x: 2, y: 3, z: 4 }
filter
var { return typeof a === 'string' };var filterString = _; //= ['a', 'b'] //= { z: 'a', b: 'b' }
reject
var { return typeof a === 'string' };var filterNotString = _; //= [1, 2, 3] //= { x: 1, y: 2, a: 3 }
every
var { return typeof a === 'string' };var allString = _; //= false //= true //= false //= true
some
var { return typeof a === 'string' };var someString = _; //= true //= false //= true //= false
reduce
var cat = _; //= 'abc'
Since objects don't have guaranteed order in ECMAScript, it's only safe to reduce over objects with operations that don't need arguments in any particular order (commutative).
var sum = _; //= 6
reduceRight
var reverseCat = _; //= 'cba' var sum = _; //= 6
reduceFrom
var catWithPrefix = _; //= 'super awesome abc' var sumFrom3 = _; //= 9
reduceRightFrom
var reverseCatWithPrefix = _; //= 'super awesome cba' var sumFrom3 = _; //= 9
OBJECT
Works on objects - or things that act like an object (i.e. have properties).
invoke
var toString = _; 'abc' 1 true {} //= ['abc', '1', 'true', '[object Object]']
invokeWith
var mapInc = _; //= [2, 3, 4]
get
var getX = _; //= 2
pick
var pick2DCoords = _;var coords3D = x: 1 y: 2 z: 200 ; //= { x: 1, y: 2 }
combine
var defaults = _;
keys
_ //= ['x', 'y', 'z']
values
_ //= [1, 2, 3]
ARRAY
For those functions that only make sense with an ordered list.
take
var take5 = _; //= [1, 2, 3, 4, 5]
head
_ //= 1_ //= undefined
tail
_ //= [2, 3]_ //= []_ //= []
initial
_ //= [1, 2]_ //= []_ //= []
last
_ //= 3_ //= undefined