Skip to content

JavaScript-oriented utilities for functional programming

License

Notifications You must be signed in to change notification settings

yurkimus/functions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Functions

JavaScript-oriented utilities for functional programming.

Functions with arity more than 1 are curried.

Table of Contents

Installation

npm

npm install @yurkimus/functions

urls

"@yurkimus/functions": "npm:@yurkimus/functions"
"@yurkimus/functions": "github:yurkimus/functions"
"@yurkimus/functions": "https://raw.githubusercontent.com/yurkimus/functions/main/source/index.js"

Requirements

Modules:

Runtime:

Exports

aggregate

Definition

aggregate :: function -> ...function -> ...parameters -> *

Example

// To do

apply

Definition

apply :: function -> parameters -> *

Example

apply(Math.pow, [2, 3]) // => 8

applyTo

Definition

applyTo :: parameters -> function -> *

Example

applyTo([2, 3], Math.pow) // => 8

arity

Definition

arity :: number -> function -> ...parameters -> *

Example

arity(1, Math.max, 1, 2, 3) // => 1

unary

Definition

unary :: function -> ...parameters -> *

Example

unary(Math.max, 1, 2, 3) // => 1

binary

Definition

binary :: function -> ...parameters -> *

Example

binary(Math.max, 1, 2, 3) // => 2

assign

Definition

assign :: parameter -> string -> object

Example

// Returns the result of the assignment operation, see "effect" to prevent this behaviour
assign(1, 'a', {}) // => 1

compose

Definition

compose :: ...function -> ...parameters -> *

Example

compose(Math.sqrt, Math.abs)(-25) // => 5

condition

Definition

condition :: function -> function -> function -> *

Example

condition(
  (array) => array.length > 0,
  () => true,
  () => false,
  [],
) // => false

construct

Definition

construct :: function -> ...parameters -> *

Example

construct(URLSearchParams, [['x', 1], ['y', 2]]) // => URLSearchParams { size: 2 }

defer

Definition

defer :: function -> ...parameters -> (() -> *)

Example

defer(console.log, '[message]') // => () => console.log('[message]')

effect

Definition

effect :: function -> parameter -> parameter

Example

effect(console.log, 5) // => 5

enforce

Definition

enforce :: function -> parameter -> ...parameters -> parameter

Example

enforce(console.log, true, 'a') // => Logs 'a', returns true

extract

Definition

extract :: ...function -> ...parameters -> *

Example

extract(
  field('search'),
  field('limit'),
)(
  new URLSearchParams([
    ['search', 'todo'],
    ['limit', 10],
  ]),
) // => ['todo', '10']

field

Definition

field :: string -> * -> *

Example

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

Definition

fields :: string[] -> * -> *

Example

fields(['a', 'key'], new URLSearchParams([['a', 0], ['key', 1]])) // => [0,1]
fields(['key'], new Map([['key', 1]])) // => [1]

hasField

Definition

hasField :: string -> * -> boolean
hasField :: string[] -> * -> boolean

Example

hasField('key', new URLSearchParams([['key', 1]])) // => true
hasField('key', new Map([['key', 1]])) // => true

hasProp

Definition

hasProp :: string -> * -> boolean
hasProp :: string[] -> * -> boolean

Example

hasProp('size', new Map([['key', 1]])) // => true
hasProp('key', new Map([['key', 1]])) // => false

identity

Definition

identity :: parameter -> parameter

Example

identity(5) // => 5

invoke

Definition

invoke :: string -> object -> ...parameters -> *

Example

// Arguments are required
invoke('sqrt', Math, 25) // => 5

method

Definition

method :: string -> object -> ...parameters -> *

Example

// Same order as when using "invoke", arguments are optional
method('of', Array) // => []
method('of', Array, 1) // => [1]

modify

Definition

modify :: object -> * -> *

Example

modify({
  key: 'a',
  length: (n) => n + 5,
}, {
  key: 'a',
  length: 0,
}) // => { key: 'a', length: 5 }

objectOf

Definition

objectOf :: string -> parameter -> object
objectOf :: string[] -> parameter[] -> object

Example

objectOf('n', 0) // => { n: 0 }

objectOf(['a', 'b'], [0, 1]) // => { a: 0, b: 1 }

partial

Definition

partial :: function -> ...parameters -> *

Example

partial((a, b) => a + b, 2) // => (b) => a + b

prop

Definition

prop :: string -> * -> *
prop :: string[] -> * -> *

Example

prop('key', { key: 1 }) // => 1
prop(['key', 'a'], { key: { a: 1 } }) // => 1

props

Definition

props :: string[] -> * -> *
props :: string[][] -> * -> *

Example

props(['key'], { key: 1 }) // => [1]
props(['a', ['key', 'b']], { a: 0, key: { b: 1 } }) // => [0,1]

raise

Definition

raise :: * -> ThrowCompletion

ECMA 6.2.4.2 | ThrowCompletion

Example

raise('Message') // => throws 'Message'

satisfies

Definition

satisfies :: function -> ...parameters -> boolean

Example

satisfies((n) => n == 0, 0) // => true

then

Definition

then :: function -> PromiseLike -> PromiseLike

Example

then((n) => n + 5, Promise.resolve(0)) // => Promise { 5 }

trigger

Definition

trigger :: string -> ...parameters -> object -> *

Example

// The order is different from when using "invoke", arguments are required
trigger('sqrt', 25)(Math) // => 5
trigger('pow', 2, 5)(Math) // => 32

unless

Definition

unless :: function -> function -> ...parameters -> *

Example

unless(
  (n) => n == 0,
  console.log.bind(console, 'not equals 0, value: '),
  1,
) // => logs 'not equals 0, value: 1'

use

Definition

use :: function -> ...function -> ...parameters -> *

Example

use(
  Math.pow,
  (n) => n + 2,
  (n) => n + 3,
)(0, 0) // => 8

when

Definition

when :: function -> function -> ...parameters -> *

Example

when(
  (n) => n == 0,
  console.log.bind(console, 'equals 0, value: '),
  0,
) // => logs 'equals 0, value: 0'

License

MIT

About

JavaScript-oriented utilities for functional programming

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published