SkillAgentSearch skills...

Transmute

kind of like lodash but works with Immutable

Install / Use

/learn @HubSpot/Transmute
About this skill

Quality Score

0/100

Supported Platforms

Universal

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
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

src/always.js:13-15

Creates a function that always returns returnValue.

Parameters

  • returnValue T

Examples

const alwaysBlue = always('blue');
alwaysBlue() === 'blue';

Returns T

bind

src/bind.js:18-18

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

Parameters

Examples

bind(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

const isOneToTen = both(
  n => n >= 1,
  n => n <= 10
);

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

Returns boolean

clear

src/clear.js:14-14

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

src/compose.js:28-31

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

src/concat.js:18-18

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

src/count.js:12-12

Returns the number of values in subject.

Parameters

  • subject TYPE

Examples

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

Returns number

curry

src/curry.js:14-16

Creates a curried version of operation.

Parameters

Examples

const toArray = curry((a, b, c) => [a, b, c]);
const toArrayWith1 = toArray(1);
toArrayWith1(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

  • arity number number of arguments the curried function accepts
  • operation Function to curry

Examples

const toArray = curryN(3)((...args) => [...args]);
toArray(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

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

src/difference.js:24-24

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

Parameters

  • toRemove Iterable
  • subject Iterable

Examples

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

removeOne(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

  • first Function
  • second **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/R
View on GitHub
GitHub Stars36
CategoryDevelopment
Updated1y ago
Forks13

Languages

JavaScript

Security Score

80/100

Audited on Nov 1, 2024

No findings