@bchar/b-to-c v0.2.3
(b -> c)
An extended library for data transformation. Includes all functions from (a -> b).
npm install @bchar/b-to-c
Array
const A = require('@bchar/b-to-c/array')
compact :: Array a -> Array a
Returns an array of only truthy values.
> A.compact([0, 1, true, false, 'hey', null])
[ 1, true, 'hey' ]
difference :: Array a -> Array a -> Array a
Returns an array with the elements present in the first that are not in the second.
> A.difference([1, 2, 3], [1])
[ 2, 3 ]
dropWhile :: (a -> Boolean) -> Array a -> Array a
Drops the first items of an array which match the predicate.
> A.dropWhile(N.isEven, [2, 4, 5, 6])
[ 5, 6 ]
flatten :: Array a -> Array a
Recursively flattens an array of arrays.
> A.flatten([1, [[2], 3], [4, [[5]]]])
[ 1, 2, 3, 4, 5 ]
groupBy :: (a -> b) -> Array a -> Object k (Array a)
Returns an object with keys as the result of applying a function to elements, and the value is an array of every matched element.
> A.groupBy(Math.floor, [4.2, 6.1, 6.4])
{ '4': [ 4.2 ], '6': [ 6.1, 6.4 ] }
init :: Array a -> Array a
Returns every element except the last.
> A.init([1, 2, 3, 4, 5])
[ 1, 2, 3, 4 ]
intersection :: Array a -> Array a -> Array a
Returns an array containing elements present in both arrays.
> A.intersection([1, 2, 3], [2, 3, 4, 5])
[ 2, 3 ]
last :: Array a -> Maybe a
Returns the last element in an array.
> A.last([1, 2, 3, 4, 5])
5
> A.last([])
undefined
maxBy :: (a -> b) -> Array a -> Maybe a
Returns the highest value element in an array.
> A.maxBy(S.length, ['bc', 'abc', 'a', 'b'])
'abc'
mean :: Array Number -> Maybe Number
Returns the mean of the elements in an array.
> A.mean([1, 2, 3, 4, 5])
3
minBy :: (a -> b) -> Array a -> Maybe a
Returns the lowest value element in an array after applying a function to the element.
> A.minBy(S.length, ['bc', 'abc', 'a', 'b'])
'a'
partition :: (a -> Boolean) -> Array a -> Array (a, a)
Equivalent to [filter(f, arr), reject(f, arr)]
.
> A.partition(N.isEven, [1, 2, 3, 4, 5])
[ [ 2, 4 ], [ 1, 3, 5 ] ]
product :: Array Number -> Number
Returns the product of the elements in an array.
> A.product([1, 2, 3])
6
reject :: (a -> Boolean) -> Array a -> Array a
Returns an array of all elements that do not match the predicate.
> A.reject(N.isEven, [1, 2, 3, 4, 5])
[ 1, 3, 5 ]
scan :: (a -> b -> b) -> b -> Array a -> Array b
Like reduce
, but returns a list with the initial value, the intermediate values, and the final value.
> A.scan(N.add, 0, [1, 2, 3])
[ 0, 1, 3, 6 ]
sortBy :: (a -> b) -> Array a -> Array a
Sort an array by applying a function to elements.
> A.sortBy(S.length, ['abc', 'a', 'ab'])
[ 'a', 'ab', 'abc' ]
> A.sortBy(O.get('name'), [{ name: 'bob'}, { name: 'alice' }, { name: 'charlie' }])
[ { name: 'alice' }, { name: 'bob' }, { name: 'charlie' } ]
span :: (a -> Boolean) -> Array a -> Array (a, a)
Equivalent to [takeWhile(f, arr), dropWhile(f, arr)]
.
> A.span(N.isEven, [2, 4, 5, 6])
[ [ 2, 4 ], [ 5, 6 ] ]
sum :: Array Number -> Number
Sums up the elements in an array.
> A.sum([1, 2, 3, 4, 5])
15
takeWhile :: (a -> Boolean) -> Array a -> Array a
Returns the first elements in an array which match the predicate.
> A.takeWhile(N.isEven, [2, 4, 5, 6])
[ 2, 4 ]
union :: Array a -> Array a -> Array a
Returns an array of unique elements in both arrays.
> A.union([1, 5, 7], [3, 5])
[ 1, 5, 7, 3 ]
uniqueBy :: (a -> b) -> Array a -> Array a
Returns an array of unique values from the applied function.
> A.uniqueBy(S.length, ['and', 'here', 'are', 'some', 'words'])
[ 'and', 'here', 'words' ]
Function
const F = require('@bchar/b-to-c/function')
ap :: Array (a -> b) -> Array a -> Array b
Applies a list of functions over a list of values.
> F.ap([N.mul(2), N.add(3)], [1, 2, 3])
[ 2, 4, 6, 4, 5, 6 ]
both :: (a -> Boolean) -> (a -> Boolean) -> a -> Boolean
Applies a value over two predicate functions, returns the result of an and
comparison. Short-circuits if the first predicate returns false.
> const test = F.both(F.gt(10), F.lt(20))
> test(15)
true
> test(30)
false
cond :: Array (a -> Boolean, a -> b) -> a -> b
Contains predicate and transformer pairs, to determine which transformer to apply over a value.
> F.cond([
[F.lt(1), N.inc],
[F.gt(1), N.dec],
[F.always(true), F.identity]
], 10)
9
either :: (a -> Boolean) -> (a -> Boolean) -> a -> Boolean
Applies a value over two predicate functions, returns the result of an or
comparison. Short-circuits if the first predicate returns true.
> const test = F.either(F.gt(10), N.isEven)
> test(15)
true
> test(5)
false
> test(4)
true
on :: (b -> b -> c) -> (a -> b) -> a -> a -> c
Applies a binary function over a unary function twice.
> const sameLength = F.on(F.equals, S.length)
> sameLength('hey', 'now')
true
when :: (a -> Boolean) -> (a -> b) -> a -> b
If the predicate is matched, run a transformer function, otherwise return the original value.
> F.when(F.lt(10), N.inc, 5)
6
Number
const N = require('@bchar/b-to-c/number')
clamp :: Number -> Number -> Number -> Number
Retricts a number to be within a range.
> const test = clamp(1, 10);
> test(0)
1
> test(20)
10
> test(5)
5
dec :: Number -> Number
Decrements a number.
> N.dec(10)
9
gcd :: Number -> Number -> Number
Greatest common denominator.
> N.gcd(12, 18)
6
inc :: Number -> Number
Increments a number.
> N.inc(10)
11
lcm :: Number -> Number -> Number
Least common multiple.
> N.lcm(12, 18)
36
mod :: Number -> Number -> Number
Behaves like the mathematical modulo operator.
> N.mod(-20, 3)
1
negate :: Number -> Number
Negated number.
> N.negate(10)
-10
pow :: Number -> Number -> Number
Returns the power.
> N.pow(2, -2)
4
rem :: Number -> Number -> Number
Returns the remainder.
> N.rem(3, -20)
-2
Object
const O = require('@bchar/b-to-c/object')
gets :: Array k -> Object k v -> Array (Maybe v)
Returns an array of values where Object k is found.
> O.gets(['a', 'b'], { a: 1, b: 2 })
[ 1, 2 ]
path :: Array k -> Object k v -> Maybe v
Returns a value from a nested object path.
> O.path(['a', 'b', 'c'], {a: {b: {c: 1}}})
1
where :: Object k (v -> Boolean) -> Object k v -> Boolean
Returns whether every key matches the predicate.
> const test = O.where({
a: F.equals('foo'),
b: F.not(F.equals('bar')),
x: F.gt(10),
y: F.lt(20)
})
> test({ a: 'foo', b: 'baz', x: 15, y: 15 })
true
> test({ a: 'foo', b: 'baz': x: 10, y: 15 })
false
String
const S = require('@bchar/b-to-c/string')
capitalize :: String -> String
Capitalize a string.
> S.capitalize('hello, world!')
'Hello, world!'
dropWhile :: (String -> Boolean) -> String -> String
Drops the first elements of a string that pass the predicate.
> S.dropWhile(equals('m'), 'mmmhmm')
'hmm'
repeat :: Number -> String -> String
Repeated a string n amount of times.
> S.repeat(3, 'abc')
'abcabcabc'
span :: (String -> Boolean) -> String -> Array (String, String)
Equivalent to [takeWhile(f, str), dropWhile(f, str)]
.
> S.span(F.equals('m'), 'mmmhmm')
[ 'mmm', 'hmm' ]
takeWhile :: (String -> Boolean) -> String -> String
Takes the first elements of a string that pass the predicate.
> S.takeWhile(F.equals('m'), 'mmmhmm')
'mmm'