entrust
v0.0.21
Published
delegatee-last structure for curried functions
Downloads
46
Maintainers
Readme
Delegatee-last curried functions for great justice
You can use entrust
to easily transform methods into a curried, delegatee-last (data-last) form. If you are used to functional programming (FP) paradigms, this makes FP in JS much easier.
Example:
// native map looks like: array.map(fn)
// entrust map looks like: e1(`map`, fn, array)
import {e1} from 'entrust'
const map = e1(`map`) // this takes a function first, and then the data
map(x => x * 2, [1,2,3]) // [2,4,6]
Here's an example of how to apply the same approach to a tertiary function, like reduce
:
import {e2} from 'entrust'
// native reduce looks like: array.reduce(fn, initial)
const reduce = e2(`reduce`) // e2(`reduce`, fn, initial, array)
const sum = reduce((a, b) => a + b, 0)
sum([1,2,3]) // 6
sum([1,2,3,4]) // 10
sum([1,2,3,4,5]) // 15
NB: This approach is used extensively in the f-utility
module. You may want to take a look at it here.
entrust
is built using katsu-curry
's curry
implementation. However, if you would like to use a different curry implementation, this is easily accomplished using the custom API. This specific technque is used to generate entrust's "debug-mode" export: entrust/debug.js
.
API
Table of Contents
custom
generate a customized version of entrust's API
Parameters
curry
function a function which curries
Examples
import {custom} from 'entrust'
import curry from 'lodash/fp/curry'
const {e0} = custom(curry)
Returns object raw+ an augmented version of the raw API
eN
invoke a delegated method with arguments as an array. enforces specific arity
Parameters
n
number 0 - 10method
string a function name on your delegateeargs
Array arguments to pass to your delegatee's methoddelegatee
any something with methods
Examples
import {eN} from 'entrust'
eN(0, `toUpperCase`, [], `cool`) // `COOL`
eN(1, `map`, [(x) => x * 2], [1,2,3]) // [2,4,6]
eN(2, `reduce`, [(a, b) => (a + b), 0], [1,2,3]) // 6
Returns any the result of delegating to the method with some arguments
eD
invoke a delegated method with arguments as an array. enforces specific arity Yells at you if you give arguments that don't match the expected arity.
Parameters
n
number 0 - 10method
string a function name on your delegateeargs
Array arguments to pass to your delegatee's methoddelegatee
any something with methods
Examples
import {eD} from 'entrust'
eD(0, `toUpperCase`, [], `cool`) // `COOL`
eD(1, `map`, [(x) => x * 2], [1,2,3]) // [2,4,6]
eD(2, `reduce`, [(a, b) => (a + b), 0], [1,2,3]) // 6
eD(2, `reduce`, [(a, b) => (a + b)], [1, 2, 3]) // throws error
Returns any the result of delegating to the method with some arguments
e0
Parameters
Examples
import {e0} from 'entrust'
const toLowerCase = e0(`toLowerCase`)
toLowerCase(`COOL`) // cool
Returns any
e1
Parameters
Examples
import {e1} from 'entrust'
const split = e1(`split`)
split(`:`, `c:o:o:l`) // [`c`,`o`,`o`,`l`]
Returns any
e10
Parameters
fn
string a function namea
any some parameterb
any some parameterc
any some parameterd
any some parametere
any some parameterf
any some parameterg
any some parameterh
any some parameteri
any some parameterj
any some parameterx
Object an object
Returns any
e2
Parameters
Examples
import {e2} from 'entrust'
const replace = e2(`replace`)
replace(`old`, `new`, `oldnew`) // newnew
Returns any
e3
Parameters
fn
string a function namea
any some parameterb
any some parameterc
any some parameterx
Object an object
Returns any
e4
Parameters
fn
string a function namea
any some parameterb
any some parameterc
any some parameterd
any some parameterx
Object an object
Returns any
e5
Parameters
fn
string a function namea
any some parameterb
any some parameterc
any some parameterd
any some parametere
any some parameterx
Object an object
Returns any
e6
Parameters
fn
string a function namea
any some parameterb
any some parameterc
any some parameterd
any some parametere
any some parameterf
any some parameterx
Object an object
Returns any
e7
Parameters
fn
string a function namea
any some parameterb
any some parameterc
any some parameterd
any some parametere
any some parameterf
any some parameterg
any some parameterx
Object an object
Returns any
e8
Parameters
fn
string a function namea
any some parameterb
any some parameterc
any some parameterd
any some parametere
any some parameterf
any some parameterg
any some parameterh
any some parameterx
Object an object
Returns any
e9
Parameters
fn
string a function namea
any some parameterb
any some parameterc
any some parameterd
any some parametere
any some parameterf
any some parameterg
any some parameterh
any some parameteri
any some parameterx
Object an object
Returns any