Radash
  1. Curry
  2. chain

Basic 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 { chain } from 'radash'

const add = (y: number) => (x: number) => x + y
const mult = (y: number) => (x: number) => x * y
const addFive = add(5)
const double = (num: number) => num * 2

const chained = chain(addFive, double)

chained(0) // => 10
chained(7) // => 24

More example

import { chain } from 'radash'

type User = { id: number; name: string }
const users: User[] = [
  { id: 1, name: 'John Doe' },
  { id: 2, name: 'John Smith' },
  { id: 3, name: 'John Wick' }
]
const getName = <T extends { name: string }>(item: T) => item.name
const upperCase: (x: string) => Uppercase<string> = (text: string) => text.toUpperCase() as Uppercase<string>

const getUpperName = chain<User, Uppercase<string>>(getName, upperCase)
//                     ^ Use chain function here

getUpperName(users[0])                  // => 'JOHN DOE'
users.map((user) => getUpperName(user)) // => ['JOHN DOE', 'JOHN SMITH', 'JOHN WICK']
users.map(getUpperName)                 // => ['JOHN DOE', 'JOHN SMITH', 'JOHN WICK']
//              ^ use chained function as a Point-free