Minimal version of the enjoy.js library. Contains probably all of the stuff to meet your daily functional programming needs. Treats arrays and objects in the same way whenever possible. Shuns the dreaded this
. Makes composition and partial application as easy as it gets. And yes, it also has object-less versions of commonly used methods and turns most operators into functions.
npm install --save enjoy-core
Calls fn
using the values of args
as arguments. No this
.
Wraps fn
so that if it gets called with only one argument and that argument is a function, a
partial application of fn
is returned instead of calling fn
.
Binds any number of arguments to fn
. No this
.
Calls fn
using the other arguments supplied to call
.
Composes all of the supplied functions so that if you call the resulting function with a value
it does the same thing as pipe
.
Reverses fn
's order of arguments.
Frees a method from the shackles of object-orientation by making it a free function where the first argument is an object compatible with the method's original object.
Binds all of the other non-undefined
arguments to fn
:
var forEachInCollection = partial(each, undefined, myCollection);
Calls fn1
with value
, then fn2
with the resulting value, then does the same for all of
the supplied functions and returns the result.
pipe([3, 1, 2], sort, reverse); // [3, 2, 1]
Calls fn
again and again until it returns false
.
Calls fn
n
times. If called with only fn
, a partial application is returned.
Retrieves the value at key
from a collection
.
Checks whether item
is contained in collection
.
Calls fn
with each item in collection
. The fn
callback has this signature:
fn(item, key, collection)
Turns an array of objects into an object where the keys are the values of a property of the objects contained within the original array.
Example:
[{name: "foo"},{name: "bar"}] => {foo: {name: "foo"}, bar: {name: "bar"}}
Returns true
if fn
returns a truthy value for each item in collection
. The execution is cut short when fn
returns a falsy value for an item in the collection.
Returns an array containing only those items from collection
that pass the test function fn
.
Returns the first item in collection
that passes the test function fn
.
Creates a getter function that returns an item from collection
given its key.
Checks if collection
contains an item for key key
.
Creates a string containing every item in collection
, separated by glue
. If no glue
is supplied, an empty string is used.
Returns an array containing collection
's keys.
Returns an array where every item from collection
has been modified using fn
.
Creates a function that returns the item at key
from a given collection.
any -> (collection -> any)
Returns an array containing all the items at key
for every collection in collection
.
collection -> string -> [any]
pluck([["foo", "bar"], [1, 2]], 1); // ["bar", 2]
pluck([{"foo": "bar"}, {"foo": "baz"}], "foo"); // ["bar", "baz"]
Turns an object into an array by putting its keys into the objects contained within the array.
Example:
{foo: {}, bar: {}} => [{name: "foo"},{name: "bar"}]
Sets collection
's value of key
to value
or adds it if the key doesn't exist yet.
Creates a function that sets the value at key
for a given collection and value:
any -> (collection -> any -> undefined)
Reduces collection
to a single value using the fn
function. Function fn
has this signature:
fn(previousValue, currentValue, key, collection);
If no initial value is supplied, fn
gets first called for the second item in the collection and the previous value is the value of the first item as-is.
Returns true
if at least one item in collection
satisfies the testing function fn
. Execution is cut short at the first item for which fn
returns a truthy value.
Creates a setter function for a collection that sets the value at some key or adds it. The returned function has this signature:
fn(key, value);
Returns an array containing all of collection
's items.
Reverses the order of items in the collection. Does not mutate the original like Array.prototype.reverse
!
Sorts collection
using fn
or, if no fn
is supplied, the built-in sort function of Array.prototype.sort
. Does not mutate the original!
Splits s
into an array at each occurrence of separator
.
Adds all of its arguments together.
Divides any number of arguments from left to right.
A functional try-catch: If fn
throws, execute instead
.
Reduces the arguments by applying the modulo ("%") operator to each and the next.
Multiplies its arguments.
Applies the not operator ("!") to a
.
Subtracts one or more numbers from n1
.