JavaScript-oriented utilities for functional programming.
Functions with arity more than 1 are curried.
npm install @yurkimus/functions
"@yurkimus/functions": "npm:@yurkimus/functions"
"@yurkimus/functions": "github:yurkimus/functions"
"@yurkimus/functions": "https://raw.githubusercontent.com/yurkimus/functions/main/source/index.js"
Modules:
Runtime:
- Array.prototype.at
- Array.prototype.every
- Array.prototype.includes
- Array.prototype.reduce
- Array.prototype.reduceRight
- Array.prototype.slice
- Array.prototype.[@@iterator]
- Function.prototype.bind
- Object.hasOwn
- Reflect.construct
- Optional chains (?.)
aggregate :: function -> ...function -> ...parameters -> *
// To do
apply :: function -> parameters -> *
apply(Math.pow, [2, 3]) // => 8
applyTo :: parameters -> function -> *
applyTo([2, 3], Math.pow) // => 8
arity :: number -> function -> ...parameters -> *
arity(1, Math.max, 1, 2, 3) // => 1
unary :: function -> ...parameters -> *
unary(Math.max, 1, 2, 3) // => 1
binary :: function -> ...parameters -> *
binary(Math.max, 1, 2, 3) // => 2
assign :: parameter -> string -> object
// Returns the result of the assignment operation, see "effect" to prevent this behaviour
assign(1, 'a', {}) // => 1
compose :: ...function -> ...parameters -> *
compose(Math.sqrt, Math.abs)(-25) // => 5
condition :: function -> function -> function -> *
condition(
(array) => array.length > 0,
() => true,
() => false,
[],
) // => false
construct :: function -> ...parameters -> *
construct(URLSearchParams, [['x', 1], ['y', 2]]) // => URLSearchParams { size: 2 }
defer :: function -> ...parameters -> (() -> *)
defer(console.log, '[message]') // => () => console.log('[message]')
effect :: function -> parameter -> parameter
effect(console.log, 5) // => 5
enforce :: function -> parameter -> ...parameters -> parameter
enforce(console.log, true, 'a') // => Logs 'a', returns true
extract :: ...function -> ...parameters -> *
extract(
field('search'),
field('limit'),
)(
new URLSearchParams([
['search', 'todo'],
['limit', 10],
]),
) // => ['todo', '10']
field :: string -> * -> *
field('key', new URLSearchParams([['key', 1]])) // => 1
field('key', new Map([['key', 1]])) // => 1
// When working with 'Object' type, the function uses `Object.hasOwn` to check if the object has it's own proeprty. To access properties through the prototype chain as well, you can use `prop` function
field('toString', {}) // => false
fields :: string[] -> * -> *
fields(['a', 'key'], new URLSearchParams([['a', 0], ['key', 1]])) // => [0,1]
fields(['key'], new Map([['key', 1]])) // => [1]
hasField :: string -> * -> boolean
hasField :: string[] -> * -> boolean
hasField('key', new URLSearchParams([['key', 1]])) // => true
hasField('key', new Map([['key', 1]])) // => true
hasProp :: string -> * -> boolean
hasProp :: string[] -> * -> boolean
hasProp('size', new Map([['key', 1]])) // => true
hasProp('key', new Map([['key', 1]])) // => false
identity :: parameter -> parameter
identity(5) // => 5
invoke :: string -> object -> ...parameters -> *
// Arguments are required
invoke('sqrt', Math, 25) // => 5
method :: string -> object -> ...parameters -> *
// Same order as when using "invoke", arguments are optional
method('of', Array) // => []
method('of', Array, 1) // => [1]
modify :: object -> * -> *
modify({
key: 'a',
length: (n) => n + 5,
}, {
key: 'a',
length: 0,
}) // => { key: 'a', length: 5 }
objectOf :: string -> parameter -> object
objectOf :: string[] -> parameter[] -> object
objectOf('n', 0) // => { n: 0 }
objectOf(['a', 'b'], [0, 1]) // => { a: 0, b: 1 }
partial :: function -> ...parameters -> *
partial((a, b) => a + b, 2) // => (b) => a + b
prop :: string -> * -> *
prop :: string[] -> * -> *
prop('key', { key: 1 }) // => 1
prop(['key', 'a'], { key: { a: 1 } }) // => 1
props :: string[] -> * -> *
props :: string[][] -> * -> *
props(['key'], { key: 1 }) // => [1]
props(['a', ['key', 'b']], { a: 0, key: { b: 1 } }) // => [0,1]
raise :: * -> ThrowCompletion
ECMA 6.2.4.2 | ThrowCompletion
raise('Message') // => throws 'Message'
satisfies :: function -> ...parameters -> boolean
satisfies((n) => n == 0, 0) // => true
then :: function -> PromiseLike -> PromiseLike
then((n) => n + 5, Promise.resolve(0)) // => Promise { 5 }
trigger :: string -> ...parameters -> object -> *
// The order is different from when using "invoke", arguments are required
trigger('sqrt', 25)(Math) // => 5
trigger('pow', 2, 5)(Math) // => 32
unless :: function -> function -> ...parameters -> *
unless(
(n) => n == 0,
console.log.bind(console, 'not equals 0, value: '),
1,
) // => logs 'not equals 0, value: 1'
use :: function -> ...function -> ...parameters -> *
use(
Math.pow,
(n) => n + 2,
(n) => n + 3,
)(0, 0) // => 8
when :: function -> function -> ...parameters -> *
when(
(n) => n == 0,
console.log.bind(console, 'equals 0, value: '),
0,
) // => logs 'equals 0, value: 0'