wasmuth
v2.1.0
Published
Practical, functional utilities that fallback on native implementations as much as possible
Downloads
37
Readme
wasmuth
Practical, functional utilities that fallback on native implementations as much as possible.
Why?
I love Ramda. But, some of the naming/semantics is not obvious for those newer to functional programming. Also, filesize (~7kb vs ~50kb 😵). Also, why not fallback to native implementations? Any performance concerns are not actually a bottleneck, and JavaScript VMs will continue to optimize these (map, filter, reduce, some etc.). Also, let me iterate objects without using a differently named export. And, finally, Ramda has a lot of extra functions I never used.
API
Every function is curryable. If it takes 2 arguments and you give it 1, it will return a partially applied function that expects only the 2 argument.
chunk
chunk(len: Number, input: Array): Array
Group an array into smaller arrays.
import chunk from '@wasmuth/chunk'
chunk(2, ['a', 'b', 'c', 'd'])
// => [['a', 'b'], ['c', 'd']]
compose
compose(fnN: Function, ..., fn1: Function): Function
Perform right-to-left function composition. That is, the value of each function (starting on the right), is passed to the next function.
import compose from '@wasmuth/compose'
compose(
map(n => n + 1),
Object.values
)({a: 1, b: 2, c: 3})
// => [2, 3, 4]
filter
filter(predicate: Function, input: Array|Object): Array|Object
import filter from '@wasmuth/filter'
const predicate = a => a % 2 === 0
filter(predicate, [1, 2, 3, 4]) // => [2, 4]
You can also filter over objects:
import filter from '@wasmuth/filter'
filter((val, key) => key === 'a' || val === 2, {a: 1, b: 2, c: 3})
// => {a: 1, b: 2}
find
find(predicate: Function, ls: Array): Array|undefined
It returns undefined
or the first element of ls satisfying predicate.
import find from '@wasmuth/find'
find(v => v === 'a', ['b', 'a', 'd', 'c'])
// => 'a'
find-index
findIndex(predicate: Function, ls: Array): Array|undefined
It returns undefined
or the first index of an element satisfying predicate.
import findIndex from '@wasmuth/find-index'
find(v => v === 'a', ['b', 'a', 'd', 'c'])
// => 1
from-pairs
fromPairs(ls: Array): Object
import fromPairs from '@wasmuth/from-pairs'
fromPairs([['a', 1], ['b', 2]])
// => {'a': 1, 'b': 2}
group-by
groupBy(prop: String, ls: Array): Object
Gather objects based on their value of prop
.
import groupBy from '@wasmuth/group-by'
const src = [{a: 1, b: 1}, {a: 1, b: 2}, {a: 2, b: 3}]
groupBy('a', src)
// => {1: [{a: 1, b: 1}, {a: 1, b: 2}], 2: [{a: 2, b: 3}]}
group-prop-by
groupPropBy(valProp: String, prop: String, ls: Array): Object
Gather the value of valProp
from each object based on their value of prop
.
import groupPropBy from '@wasmuth/group-prop-by'
const src = [{a: 1, b: 1}, {a: 1, b: 2}, {a: 2, b: 3}]
groupPropBy('b', 'a', src)
// => {1: [1, 2], 2: [3]}
includes
includes(search: Any, input: String|Array): Boolean
Just a curryable wrapper that maps to either:
Array.prototype.includes
String.prototype.includes
import includes from '@wasmuth/includes'
includes('c', ['a', 'b', 'c', 'd'])
// => true
join
join(delim: String?, arr: Array): String
Curryable wrapper around native Array.prototype.join
.
import join from '@wasmuth/join'
const arr = ['a', 'b', 'c', 'd']
join(arr)
// => 'a,b,c,d'
join()(arr)
// => 'a,b,c,d'
join('-', arr)
// => 'a-b-c-d'
last
join(arr: Array): Any
Return the last element of the given array.
import last from '@wasmuth/last'
const arr = ['a', 'b', 'c', 'd']
last(arr)
// => 'd'
map
map(mapFn: Function, input: Array|Object): Array|Object
Returns the result of calling mapFn
on each element in the iterable input
. Can be an Array or Object.
import map from '@wasmuth/map'
const double = x => x * 2
map(double, [1, 2, 3])
// => [2, 4, 6]
map((key, val) => key === 'a' ? double(val) : val, {a: 1, b: 2})
// => {a: 2, b: 2}
path
path(path: String|Array, obj: Object): Any
Return the value at the given path, for a given object or array.
import path from '@wasmuth/path'
const obj = {a: {b: 2}}
path(['a', 'b'], obj)
// => 2
path('a.b', obj)
// => 2
path-eq
pathEq(path: String|Array, val: Any, obj: Object): Boolean
Check if the value at a given path, for a given object equals val
.
import pathEq from '@wasmuth/path-eq'
const obj = {a: {b: 2}}
pathEq(['a', 'b'], 2, obj)
// => true
pathEq('a.b', 4, obj)
// => false
path-or
pathOr(def: Any, path: String|Array, obj: Object): Any
Return the value at the given path, for a given object or array, or def
if falsy.
import pathOr from '@wasmuth/path-or'
const obj = {a: {b: 2}}
pathOr(5, ['x', 'y'], obj)
// => 5
pathOr(11, 'a.b', obj)
// => 2
path-remove
pathRemove(path: String|Array, obj: Object): Any
Remove the value at the given path, for a given object or array.
import pathRemove from '@wasmuth/path-remove'
const obj = {a: {b: 2, c: 3}}
pathRemove(['a', 'b'], obj)
// => {a: {c: 3}}
path-set
pathSet(path: String|Array, def: Any, obj: Object): Any
Set the value on a given path for a given object.
import pathSet from '@wasmuth/path-set'
const obj = {a: {b: 1}}
pathSet(['a', 'b'], 2, obj)
// => {a: {b: 2}}
pathSet('a.b', 3, obj)
// => {a: {b: 3}}
pathSet('0.a.b', 3, [obj])
// => [{a: {b: 3}}]
path-update
pathUpdate(path: String|Array, value: any, obj: Object): Any
Update the value on a given path for a given object.
If the existing path target and the new value are both arrays, concat the value. If the existing path target and the new value are both objects, merge the value.
import pathUpdate from '@wasmuth/path-update'
const obj = {a: {b: 1, c: [10, 20]}}
pathUpdate('a', {d: 2}, obj)
// => {a: {b: 1, c: [10, 20], d: 2}}
pathUpdate('a.b', 3, obj)
// => {a: {b: 3, c: [10, 20]}}
pathUpdate('a.c', 30, obj)
// => {a: {b: 1, c: 30}}
pathUpdate('a.c', [30], obj)
// => {a: {b: 1, c: [10, 20, 30]}}
pick
pick(keys: Array, obj: Object): Object
Return obj with only the keys specified.
import pick from '@wasmuth/pick'
pick(['a', 'c'], {a: 1, b: 2, c: 3})
// => {a: 1, c: 3}
pipe
pipe(fn1: Function, ..., fnN: Function): Function
Perform left-to-right function composition. That is, the value of each function (starting on the left), is passed to the next function.
import pipe from '@wasmuth/pipe'
pipe(
Object.values,
map(n => n + 1)
)({a: 1, b: 2, c: 3})
// => [2, 3, 4]
range
range(from: Number, to: Number): Array
Returns a list of numbers from from
(inclusive) to to
(exclusive).
import range from '@wasmuth/range'
range(0, 6)
// => [0, 1, 2, 3, 4, 5]
reduce
reduce(fn: Function, initVal: Any, arr: Array): Any
Curryable wrapper around native Array.prototype.reduce
.
import reduce from '@wasmuth/reduce'
const arr = [1, 2, 3, 4]
reduce((acc, elm) => acc + elm, 0, arr)
// => 10
reduce((acc, elm) => acc + elm, 0)(arr)
// => 10
reduce((acc, elm) => acc + elm)(0)(arr)
// => 10
to-pairs
toPairs(obj: Objectr): Array
import toPairs from '@wasmuth/to-pairs'
toPairs({a: 1, b: 2, c: 3})
// => [['a', 1], ['b', 2], ['c', 3]]