npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@hamlab/data-transformations

v1.0.29

Published

A practical functional library for data transformations in JavaScript.

Downloads

5

Readme

data-transformations

Description

A practical functional library for data transformations in JavaScript.

Take a look at the interactive documentation

Installation

To use with node:

$ npm install @hamlab/data-transformations

Then in the console:

const DT = require('@hamlab/data-transformations');
const { map } = require('@hamlab/data-transformations/map');
...

Documentation

Table of Contents

bind

src/bind.js:24-30

[Curried function]

Creates a function that is bound to a context. Note: bind does not provide the additional argument-binding capabilities of Function.prototype.bind.

Parameters

  • fn Function The function to bind to context
  • thisObj Object The context to bind fn to

Examples

const log = bind(console.log, console);
pipe(inc, log, double)(2); //=> 6
// logs: 3

Returns Function A function that will execute in the context of thisObj.

complement

src/complement.js:24-26

Takes a function f and returns a function g such that:

  • applying g to zero or more arguments will give true if applying the same arguments to f gives a logical false value; and

  • applying g to zero or more arguments will give false if applying the same arguments to f gives a logical true value.

Parameters

Examples

const isEven = (n) => n % 2 === 0;
const isOdd = complement(isEven);

isOdd(21); //=> true
isOdd(42); //=> false

Returns Function

compose

src/compose.js:18-26

  • See: pipe

Performs right-to-left function composition. The rightmost function may have any arity; the remaining functions must be unary.

Parameters

Examples

const inc = x => x + 1:
const negate = x => -x;

var f = compose(inc, negate, Math.pow);
f(3, 4); // -(3^4) + 1

Returns Function a function that represents the composition of the arguments

composeAsync

src/composeAsync.js:25-43

  • See: pipeAsync

Performs right-to-left function composition. The rightmost function may have any arity; the remaining functions must be unary. If any function returns a promise, it chains remaining ones

Parameters

Examples

var head = ([h]) => h;
var process = (a) => Promise.resolve(a);
var promiseAll = Promise.all.bind(Promise);

var f = composeAsync(head, promiseAll, process);

await f([1, 2]); // Promise.all([process(1), process(2)])[0] => 1

Returns Function a function that represents the composition of the arguments

curry

src/curry.js:27-35

  • See: curryN

Returns a curried equivalent of the provided function. The curried function has two unusual capabilities. First, its arguments needn't be provided one at a time. If f is a ternary function and g is curry(f), the following are equivalent:

  • g(1)(2)(3)
  • g(1)(2, 3)
  • g(1, 2)(3)
  • g(1, 2, 3)

Parameters

  • fn Function The function to curry.
  • initialArgs ...any The fn initial arguments.

Examples

var addFourNumbers = function(a, b, c, d) {
  return a + b + c + d;
};
var curriedAddFourNumbers = curry(addFourNumbers);
var f = curriedAddFourNumbers(1, 2);
var g = f(3);
g(4); //=> 10

Returns Function A new, curried function.

curryN

src/curryN.js:31-39

  • See: curry

Returns a curried equivalent of the provided function, with the specified arity. The curried function has two unusual capabilities. First, its arguments needn't be provided one at a time. If g is curryN(3, f), the following are equivalent:

  • g(1)(2)(3)
  • g(1)(2, 3)
  • g(1, 2)(3)
  • g(1, 2, 3)

Parameters

  • arity Number The arity for the returned function.
  • fn Function The function to curry.
  • initialArgs ...any The function initial arguments.

Examples

var addFourNumbers = function(a, b, c, d = 0) {
  return a + b + c + d;
};

var curriedAddFourNumbers = curryN(3, addFourNumbers);
var f = curriedAddFourNumbers(1, 2);
f(3); //=> 6

Returns Function A new, curried function.

filter

src/filter.js:39-55

  • See: transduce

[Curried function]

Returns a new list containing only those items that match a given predicate function. The predicate function is passed: (value, index, collection). in the case of filtering and object, the predicate function is passed: (value, key, collection).

Note that filter does not skip deleted or unassigned indices, unlike the native Array.prototype.filter method. For more details on this behavior, see: Array.prototype.filter

Acts as a transducer if a transformer is given as second parameter.

Parameters

  • fn Function The function called per iteration.
  • collection Collection? The collection to iterate over.

Examples

const isEven = (n) => n % 2 === 0;
const isValueEven = ([k, v]) => isEven(v)
const isVowel = (c) => /[aeiou]/.test(c);

filter(isVowel, 'string'); //=> 'i'
filter(isEven, [1, 2, 3]); //=> [2]
filter(isEven, Set[1, 2, 3]); //=> Set[2]
filter(isEven, function* () { yield* [1, 2, 3]}); //=> [2]
filter(isEven, { a: 1, b: 2 }); //=> { b: 2 }
filter(isValueEven, Map{ a: 1, b: 2 }); //=> Map{ b: 2 }

Returns (Collection | Function) The new filtered array.

Transducer

src/internal/typedef.js:1-17

see The Transducer Protocol

Type: Object

Properties

  • null Function @@transducer/init - Function: () => any, Hook called at the beginning to compute the initial state
  • null Function @@transducer/result - Function: (state) => any, Hook called when the result is computed
  • null Function @@transducer/step - Function: (state, value, index, collection) => any, Hook called at every iteration

Collection

src/internal/typedef.js:1-17

Type: (Array | Object | Transducer | Set | Map | string | Iterator | GeneratorFunction | Generator | Iterable)

Into

src/into.js:54-91

Type: Object

Properties

  • array function (Function, Collection): Array? Function: (value, index, collection, accumulator) => any, Merge strategy: Array.push
  • string function (Function, Collection): string? Function: (value, index, collection, accumulator) => any, Merge strategy: concatenation
  • number function (Function, Collection): number? Function: (value, index, collection, accumulator) => any, Merge strategy: last returned value becomes the accumulator
  • set function (Function, Collection): Set? Function: (value, index, collection, accumulator) => any, Merge strategy: Set.add
  • map function (Function, Collection): Map? Function: ([key, value], index, collection, accumulator) => [any, any], Merge strategy: Map.add
  • object function (Function, Collection): Object? Function: ([key, value], index, collection, accumulator) => [any, any], Merge strategy: object assignation

into

src/into.js:54-91

Transforms the items of the iterable with the transducer and appends the transformed items to the accumulator using an appropriate iterator function based on the accumulator type.

The transducer function takes 4 params: (value, index, collection, accumulator). Then value parameter is a pair [key, value] when the accumulator is an Object or a Map.

In the case the accumulator is a number (into.number), there is no merge strategy and just returns what the transducer returns

The iteration is performed with reduce after initializing the transducer.

All properties are curried.

Type: Into

Examples

var value = ([k, v]] => v
var isEven = (x) => x % 2 === 0
var isVowel = (x) => /aeiou/.test(x)
var isObject = (x) => typeof x === 'object'
var toUppercase = (x) => x.toUppercase()
var addAccumulator = (value, index, collection, accumulator) => value + accumulator
var pairById = (x) => [x.id, x]

into.array(compose(map(value), filter(isEven)), {a: 1, b: 2}) // [2]

into.string(compose(filter(isVowel), map(toUppercase)), 'string') // 'I'

into.number(map(addAccumulator), [1, 2, 3]) // 1 + 2 + 3 = 6

into.object(
  compose(filter(isObject), map(pairById)),
    [1, { id: '1-2-3', value: 10 }]
  ) // { '1-2-3': { id: '1-2-3', value: 10 } }

is

src/is.js:142-177

[Curried function]

See if an object (val) is an instance of the supplied constructor. This function will check up the inheritance chain, if any.

  • Additionally, it has the following methods for nicer and more semantic type checking: array - function - functor - filterable - generator - iterator - map - object (strict checking) - promise - set - string - thenable - transducer - type - strict - nil - not (negates any of the previous ones)
  • @func

Parameters

  • ctor Object A constructor
  • val any The value to test

Properties

  • array function (any): boolean? Checks if is an Array

  • function function (any): boolean? Checks if is type function

  • functor function (any): boolean? Checks if it has a map method

  • filterable function (any): boolean? Checks if it has a filter method

  • generator function (any): boolean? Checks if it is a generator function

  • iterator function (any): boolean? Checks if it has [Symbol.iterator] method

  • map function (any): boolean? Checks if it is a Map or an instance of a Map

  • object function (any): boolean? Checks if the constructor equals to Object (if it is strictly an object)

  • promise function (any): boolean? Checks if it is a Promise or an instance of a Promise

  • set function (any): boolean? Checks if it is a Set or an instance of a Set

  • string function (any): boolean? Checks if it is boolean type

  • thenable function (any): boolean? Checks if it has a then method

  • transducer function (any): boolean? Checks if it is an object that follows the transducer protocol

  • type function (string, any): boolean? Checks if the type matches

  • strict function (Function, any): boolean? Checks if the constructor function matches

  • nil function (any): boolean? Checks if it is null or undefined

  • not Object? negates the following method

    • not.array function (any): boolean? Checks if is not an Array
    • not.function function (any): boolean? Checks if is not type function
    • not.functor function (any): boolean? Checks if it has not a map method
    • not.filterable function (any): boolean? Checks if it has not a filter method
    • not.generator function (any): boolean? Checks if it is not a generator function
    • not.iterator function (any): boolean? Checks if it has not [Symbol.iterator] method
    • not.map function (any): boolean? Checks if it is not a Map nor an instance of a Map
    • not.object function (any): boolean? Checks if the constructor not equals to Object (if it is strictly not an object)
    • not.promise function (any): boolean? Checks if it is not a Promise nor an instance of a Promise
    • not.set function (any): boolean? Checks if it is not a Set nor an instance of a Set
    • not.string function (any): boolean? Checks if it is not boolean type
    • not.thenable function (any): boolean? Checks if it has not a then method
    • not.transducer function (any): boolean? Checks if it is not an object that follows the transducer protocol
    • not.type function (string, any): boolean? Checks if the type do not matches matches
    • not.strict function (Function, any): boolean? Checks if the constructor function do not matches
    • not.nil function (any): boolean? Checks if it is not null or undefined

Examples

is.object([]); => false
is(Object, []); //=> true
is(Object, {}); //=> true
is(Number, 1); //=> true
is(Object, 1); //=> false
is(String, 's'); //=> true
is(String, new String('')); //=> true
is(Object, new String('')); //=> true
is(Object, 's'); //=> false
is(Number, {}); //=> false
is.function(() => {}); => true
is.not.function(() => {}); => false

Returns Boolean

map

src/map.js:42-58

  • See: transduce

[Curried function]

Returns a new Iterable, constructed by applying the supplied function to every element of the supplied list.

When mapping an object, the function mapper accepts (value, key, object) In any other case, the mapper function accepts (value, index, collection). The value comes from the iterator of the collection.

Note: map does not skip deleted or unassigned indices (sparse arrays), unlike the native Array.prototype.map method. For more details on this behavior, see: Array.prototype.map

Acts as a transducer if a transformer is given as second parameter.

Parameters

  • fn Function The function to be called on every element of the input Iterable.
  • collection Collection? The Iterable to be iterated over.

Examples

const toUpperCase = (x) => x.toUpperCase();
const double = (x) => x * 2;
const doubleValue = ([k, v]) => [k, v * 2];

map(toUpperCase, 'aeiou'); //=> 'AEIOU'
map(double, [1, 2, 3]); //=> [2, 4, 6]
map(double, Set[1, 2, 3]); //=> Set[2, 4, 6]
map(double, function* () { yield* [1, 2, 3]}); //=> [2, 4, 6]
map(double, { a: 1, b: 2 }); //=> { a: 2, b: 4 }
map(doubleValue, Map{ a: 1, b: 2 }); //=> Map{ a: 2, b: 4 }

Returns (Collection | Function) The new Iterable or curried function.

pipe

src/pipe.js:20-28

  • See: compose

Performs left-to-right function composition. The leftmost function may have any arity; the remaining functions must be unary.

In some libraries this function is named sequence.

Parameters

Examples

var f = pipe(Math.pow, toString);

f(3, 4); // (3^4).toString()

Returns Function a function that represents the composition of the arguments

pipeAsync

src/pipeAsync.js:25-43

  • See: composeAsync

Performs left-to-right function composition. The leftmost function may have any arity; the remaining functions must be unary. If any function returns a promise, it chains remaining ones

Parameters

Examples

var head = ([h]) => h;
var process = (a) => Promise.resolve(a);
var promiseAll = Promise.all.bind(Promise);

var f = pipeAsync(process, promiseAll, head);

f([1, 2]); // Promise.all([process(1), process(2)])[0] => 1

Returns Function a function that represents the composition of the arguments

reduce

src/reduce.js:45-57

  • See: reduced

[Curried function]

Returns a single item by iterating through the collection, successively calling the iterator function and passing it an accumulator value and the current value from the array, and then passing the result to the next call.

The iterator function receives: (acc, value, index, collection). while reducing an object it receives: (acc, [value, key], collection). It may use reduced to stop the iteration.

It also accepts a transducer instead of an iterator function.

Note: reduce does not skip deleted or unassigned indices (sparse arrays), unlike the native Array.prototype.reduce method. For more details on this behavior, see: Array.prototype.reduce

Parameters

  • transducer Function The iterator function. Receives four values: the accumulator, the current element from the Iterable, the iteration index and the whole Iterable. It also accepts a Transducer
  • initial any The accumulator initial value.
  • collection Collection An Iterable parameter.

Examples

const add = (acc, act) => a + b;

reduce(add, 'normal', 'string'); //=> 'normal string'
reduce(add, 10, [1, 2, 3]); //=> 16
reduce(add, 10, Set[1, 2, 3]); //=> 16
reduce(add, 10, function* () { yield* [1, 2, 3]}); //=> 16
reduce(add, '', { a: 1, b: 2 }); //=> 'a,1b,2'
reduce(add, '', Map{ a: 1, b: 2 }); //=> 'a,1b,2'

Returns any The final, accumulated value.

reduced

src/reduced.js:22-25

  • See: reduce, transduce

Returns a value wrapped to indicate that it is the final value of the reduce and transduce functions. The returned value should be considered a black box: the internal structure is not guaranteed to be stable.

Parameters

  • state any The final value of the reduce.

Examples

reduce(
  pipe(add, ifElse(lte(10), reduced, identity)),
  0,
  [1, 2, 3, 4, 5]
) // 10

Returns any The wrapped value.

take

src/take.js:44-61

[Curried function]

Returns the first n elements of the iterable.

Parameters

Examples

const names = names

take(1, names); //=> ['foo']
take(1, function* () { yield* names }); //=> ['foo']
take(1, names); //=> ['foo']
take(2, names); //=> ['foo', 'bar']
take(3, names); //=> ['foo', 'bar', 'baz']
take(4, names); //=> ['foo', 'bar', 'baz']
take(3, 'ramda'); //=> 'ram'
take(1, Map{ a: 1, b: 2 }) => [['a', 1]]

const personnel = [
 'Dave Brubeck',
 'Paul Desmond',
 'Eugene Wright',
 'Joe Morello',
 'Gerry Mulligan',
 'Bob Bates',
 'Joe Dodge',
 'Ron Crotty'
];

const takeTwo = take(2);
takeFive(personnel); //=> ['Dave Brubeck', 'Paul Desmond']

Returns any

tap

src/tap.js:25-35

[Curried function]

Runs the given function with the supplied object, then returns the object.

Acts as a transducer if a transformer is given as second parameter.

Parameters

  • fn Function The function to call with x. The return value of fn will be thrown away.
  • x any

Examples

const sayX = x => console.log('x is ' + x);
tap(sayX, 100); //=> 100
// logs 'x is 100'

Returns any x.

transduce

src/transduce.js:52-69

  • See: reduce, reduced, into

[Curried function]

Initializes a transducer using supplied iterator function. Returns a single item by iterating through the list, successively calling the transformed iterator function and passing it an accumulator value and the current value from the array, and then passing the result to the next call.

The iterator function receives two values: (acc, value). It will be wrapped as a transformer to initialize the transducer. A transformer can be passed directly in place of an iterator function. In both cases, iteration may be stopped early with the reduced function.

A transducer is a function that accepts a transformer and returns a transformer and can be composed directly.

A transformer is an object that provides a 2-arity reducing iterator function, step, 0-arity initial value function, init, and 1-arity result extraction function, result. The step function is used as the iterator function in reduce. The result function is used to convert the final accumulator into the return type and in most cases is the identity function. The init function can be used to provide an initial accumulator, but is ignored by transduce.

The iteration is performed with reduce after initializing the transducer.

Parameters

  • transducer Function The transducer function. Receives a transformer and returns a transformer.
  • combine Function The iterator function. Receives two values, the accumulator and the current element from the array. Wrapped as transformer, if necessary, and used to initialize the transducer
  • initial any The initial accumulator value.
  • collection Collection The Iterable to iterate over.

Examples

var t = transducers;
var inc = function(n) { return n+1; };
var isEven = function(n) { return n % 2 == 0; };
var apush = function(arr,x) { arr.push(x); return arr; };
var xf = compose(map(inc),filter(isEven));
transduce(xf, apush, [], [1,2,3,4]); // [2,4]

Returns any The final, accumulated value.

bind

src/bind.js:20-26

Creates a function that is bound to a context. Note: bind does not provide the additional argument-binding capabilities of Function.prototype.bind.

Parameters

  • fn Function The function to bind to context
  • thisObj Object The context to bind fn to

Examples

const log = bind(console.log, console);
pipe(inc, log, double)(2); //=> 6
// logs: 3

Returns Function A function that will execute in the context of thisObj.

complement

src/complement.js:24-26

Takes a function f and returns a function g such that:

  • applying g to zero or more arguments will give true if applying the same arguments to f gives a logical false value; and

  • applying g to zero or more arguments will give false if applying the same arguments to f gives a logical true value.

Parameters

Examples

const isEven = (n) => n % 2 === 0;
const isOdd = complement(isEven);

isOdd(21); //=> true
isOdd(42); //=> false

Returns Function

compose

src/compose.js:18-26

  • See: pipe

Performs right-to-left function composition. The rightmost function may have any arity; the remaining functions must be unary.

Parameters

Examples

const inc = x => x + 1:
const negate = x => -x;

var f = compose(inc, negate, Math.pow);
f(3, 4); // -(3^4) + 1

Returns Function a function that represents the composition of the arguments

composeAsync

src/composeAsync.js:25-43

  • See: pipeAsync

Performs right-to-left function composition. The rightmost function may have any arity; the remaining functions must be unary. If any function returns a promise, it chains remaining ones

Parameters

Examples

var head = ([h]) => h;
var process = (a) => Promise.resolve(a);
var promiseAll = Promise.all.bind(Promise);

var f = composeAsync(head, promiseAll, process);

await f([1, 2]); // Promise.all([process(1), process(2)])[0] => 1

Returns Function a function that represents the composition of the arguments

curry

src/curry.js:27-35

  • See: curryN

Returns a curried equivalent of the provided function. The curried function has two unusual capabilities. First, its arguments needn't be provided one at a time. If f is a ternary function and g is curry(f), the following are equivalent:

  • g(1)(2)(3)
  • g(1)(2, 3)
  • g(1, 2)(3)
  • g(1, 2, 3)

Parameters

  • fn Function The function to curry.
  • initialArgs ...any The fn initial arguments.

Examples

var addFourNumbers = function(a, b, c, d) {
  return a + b + c + d;
};
var curriedAddFourNumbers = curry(addFourNumbers);
var f = curriedAddFourNumbers(1, 2);
var g = f(3);
g(4); //=> 10

Returns Function A new, curried function.

curryN

src/curryN.js:31-39

  • See: curry

Returns a curried equivalent of the provided function, with the specified arity. The curried function has two unusual capabilities. First, its arguments needn't be provided one at a time. If g is curryN(3, f), the following are equivalent:

  • g(1)(2)(3)
  • g(1)(2, 3)
  • g(1, 2)(3)
  • g(1, 2, 3)

Parameters

  • arity Number The arity for the returned function.
  • fn Function The function to curry.
  • initialArgs ...any The function initial arguments.

Examples

var addFourNumbers = function(a, b, c, d = 0) {
  return a + b + c + d;
};

var curriedAddFourNumbers = curryN(3, addFourNumbers);
var f = curriedAddFourNumbers(1, 2);
f(3); //=> 6

Returns Function A new, curried function.

filter

src/filter.js:36-52

  • See: transduce

Returns a new list containing only those items that match a given predicate function. The predicate function is passed: (value, index, collection). in the case of filtering and object, the predicate function is passed: (value, key, collection).

Note that filter does not skip deleted or unassigned indices, unlike the native Array.prototype.filter method. For more details on this behavior, see: Array.prototype.filter

Acts as a transducer if a transformer is given as second parameter.

Parameters

  • fn Function The function called per iteration.
  • collection Collection? The collection to iterate over.

Examples

const isEven = (n) => n % 2 === 0;
const isValueEven = ([k, v]) => isEven(v)
const isVowel = (c) => /[aeiou]/.test(c);

filter(isVowel, 'string'); //=> 'i'
filter(isEven, [1, 2, 3]); //=> [2]
filter(isEven, Set[1, 2, 3]); //=> Set[2]
filter(isEven, function* () { yield* [1, 2, 3]}); //=> [2]
filter(isEven, { a: 1, b: 2 }); //=> { b: 2 }
filter(isValueEven, Map{ a: 1, b: 2 }); //=> Map{ b: 2 }

Returns (Collection | Function) The new filtered array.

Transducer

src/internal/typedef.js:1-16

see The Transducer Protocol

Type: Object

Properties

  • null Function @@transducer/init - Function: () => any, Hook called at the beginning to compute the initial state
  • null Function @@transducer/result - Function: (state) => any, Hook called when the result is computed
  • null Function @@transducer/step - Function: (state, value, index, collection) => any, Hook called at every iteration

Collection

src/internal/typedef.js:1-16

Type: (Array | Object | Transducer | Set | Map | string | Iterator | GeneratorFunction | Generator | Iterable)

Into

src/into.js:52-89

Type: Object

Properties

  • array function (Function, Collection): Array? Function: (value, index, collection, accumulator) => any, Merge strategy: Array.push
  • string function (Function, Collection): string? Function: (value, index, collection, accumulator) => any, Merge strategy: concatenation
  • number function (Function, Collection): number? Function: (value, index, collection, accumulator) => any, Merge strategy: last returned value becomes the accumulator
  • set function (Function, Collection): Set? Function: (value, index, collection, accumulator) => any, Merge strategy: Set.add
  • map function (Function, Collection): Map? Function: ([key, value], index, collection, accumulator) => [any, any], Merge strategy: Map.add
  • object function (Function, Collection): Object? Function: ([key, value], index, collection, accumulator) => [any, any], Merge strategy: object assignation

into

src/into.js:52-89

Transforms the items of the iterable with the transducer and appends the transformed items to the accumulator using an appropriate iterator function based on the accumulator type.

The transducer function takes 4 params: (value, index, collection, accumulator). Then value parameter is a pair [key, value] when the accumulator is an Object or a Map.

In the case the accumulator is a number (into.number), there is no merge strategy and just returns what the transducer returns

The iteration is performed with reduce after initializing the transducer.

Type: Into

Examples

var value = ([k, v]] => v
var isEven = (x) => x % 2 === 0
var isVowel = (x) => /aeiou/.test(x)
var isObject = (x) => typeof x === 'object'
var toUppercase = (x) => x.toUppercase()
var addAccumulator = (value, index, collection, accumulator) => value + accumulator
var pairById = (x) => [x.id, x]

into.array(compose(map(value), filter(isEven)), {a: 1, b: 2}) // [2]

into.string(compose(filter(isVowel), map(toUppercase)), 'string') // 'I'

into.number(map(addAccumulator), [1, 2, 3]) // 1 + 2 + 3 = 6

into.object(
  compose(filter(isObject), map(pairById)),
    [1, { id: '1-2-3', value: 10 }]
  ) // { '1-2-3': { id: '1-2-3', value: 10 } }

is

src/is.js:139-174

See if an object (val) is an instance of the supplied constructor. This function will check up the inheritance chain, if any.

  • Additionally, it has the following methods for nicer and more semantic type checking: array - function - functor - filterable - generator - iterator - map - object (strict checking) - promise - set - string - thenable - transducer - type - strict - nil - not (negates any of the previous ones)
  • @func

Parameters

  • ctor Object A constructor
  • val any The value to test

Properties

  • array function (any): boolean? Checks if is an Array

  • function function (any): boolean? Checks if is type function

  • functor function (any): boolean? Checks if it has a map method

  • filterable function (any): boolean? Checks if it has a filter method

  • generator function (any): boolean? Checks if it is a generator function

  • iterator function (any): boolean? Checks if it has [Symbol.iterator] method

  • map function (any): boolean? Checks if it is a Map or an instance of a Map

  • object function (any): boolean? Checks if the constructor equals to Object (if it is strictly an object)

  • promise function (any): boolean? Checks if it is a Promise or an instance of a Promise

  • set function (any): boolean? Checks if it is a Set or an instance of a Set

  • string function (any): boolean? Checks if it is boolean type

  • thenable function (any): boolean? Checks if it has a then method

  • transducer function (any): boolean? Checks if it is an object that follows the transducer protocol

  • type function (string, any): boolean? Checks if the type matches

  • strict function (Function, any): boolean? Checks if the constructor function matches

  • nil function (any): boolean? Checks if it is null or undefined

  • not Object? negates the following method

    • not.array function (any): boolean? Checks if is not an Array
    • not.function function (any): boolean? Checks if is not type function
    • not.functor function (any): boolean? Checks if it has not a map method
    • not.filterable function (any): boolean? Checks if it has not a filter method
    • not.generator function (any): boolean? Checks if it is not a generator function
    • not.iterator function (any): boolean? Checks if it has not [Symbol.iterator] method
    • not.map function (any): boolean? Checks if it is not a Map nor an instance of a Map
    • not.object function (any): boolean? Checks if the constructor not equals to Object (if it is strictly not an object)
    • not.promise function (any): boolean? Checks if it is not a Promise nor an instance of a Promise
    • not.set function (any): boolean? Checks if it is not a Set nor an instance of a Set
    • not.string function (any): boolean? Checks if it is not boolean type
    • not.thenable function (any): boolean? Checks if it has not a then method
    • not.transducer function (any): boolean? Checks if it is not an object that follows the transducer protocol
    • not.type function (string, any): boolean? Checks if the type do not matches matches
    • not.strict function (Function, any): boolean? Checks if the constructor function do not matches
    • not.nil function (any): boolean? Checks if it is not null or undefined

Examples

is.object([]); => false
is(Object, []); //=> true
is(Object, {}); //=> true
is(Number, 1); //=> true
is(Object, 1); //=> false
is(String, 's'); //=> true
is(String, new String('')); //=> true
is(Object, new String('')); //=> true
is(Object, 's'); //=> false
is(Number, {}); //=> false
is.function(() => {}); => true
is.not.function(() => {}); => false

Returns Boolean

map

src/map.js:39-55

  • See: transduce

Returns a new Iterable, constructed by applying the supplied function to every element of the supplied list.

When mapping an object, the function mapper accepts (value, key, object) In any other case, the mapper function accepts (value, index, collection). The value comes from the iterator of the collection.

Note: map does not skip deleted or unassigned indices (sparse arrays), unlike the native Array.prototype.map method. For more details on this behavior, see: Array.prototype.map

Acts as a transducer if a transformer is given as second parameter.

Parameters

  • fn Function The function to be called on every element of the input Iterable.
  • collection Collection? The Iterable to be iterated over.

Examples

const toUpperCase = (x) => x.toUpperCase();
const double = (x) => x * 2;
const doubleValue = ([k, v]) => [k, v * 2];

map(toUpperCase, 'aeiou'); //=> 'AEIOU'
map(double, [1, 2, 3]); //=> [2, 4, 6]
map(double, Set[1, 2, 3]); //=> Set[2, 4, 6]
map(double, function* () { yield* [1, 2, 3]}); //=> [2, 4, 6]
map(double, { a: 1, b: 2 }); //=> { a: 2, b: 4 }
map(doubleValue, Map{ a: 1, b: 2 }); //=> Map{ a: 2, b: 4 }

Returns (Collection | Function) The new Iterable or curried function.

pipe

src/pipe.js:20-28

  • See: compose

Performs left-to-right function composition. The leftmost function may have any arity; the remaining functions must be unary.

In some libraries this function is named sequence.

Parameters

Examples

var f = pipe(Math.pow, toString);

f(3, 4); // (3^4).toString()

Returns Function a function that represents the composition of the arguments

pipeAsync

src/pipeAsync.js:25-43

  • See: composeAsync

Performs left-to-right function composition. The leftmost function may have any arity; the remaining functions must be unary. If any function returns a promise, it chains remaining ones

Parameters

Examples

var head = ([h]) => h;
var process = (a) => Promise.resolve(a);
var promiseAll = Promise.all.bind(Promise);

var f = pipeAsync(process, promiseAll, head);

f([1, 2]); // Promise.all([process(1), process(2)])[0] => 1

Returns Function a function that represents the composition of the arguments

reduce

src/reduce.js:42-54

  • See: reduced

Returns a single item by iterating through the collection, successively calling the iterator function and passing it an accumulator value and the current value from the array, and then passing the result to the next call.

The iterator function receives: (acc, value, index, collection). while reducing an object it receives: (acc, [value, key], collection). It may use reduced to stop the iteration.

It also accepts a transducer instead of an iterator function.

Note: reduce does not skip deleted or unassigned indices (sparse arrays), unlike the native Array.prototype.reduce method. For more details on this behavior, see: Array.prototype.reduce

Parameters

  • transducer Function The iterator function. Receives four values: the accumulator, the current element from the Iterable, the iteration index and the whole Iterable. It also accepts a Transducer
  • initial any The accumulator initial value.
  • collection Collection An Iterable parameter.

Examples

const add = (acc, act) => a + b;

reduce(add, 'normal', 'string'); //=> 'normal string'
reduce(add, 10, [1, 2, 3]); //=> 16
reduce(add, 10, Set[1, 2, 3]); //=> 16
reduce(add, 10, function* () { yield* [1, 2, 3]}); //=> 16
reduce(add, '', { a: 1, b: 2 }); //=> 'a,1b,2'
reduce(add, '', Map{ a: 1, b: 2 }); //=> 'a,1b,2'

Returns any The final, accumulated value.

reduced

src/reduced.js:22-25

  • See: reduce, transduce

Returns a value wrapped to indicate that it is the final value of the reduce and transduce functions. The returned value should be considered a black box: the internal structure is not guaranteed to be stable.

Parameters

  • state any The final value of the reduce.

Examples

reduce(
  pipe(add, ifElse(lte(10), reduced, identity)),
  0,
  [1, 2, 3, 4, 5]
) // 10

Returns any The wrapped value.

take

src/take.js:41-58

Returns the first n elements of the iterable.

Parameters

Examples

const names = names

take(1, names); //=> ['foo']
take(1, function* () { yield* names }); //=> ['foo']
take(1, names); //=> ['foo']
take(2, names); //=> ['foo', 'bar']
take(3, names); //=> ['foo', 'bar', 'baz']
take(4, names); //=> ['foo', 'bar', 'baz']
take(3, 'ramda'); //=> 'ram'
take(1, Map{ a: 1, b: 2 }) => [['a', 1]]

const personnel = [
 'Dave Brubeck',
 'Paul Desmond',
 'Eugene Wright',
 'Joe Morello',
 'Gerry Mulligan',
 'Bob Bates',
 'Joe Dodge',
 'Ron Crotty'
];

const takeTwo = take(2);
takeFive(personnel); //=> ['Dave Brubeck', 'Paul Desmond']

Returns any

tap

src/tap.js:22-32

Runs the given function with the supplied object, then returns the object.

Acts as a transducer if a transformer is given as second parameter.

Parameters

  • fn Function The function to call with x. The return value of fn will be thrown away.
  • x any

Examples

const sayX = x => console.log('x is ' + x);
R.tap(sayX, 100); //=> 100
// logs 'x is 100'

Returns any x.

transduce

src/transduce.js:49-66

  • See: reduce, reduced, into

Initializes a transducer using supplied iterator function. Returns a single item by iterating through the list, successively calling the transformed iterator function and passing it an accumulator value and the current value from the array, and then passing the result to the next call.

The iterator function receives two values: (acc, value). It will be wrapped as a transformer to initialize the transducer. A transformer can be passed directly in place of an iterator function. In both cases, iteration may be stopped early with the reduced function.

A transducer is a function that accepts a transformer and returns a transformer and can be composed directly.

A transformer is an object that provides a 2-arity reducing iterator function, step, 0-arity initial value function, init, and 1-arity result extraction function, result. The step function is used as the iterator function in reduce. The result function is used to convert the final accumulator into the return type and in most cases is R.identity. The init function can be used to provide an initial accumulator, but is ignored by transduce.

The iteration is performed with reduce after initializing the transducer.

Parameters

  • transducer Function The transducer function. Receives a transformer and returns a transformer.
  • combine Function The iterator function. Receives two values, the accumulator and the current element from the array. Wrapped as transformer, if necessary, and used to initialize the transducer
  • initial any The initial accumulator value.
  • collection Collection The Iterable to iterate over.

Examples

var t = transducers;
var inc = function(n) { return n+1; };
var isEven = function(n) { return n % 2 == 0; };
var apush = function(arr,x) { arr.push(x); return arr; };
var xf = compose(map(inc),filter(isEven));
transduce(xf, apush, [], [1,2,3,4]); // [2,4]

Returns any The final, accumulated value.