0.7.4 • Published 8 years ago

powerglove v0.7.4

Weekly downloads
9
License
MIT
Repository
github
Last release
8 years ago

Powerglove

npm install powerglove

API

pipe

[(a -> *)] -> a -> Promise -> *

Passes a value through an array of functions sequentially; Returns value fulfilled from final function in the array.

Example:

import { pipe } from 'powerglove'

void async () => {

  const weirdMath = pipe([
    n => n / 1,
    n => n + 4,
    n => n * 7
  ])

  await weirdMath(2)
  // -> 42

}()

Params:

TypeNameDescription
Function[]funcsArray of functions
*xAny value

Return:

TypeDescription
PromiseFulfills with value returned by last index of func

all

[(a -> *)] -> a -> Promise -> [*]

Executes an array of functions concurrently; returns an array of fulfilled values.

Example:

import { all } from 'powerglove'

void async () => {

  const sayHi = all([
    x => `Hello, ${x}!`,
    x => `¡Hola, ${x}!`,
    x => `Bonjour, ${x}!`
  ])

  await sayHi('world')
  // -> [ 'Hello, world!', '¡Hola, world!', 'Bonjour, world!' ]

}()

Params:

TypeNameDescription
Function[]funcsArray of functions
*xAny value

Return:

TypeDescription
PromiseFulfills with an array of values returned from each function in funcs

race

[(a -> *)] -> a -> Promise -> *

Executes an array of functions concurrently; Returns value of first function to resolve.

Example:

import { race, delay } from 'powerglove'

void async () => {

  const fast = delay(200)(x => `${x} Speed Racer`)

  const faster = delay(100)(x => `${x} Racer X`)

  const fastest = delay(0)(x => `${x} Chim Chim`)

  const announceWinner = race([
    fast,
    faster,
    fastest
  ])

  await announceWinner('And the winner is...')
  // -> `And the winner is... Chim Chim!`

}()

Params:

TypeNameDescription
Function[]funcsArray of functions
*xAny value

Return:

TypeDescription
PromiseFulfills with value of first function in funcs to resolve

until

(a -> Bool) -> (a -> *) -> a -> Promise -> *

Executes function until done returns true. The return value of the repeating function is passed into itself on each successive iteration.

Example:

import { until } from 'powerglove'

void async () => {

  const minusminus = x => x - 1

  const smallEnough = x => x <= 0

  const subtractAll = until(smallEnough)(minusminus)

  await subtractAll(100000)
  // -> 0

  const plusplus = x => x + 1

  const largeEnough = x => x >= 100000

  const addALot = until(largeEnough)(plusplus)

  await addALot(0)
  // -> 100000

}()

Params:

TypeNameDescription
FunctiondoneAccepts value returned by f. f is called repeatedly until this function returns true
FunctionfFunction to be called repeatedly. Passes its own return value into itself on each iteration
*xAny value

Return:

TypeDescription
PromiseFulfills result of f after n recursive calls

when

(a -> Bool) -> (a -> *) -> (a -> *) -> a -> Promise -> *

Tests a value and passes value to either the pass function or the fail function

Example:

import { when } from 'powerglove'

void async () => {

  const over9000 = lvl => lvl > 9000

  const praise = lvl => `Holy crap! ${lvl}?! THAT'S OVER 9000!`

  const insult = lvl => `Pffft. ${lvl}? Is that all you got???`

  const analyzePowerLevel = when(over9000)(praise)(insult)

  await analyzePowerLevel(9001)
  // -> `Holy crap! 9001?! THAT'S OVER 9000!`

  await analyzePowerLevel(1)
  // -> `Pffft. 1? Is that all you got???`

}()

Params:

TypeNameDefaultDescription
FunctiontestAccepts x; returns true or false
FunctionpassCalled with x if test returns true
Functionfaila => aCalled with x if test returns false
*xAny value

Return:

TypeDescription
PromiseFulfills with value returned by pass or fail

delay

Number -> (a -> *) -> a -> Promise -> *

Accepts ms, number of milliseconds to wait, before executing function f.

Example:

import { delay } from 'powerglove'

void async () => {

  const timeSince = delay(300)(ms => Date.now() - ms)

  await timeSince(Date.now())
  // -> ~300ms

}()

Params:

TypeNameDescription
NumberfuncsArray of functions
FunctionfFunction to be called after timeout
*xAny value

Return:

TypeDescription
PromiseFulfills with value returned by f(x)
0.7.4

8 years ago

0.7.3

8 years ago

0.7.2

8 years ago

0.7.1

8 years ago

0.7.0

9 years ago

0.6.3

9 years ago

0.6.2

9 years ago

0.6.1

9 years ago

0.6.0

9 years ago

0.5.2

9 years ago

0.5.1

9 years ago

0.5.0

9 years ago

0.4.0

9 years ago

0.3.5

9 years ago

0.3.4

9 years ago

0.2.4

9 years ago

0.2.3

9 years ago

0.2.2

9 years ago

0.2.1

9 years ago

0.2.0

9 years ago

0.1.0

9 years ago