Skip to main content
Module

x/froebel/mod.ts>nullishChain

A strictly typed utility library.
Go to Latest
variable nullishChain
import { nullishChain } from "https://deno.land/x/froebel@v0.22.0/mod.ts";

Given a list of functions that accept the same parameters, returns a function that given these arguments returns the result of the first function whose result is not nullish.

This is equivalent to chaining together invocations of the passed in functions with the given arguments with nullish coalescing (??) operators.

Examples

Example 1

const isAdult   = (age: number) => { if (n >= 18) return 'adult' }
const isToddler = (age: number) => { if (n <= 3) return 'toddler' }

const ageGroup = nullishChain(isAdult, isToddler, () => 'child')

// this is functionally equivalent to:
const ageGroup = age => isAdult(age) ?? isToddler(age) ?? 'child'

ageGroup(1)  // prints: 'toddler'
ageGroup(10) // prints: 'child'
ageGroup(50) // prints: 'adult'

type

<FF extends λ, FR extends λ<Parameters<FF>>[]>(...unnamed 0: [FF, ...FR] | []) => unknown