Skip to content

chain

Create a chain of function to run in order

93 bytes

Usage

Chaining functions will cause them to execute one after another, passing the output from each function as the input to the next, returning the final output at the end of the chain.

import * as _ from 'radashi'
const add = (y: number) => (x: number) => x + y
const multiply = (y: number) => (x: number) => x * y
const addFive = add(5)
const double = multiply(2)
const chained = _.chain(addFive, double)
chained(0) // => 10
chained(7) // => 24

Example

import * as _ from 'radashi'
type Deity = {
name: string
rank: number
}
const gods: Deity[] = [
{ rank: 8, name: 'Ra' },
{ rank: 7, name: 'Zeus' },
{ rank: 9, name: 'Loki' },
]
const getName = (god: Deity) => item.name
const upperCase = (text: string) => text.toUpperCase() as Uppercase<string>
const getUpperName = _.chain(getName, upperCase)
getUpperName(gods[0]) // => 'RA'
gods.map(getUpperName) // => ['RA', 'ZEUS', 'LOKI']