HubSpot/transmute

Name: transmute

Owner: HubSpot

Description: kind of like lodash but works with Immutable

Created: 2017-05-26 20:08:11.0

Updated: 2018-03-23 22:42:30.0

Pushed: 2018-03-26 16:50:58.0

Homepage: http://github.hubspot.com/transmute/

Size: 1549

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

@hs/transmute

transmute on npm Travis branch

@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
rt { Map } from 'immutable';
rt pick from 'transmute/pick';

eturns Map { one => 1, three => 3 }
(['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.

rt { Map } from 'immutable';
rt pick from 'transmute/pick';

t pickTwo = pick(['two']);
eturns Map { two => 2 }
Two(Map({one: 1, two: 2, three: 3}));

transmute also includes some helpful composition functions which are powerful when we combine them with curried transforms.

rt { Map, Set } from 'immutable';
rt * as t from 'transmute';

t setOfKeysWithEvenValues = t.pipe(
filter((val) => val % 2 === 0),
keySeq,
t


eturns Set { 'two', 'four' }
EvenValues(Map({one: 1, two: 2, three: 3, four: 4}));
API
always

src/always.js:13-15

Creates a function that always returns returnValue.

Parameters

Examples

t alwaysBlue = always('blue');
ysBlue() === 'blue';

Returns T

bind

src/bind.js:18-18

Sets a function's this context. Similar to Function.prototype.bind.

Parameters

Examples

(console.log, console);

Returns Function

both

src/both.js:29-29

Returns true if the results of arg applied to both condition1 and condition2 are truthy.

Parameters

Examples

t isOneToTen = both(
=> n >= 1,
=> n <= 10


eToTen(3) === true;
eToTen(11) === false;

Returns boolean

clear

src/clear.js:14-14

Returns an empty copy of subject.

Parameters

Examples

r([1, 2, 3]) // returns []
r(List.of(1, 2, 3)) // returns List []
r({one: 1, two: 2, three: 3}) // returns {}

Returns (Array | Collection | Object)

compose

src/compose.js:28-31

Create a function that runs operations from right-to-left.

compose is not curried.

Parameters

Examples

t doubleAndTakeEvens = pipe(
lter(n => n % 2 === 0),
p(n => n * 2)


leAndTakeEvens(List.of(1, 2, 3))
eturns List [ 2, 4, 6 ]

Returns Function

count

src/count.js:12-12

Returns the number of values in subject.

Parameters

Examples

t(List.of(1, 2, 3)) === 3;

Returns number

curry

src/curry.js:14-16

Creates a curried version of operation.

Parameters

Examples

t toArray = curry((a, b, c) => [a, b, c]);
t toArrayWith1 = toArray(1);
rayWith1(2, 3) === [1, 2, 3];

Returns Function

curryN

src/curryN.js:41-41

Create a curried version of operation that expects arity arguments. Inception-ally, curryN is also curried.

Parameters

Examples

t toArray = curryN(3)((...args) => [...args]);
ray(1, 2, 3) === [1, 2, 3];

Returns Function

debounce

src/debounce.js:42-42

operation is called interval milliseconds after the most recent call.

Parameters

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

src/difference.js:24-24

Take the difference between one iterable and another iterable. Only the elements present in just subject will remain.

Parameters

Examples

t removeOne = difference(Set.of(1));

veOne(Set.of(1, 2, 3)) // returns Set { 2, 3 }

Returns Iterable

either

src/either.js:26-26

Returns true if the results of arg applied to either first or second are truthy.

Parameters

Examples

t oneOrTwo = either(
=> n === 1,
=> n === 2


rTwo(1) === true;
rTwo(2) === true;
rTwo(3) === false;

Returns boolean

entrySeq

src/entrySeq.js:13-13

Get a Seq of the entries (i.e. [key, value] tuples) in subject.

Parameters

Examples

ySeq(Map({one: 1, two: 2}))
eturns Seq [ ['one', 1], ['two', 2] ]

Returns Seq

every

src/every.js:17-17

Returns true if all items in subject match predicate.

Parameters

Examples

t alwaysBlue = every(v => v === 'blue');

ysBlue(List.of('blue', 'blue')) === true;
ysBlue(List.of('red', 'blue')) === false;

Returns bool

filter

src/filter.js:25-25

Remove values for which predicate returns false.

Parameters

Examples

eturns List [ 2 ]
er(
) => n % 2 === 0,
st.of(1, 2, 3)

Records have a fixed set of keys, so filter returns a Map instead.

eturns Map { 'one' => 1, 'three' => 3 }
er(
) => n % 2 === 0,
reeRecord({one: 1, two: 2, three: 3})

Returns Iterable without values that didn't match predicate.

filterNot

src/filterNot.js:22-22

Remove values for which predicate returns true.

Parameters

Examples

eturns List [ 1, 3 ]
erNot(
) => n % 2 === 0,
st.of(1, 2, 3)

Returns Iterable without values that matched predicate.

flatten

src/flatten.js:13-15

Flattens an iterable as deeply as possible.

Parameters

Examples

eturn List [ 1, 2, 3, 4, 5, 6 ]
ten(List.of(List.of(1, List.of(2, 3)), List.of(4, 5, 6)));

Returns Iterable

flattenN

src/flattenN.js:16-16

Flattens an iterable depth levels.

Parameters

Examples

eturn List [ 1, List [ 2, 3 ], 4, 5, 6 ]
tenN(1, List.of(List.of(1, List.of(2, 3)), List.of(4, 5, 6)));

Returns Iterable

forEach

src/forEach.js:22-22

Executes effect for each value in subject, then returns subject.

Parameters

Examples

ach(
=> console.log(v),
p({ one: 1, two: 2, three: 3 })


rints...



Returns TYPE

fromJS

src/fromJS.js:15-17

A version of Immutable.fromJS that drops all but the first argument for compatibility with other transmute functions like map.

Parameters

Examples

JS({items: [1, 2, 3]})
eturns Map { items: List [ 1, 2, 3 ] }

Returns Iterable?

get

src/get.js:15-15

Retrieve the value at key from subject.

Parameters

Examples

eturns 1
'one', Map({one: 1, two: 2, three: 3}))

Returns any the value at key.

getIn

src/getIn.js:23-23

Retrieve a keyPath from a nested Immutable or JS structure.

getIn short circuts when it encounters a null or undefined value.

Parameters

Examples

t getFirstName = getIn(['name', 'first']);
t user = UserRecord({
me: Map({
first: 'Test',
last: 'Testerson',
,

irstName(user) === 'Test'

Returns any

has

src/has.js:17-17

Returns true if key exists in subject.

Parameters

Examples

t hasOne = has('one');

ne({one: 1}) === true;
ne(Map({two: 2})) === false;

Returns boolean

hasIn

src/hasIn.js:41-41

Returns true if keyPath is defined in a nested data structure.

hasIn short circuts and returns false when it encounters a null or undefined value.

Parameters

Examples

t hasFirstName = hasIn(['name', 'first']);
t user = UserRecord({
me: Map({
first: 'Test',
last: 'Testerson',
,

irstName(user) === true

Returns boolean

identity

src/identity.js:12-14

Returns it's first argument.

Parameters

Examples

tity('something') === 'something'

Returns any

ifElse

src/ifElse.js:31-31

Applies affirmative to subject if predicate(subject) is truthy. Otherwise applies negative to subject.

Parameters

Examples

t incrementAwayFromZero = ifElse(
=> n >= 0,
=> n + 1,
=> n - 1


ementAwayFromZero(1) === 2
ementAwayFromZero(-1) === -2

Returns any

ifThen

src/ifThen.js:32-32

Applies affirmative to subject if predicate(subject) is truthy. Otherwise returns subject.

Parameters

Examples

rt ifThen from 'transmute/ifThen';

t toJS = ifThen(
bject => typeof subject.toJS === 'function',
bject => subject.toJS


(List.of(1, 2, 3)) //=> [1, 2, 3]
([1, 2, 3]) //=> [1, 2, 3]

Returns any

indexBy

src/indexBy.js:27-27

Create a Map, or OrderedMap from subject with a key for each item returned by keyMapper.

Parameters

Examples

xBy(get('id'), List.of({id: 123}, {id: 456}))
eturns Map { 123: {id: 123}, 456: {id: 456} }

Returns KeyedIterable

keySeq

src/keySeq.js:13-13

Get a Seq of the keys in subject.

Parameters

Examples

eq({one: 1, two: 2, three: 3})
eturns Seq [ 'one', 'two', 'three' ]

Returns Seq

map

src/map.js:18-18

Create a new Iterable by applying mapper to each item in subject.

Parameters

Examples

eturns List [ 2, 3, 4 ]

al) => val + 1,
st.of(1, 2, 3)

Returns Iterable with each value of subject updated with mapper.

reduce

src/reduce.js:21-21

Transform the contents of subject to into by applying operation to each item.

Parameters

Examples

ce(
st(),
cc, val) => acc.push(val),
p({ one: 1, two: 2, three: 3 })

eturns List [ 1, 2, 3 ]

Returns Iterable

set

src/set.js:16-16

Returns a copy of subject with key set to value.

Parameters

Examples

'one', 2, {one: 1});
eturns {one: 2}

Returns (Array | Iterable | Object)

some

src/some.js:17-17

Returns true if any items in subject match predicate.

Parameters

Examples

t anyBlue = some(v => v === 'blue');

lue(List.of('blue', 'red')) === true;
lue(List.of('red', 'red')) === true;

Returns bool

sortBy

src/sortBy.js:25-25

Sort subject according to the value returned by getSortValue.

Parameters

Examples

eturns List [ 2, 1, 3 ]
By(
) => n % 2,
st.of(1, 2, 3)

avascript
eturns OrderedMap { "one" => 1, "two" => 2, "three" => 3 }
By(
) => n % 2,
p({three: 3, one: 1, two: 2})

Returns Iterable an ordered version of subject (e.g. sorting a Map returns an OrderedMap).

valueSeq

src/valueSeq.js:13-13

Get a Seq of the values in subject.

Parameters

Examples

eSeq(Map({ one: 1, two: 2, three: 3 }))
eturns Seq [ 1, 2, 3 ]

Returns Seq

isArray

src/isArray.js:9-11

Returns true if value is an Array.

Parameters

Returns boolean

isEmpty

src/isEmpty.js:11-16

Returns true if value is “empty”. If given null, undefined, isEmpty will return true.

Parameters

Returns boolean

isFunction

src/isFunction.js:9-11

Returns true if value is a Function.

Parameters

Returns boolean

isInstanceOf

src/isInstanceOf.js:14-14

Returns true if value is an instance of Constructor.

Parameters

Returns boolean

isNull

src/isNull.js:9-11

Returns true if subject is null.

Parameters

Returns boolean

isNumber

src/isNumber.js:9-11

Returns true if subject is a JavaScript Number and not NaN.

Parameters

Returns boolean

isObject

src/isObject.js:9-11

Returns true if value is an Object.

Parameters

Returns boolean

isRecord

src/isRecord.js:10-14

Returns true if subject is an instance of a Record.

Parameters

Returns boolean

isString

src/isString.js:9-11

Returns true if value is a String.

Parameters

Returns boolean

isUndefined

src/isUndefined.js:9-11

Returns true if subject is undefined.

Parameters

Returns boolean

mapKeys

src/mapKeys.js:37-37

Like map but transforms an Iterable's keys rather than its values.

Parameters

Examples

Can be useful for converting keys of API results to a common type.

rt { mapKeys, toString } from 'transmute';
t stringifyKeys = mapKeys(toString);
t m = Map.of(123, Map(), 456, Map(), 789, Map());
ngifyKeys(m).equals(Map.of('123', Map(), '456', Map(), '789', Map()));

Returns KeyedIterable

match

src/match.js:25-25

Returns true if the key => value pairs in pattern match the correspoding key => value pairs in subject.

Parameters

Examples

t hasOneAndThree = match({one: 1, three: 3});
neAndThree(Map({one: 1, two: 2, three: 3})) === true;

Returns boolean

memoize

src/memoize.js:54-61

Memoizer that uses a Map to allow for arbitrarily many/complex keys.

Parameters

Examples

t sum = memoize((list) => {
turn list.reduce((total, n) => total + n, 0);

oes work and returns 15
List.of(1, 2, 3, 4, 5))
eturns 15 but does no work
List.of(1, 2, 3, 4, 5))

We can use the hashFunction param to customize the key used in the cache.

t sum = memoize(
ist) => list.reduce((total, n) => total + n, 0),
ist) => return list.join('-')

It's also possible to inspect the state of an instance by reading the .cache property.

t sum = memoize(...);
isMap(sum.cache) === true;

Returns Function memoized version of operation.

memoizeLast

src/memoizeLast.js:21-44

Like memoize, but only caches the most recent value. It's often useful for caching expensive calculations in react components.

Parameters

Examples

t sum = memoizeLast((...nums) => nums.reduce((acc, n) => acc + n));
List.of(1, 2, 3))
does work, returns 6
List.of(1, 2, 3))
takes cached value, returns 6
List.of(4, 5, 6))
does work, returns 15
List.of(1, 2, 3))
does work again, returns 6

Returns Function

merge

src/merge.js:23-23

Takes each entry of updates and sets it on subject.

Parameters

Examples

eturns Map { "one" => 3, "two" => 2, "three" => 1}
e(
p({one: 1, two: 2, three: 3}),
p({one: 3, three: 1})

Returns Iterable with each key-value of updates merged into subject.

omit

src/omit.js:24-24

Drop specified keys from a KeyedIterable (e.g. a Map or OrderedMap).

Parameters

Examples

eturns Map { "two" => 2 }
(
one', 'three'],
p({one: 1, two: 2, three: 3})

Returns KeyedIterable without keys.

once

src/once.js:7-17

fn is only run one time.

Parameters

Returns any the result of the first time fn was called

partial

src/partial.js:17-20

Like fn.bind(), but without the option to pass context.

partial is not curried.

const add = (a, b, c) => a + b + c; const add11 = partial(add, 5, 6); add11(7); // returns 18

Parameters

Returns Function

partialApply

src/partialApply.js:34-34

Like transmute/partial, but takes an Array or Iterable of arguments to pass to operation rather than a dynamic number of args. Unlike partial it is curried.

partial : partialApply :: Function.prototype.call : Function.prototype.apply

Parameters

Examples

t add = (a, b, c) => a + b + c;
t add11 = partialApply(add, [5, 6]);
1(7); // returns 18

Returns Function

pick

src/pick.js:24-24

Select specified keys from a KeyedIterable (e.g. a Map or OrderedMap).

Parameters

Examples

eturns Map { "one" => 1, "three" => 3 }
(
one', 'three'],
p({one: 1, two: 2, three: 3})

Returns KeyedIterable with just keys.

pipe

src/pipe.js:28-31

Create a function that runs operations from left-to-right.

pipe is not curried.

Parameters

Examples

t takeEvensAndDouble = pipe(
lter(n => n % 2 === 0),
p(n => n * 2)


EvensAndDouble(List.of(1, 2, 3))
eturns List [ 4 ]

Returns Function

pluck

src/pluck.js:20-20

Select key from each item in subject.

Parameters

Examples

t pluckName = pluck('name');
kName(userMap) === Map({123: 'Testing'});

Returns Iterable

setArity

src/setArity.js:18-18

Creates a function identical to operation but with length arity.

Parameters

Examples

t op = (...args) => args;
ength === 0;

t twoArgOp = setArity(2, op);
rgOp.length === 2;

Returns Function

setIn

src/setIn.js:23-23

Set the value at keyPath in a nested structure.

Parameters

Examples

n(['one', 'two'], 3, {one: {two: 2}});
eturns {one: {two: 3}}

Unset keyPaths will be set based on the most recent type.

n(['one', 'two'], 3, {});
eturns {one: {two: 3}}

n(['one', 'two'], 3, Map());
eturns Map { one => Map { two => 3 } }
throttle

src/throttle.js:47-47

Ensures operation is only called once every interval milliseconds.

Parameters

Returns any the most recent result of operation

toJS

src/toJS.js:7-18

Converts an Iterable to a native JS structure.

Parameters

Returns (Array \| Object) native JS requivalent of subject.

toSeq

src/toSeq.js:11-15

Converts subject to a Seq if possible.

Parameters

Returns Seq

toString

src/toString.js:6-8

Returns the value converted to a string.

Parameters

uniqueId

src/uniqueId.js:12-14

Returns a unique integer string appended to prefix.

Parameters

Examples

ueId('test-') === 'test-1';
ueId('other-') === 'other-2';
ueId('test-') === 'test-3';
update

src/update.js:23-23

Sets the value at key to the result of updater.

Parameters

Examples

t incrementCount = update('count', n => n + 1);
ementCount({count: 1});
eturns {count: 2}

Returns (Array | Iterable | Object)

updateIn

src/updateIn.js:31-31

Apply updater to the value at keyPath.

Parameters

Examples

t incrementUserCount = updateIn(['users', 'count'], n => n + 1);
ementUserCount({users: {count: 1}});
eturns {users: {count: 2}}

Unset keyPaths will be set based on the most recent type.

t incrementUserCount = updateIn(['users', 'count'], (n = 0) => n + 1);
ementUserCount({});
eturns {users: {count: 1}}

ementUserCount(Map());
eturns Map { users => Map { count => 1 } }

Returns (Array | Iterable | Object)

where

src/where.js:25-25

Takes items in subject that match pattern.

Parameters

Examples

t users = Map({
3: {id: '123', name: 'Jack'},
6: {id: '456', name: 'Jill'},


e({name: 'Jack'}, users);
eturns Map { 123: {id: '123', name: 'Jack'} }

Returns Iterable

without

src/without.js:23-23

Removes values in unwanted from subject.

Parameters

Examples

t removeOne = without(Set.of(1));

veOne(Set.of(1, 2, 3)) // returns Set { 2, 3 }

Returns Iterable


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.