Functional JavaScript
In a browser e.g.
<script type="module">
import * as fnArray from './array.mjs'
console.log(fnArray.head(['foo', 'bar']))
</script>
In Node.js:
The --experimental-modules
flag can be used to enable features for loading ESM modules.
node --experimental-modules .
$ npm init -y
$ npm i daliborgogic/functional
$ touch index.mjs
// index.mjs
import * as fnArray from './node_modules/functional/array'
console.log(fnArray.head(['foo', 'bar']))
$ npm start
> foo
Combine multiple arrays into one array.
combine(['foo'], ['bar', 'baz'], [1, 2]) // => ['foo', 'bar', 'baz', 1, 2]
Returns a copy of the array with all falsy values removed.
compact([0, 1, false, 2, '', 3]) // => [1, 2, 3]
Returns true if the value is present in the array.
contains([1, 2, 3], 3) // => true
Similar to without, but returns the values from array that are not present in the other arrays.
difference([1, 2, 3, 4, 5], [5, 2, 10]) // => [1, 3, 4]
Returns the first element of an array.
head(['foo', 'bar']) // => 'foo'
Returns everything but the last entry of the array.
initial([3, 2, 1]) // => [3, 2]
Computes the list of values that are the intersection of all the arrays. Each value in the result is present in each of the arrays.
intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]) // => [1, 2]
Returns the last element of an array.
last(['foo', 'bar' ]) // => 'bar'
Determine the index at which the value should be inserted into the array in order to maintain the array's sorted order.
sortedIndex([10, 20, 30, 40, 50], 35) // => 3
Returns everything but the first entry of the array.
tail(['foo', 'bar', 'baz']) // => ['bar', 'baz']
Returns a real Array. Useful for transmuting the arguments object.
Array.isArray((() => toArray(arguments))('foo', 'bar')) // => true
Computes the union of the passed-in arrays: the list of unique items, in order, that are present in one or more of the arrays.
union([1, 2, 3], [101, 2, 1, 10], [2, 1]) // => [1, 2, 3, 101, 10]
Produces a duplicate-free version of the array.
unique([1, 2, 1, 3, 1, 4]) // => [1, 2, 3, 4]
Returns a copy of the array with all instances of the values removed.
without([1, 2, 1, 0, 3, 1, 4], 0, 1) // => [2, 3, 4]
Returns an array with the object's values.
getValues({ foo: 'bar', hello: 'world' }) // => ['bar', 'world']
Combine multiple objects into a new object.
merge({ foo: 'bar' }, { hello: 'world' }) // => { foo: 'bar', hello: 'world' }
Convert an Object to a Map.
toMap({ name: 'Dalibor', age: 40 }) // => Map { name: 'Dalibor', age: 40 }
Returns the minimum value in the array.
min([10, 50, 30]) // => 10
Returns the maximum value in the array.
max([10, 50, 30]) // => 50
Returns the sum of all values in the array.
sum([1, 2, 3]) // => 6
Returns the product of all values in the array.
product([2, 5, 10]) // => 100
Creates a new function returning the opposite of the function provided as its argument.
const isNull = x => x == null
const isSet = not(isNull)
isSet(undefined) // => false
Returns a new function that won't execute if not enough arguments are provided.
let greet = (message, name) => console.log(message + ' ' + name)
let safeGreet = requireArguments(greet)
greet('Hi') // => 'Hi undefined'
safeGreet('Hi') // => Doesn't execute
Returns a new function that won't execute more than once.
const greet = () => console.log('Hi')
const greetOnce = once(greet)
greetOnce() // => 'Hi'
greetOnce() // => Doesn't execute
Curries a function.
const add = curry((a, b) => a + b)
add(2, 3) // => 5
add(2)(3) // => 5
Returns the composition of multiple functions from left to right.
const plus1 = a => a + 1
const mult2 = a => a * 2
let addThenMult = pipeline(plus1, mult2)
addThenMult(5) // => 12