0.1.0 • Published 5 years ago

@wasmuth/const-to-camel v0.1.0

Weekly downloads
191
License
ISC
Repository
github
Last release
5 years ago

wasmuth

Build Status

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

Source Tests


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}

Source Tests


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'

Source Tests


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

Source Tests


from-pairs

fromPairs(ls: Array): Object

import fromPairs from '@wasmuth/from-pairs'
fromPairs([['a', 1], ['b', 2]])
// => {'a': 1, 'b': 2}

Source Tests


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

Source Tests


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

Source Tests


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

Source Tests


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'

Source Tests


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((val, prop) => prop === 'a' ? double(val) : val, {a: 1, b: 2})
// => {a: 2, b: 2}

Source Tests


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

Source Tests


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

Source Tests


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

Source Tests


path-set

pathSet(def: Any, path: String|Array, 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}}]

Source Tests


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}

Source Tests


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]

Source Tests


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]

Source Tests


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

Source Tests