Transmute
kind of like lodash but works with Immutable
Install / Use
/learn @HubSpot/TransmuteREADME
@hs/transmute
@hs/transmute provides convenient, composable functions for transforming Arrays, Immutable.js data structures, and Objects.
Getting started
Transmute can be installed with npm or yarn.
npm install @hs/transmute
import { Map } from 'immutable';
import pick from 'transmute/pick';
// returns Map { one => 1, three => 3 }
pick(['one', 'three'], Map({one: 1, two: 2, three: 3}));
Most of the function (with the execption of some of the composition functions like compose and pipe) are curried to facilitate partial application. You might also notice that the argument order is the oposite of you'll find in other utility libraries. Passing the options and then the subject makes currying much more useful.
import { Map } from 'immutable';
import pick from 'transmute/pick';
const pickTwo = pick(['two']);
// returns Map { two => 2 }
pickTwo(Map({one: 1, two: 2, three: 3}));
transmute also includes some helpful composition functions which are powerful when we combine them with curried transforms.
import { Map, Set } from 'immutable';
import * as t from 'transmute';
const setOfKeysWithEvenValues = t.pipe(
t.filter((val) => val % 2 === 0),
t.keySeq,
Set
);
// returns Set { 'two', 'four' }
takeEvenValues(Map({one: 1, two: 2, three: 3, four: 4}));
API
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->always
Creates a function that always returns returnValue.
Parameters
returnValueT
Examples
const alwaysBlue = always('blue');
alwaysBlue() === 'blue';
Returns T
bind
Sets a function's this context. Similar to Function.prototype.bind.
Parameters
Examples
bind(console.log, console);
Returns Function
both
Returns true if the results of arg applied to both condition1 and
condition2 are truthy.
Parameters
Examples
const isOneToTen = both(
n => n >= 1,
n => n <= 10
);
isOneToTen(3) === true;
isOneToTen(11) === false;
Returns boolean
clear
Returns an empty copy of subject.
Parameters
Examples
clear([1, 2, 3]) // returns []
clear(List.of(1, 2, 3)) // returns List []
clear({one: 1, two: 2, three: 3}) // returns {}
Returns (Array | Collection | Object)
compose
Create a function that runs operations from right-to-left.
compose is not curried.
Parameters
Examples
const doubleAndTakeEvens = pipe(
filter(n => n % 2 === 0),
map(n => n * 2)
);
doubleAndTakeEvens(List.of(1, 2, 3))
// returns List [ 2, 4, 6 ]
Returns Function
concat
Joins two Iterable.Indexed objects together.
Examples
// Arrays
concat(List([3]), List([1, 2])); // Returns List [ 1, 2, 3 ]
const addY = concat(List(['y']);
addY(List(['x'])); // Returns List [ 'x', 'y' ]
Returns Iterable with the concatenated value. Does not support keyed Iterable subjects.
count
Returns the number of values in subject.
Parameters
subjectTYPE
Examples
count(List.of(1, 2, 3)) === 3;
Returns number
curry
Creates a curried version of operation.
Parameters
operationFunction
Examples
const toArray = curry((a, b, c) => [a, b, c]);
const toArrayWith1 = toArray(1);
toArrayWith1(2, 3) === [1, 2, 3];
Returns Function
curryN
Create a curried version of operation that expects arity arguments.
Inception-ally, curryN is also curried.
Parameters
Examples
const toArray = curryN(3)((...args) => [...args]);
toArray(1, 2, 3) === [1, 2, 3];
Returns Function
debounce
operation is called interval milliseconds after the most recent call.
Parameters
Examples
const sayHello = (first, last) => console.log(`Hello ${first} ${last}!`);
// Second param is just the function name even if the function takes arguments
const debouncedSayHello = debounce(300, sayHello);
debouncedSayHello('hs', 'transmute'); // logs "Hello hs transmute!" after 300 milliseconds
Returns any the most recent result of operation
debounceImmediate
src/debounceImmediate.js:52-52
operation is called immediately and then interval milliseconds after the most
recent call.
Parameters
Returns any the most recent result of operation
difference
Take the difference between one iterable and another iterable. Only the elements present in just subject will remain.
Parameters
toRemoveIterablesubjectIterable
Examples
const removeOne = difference(Set.of(1));
removeOne(Set.of(1, 2, 3)) // returns Set { 2, 3 }
Returns Iterable
either
Returns true if the results of arg applied to either first or second
are truthy.
Parameters
firstFunctionsecond**[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/R
