Figment
GithubIssue TrackerSponsor
  • Introduction
  • F.A.Q.
  • Changelog
  • API
    • Functions
Powered by GitBook
On this page
  • identity
  • or
  • and
  • apply
  • map
  • chain
  • constant
  • cond
  • equals
  • first
  • nth
  • index
  • last
  • rest
  • greaterThan
  • greaterThanEquals
  • lessThan
  • lessThanEquals
  • isBetween
  • pluck
  • omit
  • pipe

Was this helpful?

  1. API

Functions

These are the available library functions.

identity

A function that takes a single parameter and returns it. Essentially a no-op for mapping.

map(identity, [1, 2, 3])
type or = <Args extends any[]>(
  ...options: Array<(...args: Args) => unknown>
) => (...args: Args): boolean

or

Given several methods which can return a boolean, create a new predicate which logically "ors" them together.

const isNotATeen = or(lessThan(13), greaterThan(19))
isNotATeen(13)
type or = <Args extends any[]>(
  ...options: Array<(...args: Args) => unknown>
) => (...args: Args): boolean

and

Given several methods which can return a boolean, create a new predicate which logically "ands" them together.

const isBetweenFiveAndTen = and(greaterThan(5), lessThan(10))
isBetweenFiveAndTen(7)
type and = <Args extends any[]>(
  ...options: Array<(...args: Args) => unknown>
) => (...args: Args): boolean

apply

Given a function that takes multiple parameters, create a new function which takes those parameters as a single tuple instead.

const adder = (a: number, b: string) => a + b
const tupleAdder = apply(adder)
adder(5, 10)
tupleAdder([5, 10])
type apply = <T, Args extends any[]>(fn: (...args: Args[]) => T): (args: Args) => T

map

Exactly like Array.prototype.map, but functional instead of on the Array prototype.

const adder = map(a => a + 1)
adder([1, 2, 3])
type map = <T, U>(fn: (a: T) => U) => (data: T[]): U[]

chain

Given a value and a function, run the function on the value. This is like map, but for working on any data type. Useful in piped series of computations.

const adder = chain(a => a + 1)
adder(data)
type chain = <T, U>(fn: (a: T) => U) => (data: T): U

constant

Given a value, return a function that will return that value every time it is called.

const alwaysTrue = constant(true)
alwaysTrue()
type constant = <T>(value: T) => () => T

cond

Basically a functional switch statement. Run several predicates to find the correct code path, then transform the data as requested.

const detectRange = cond(
  [lessThan(0), () => "Negative"],
  [lessThan(100), () => "Less than 100"],
  () => "Greater than 100",
)

detectRange(101)
type cond = <T, U>(conditions: Condition<T, U>[]): (data: T) => U | undefined

equals

Given a value, create a new function which will take a second value and compare their equality. Uses Javascript's === operator.

const equalsFive = equals(5)
equalsFive(5)
equalsFive(10)
type equals = <T>(value: T) => (data: T): boolean

first

Given an array, return the first value or undefined.

first([1, 2, 3])
type first = <T>(items: T[]) => T | undefined

nth

Given an array, return the nth value or undefined. nth is 1-indexed.

nth([1, 2, 3])(1) // 1
type nth = (n: number) => <T>(items: T[]) => T | undefined

index

Given an array, return the index value or undefined. index is 0-indexed.

index([1, 2, 3])(1) // 2
type index = (n: number) => <T>(items: T[]) => T | undefined

last

Given an array, return the last value or undefined.

last([1, 2, 3])
type last = <T>(items: T[]) => T | undefined

rest

Given an array, return every item except the first.

rest([1, 2, 3]) // [2, 3]
type rest = <T>(items: T[]) => T[]

greaterThan

Create a predicate which checks whether a value is greater than a number.

const greaterThanFive = greaterThan(5)
greaterThanFive(10)
greaterThanFive(1)
type greaterThan = (value: number) => (data: number): boolean

greaterThanEquals

Create a predicate which checks whether a value is greater than or equal a number.

const greaterThanEqualsFive = greaterThanEquals(5)
greaterThanEqualsFive(10)
greaterThanEqualsFive(1)
type greaterThanEquals = (value: number) => (data: number): boolean

lessThan

Create a predicate which checks whether a value is less than a number.

const lessThanFive = lessThan(5)
lessThanFive(10)
lessThanFive(1)
type lessThan = (value: number) => (data: number): boolean

lessThanEquals

Create a predicate which checks whether a value is less than or equal a number.

const lessThanEqualsFive = lessThanEquals(5)
lessThanEqualsFive(10)
lessThanEqualsFive(1)
type lessThanEquals = (value: number) => (data: number): boolean

isBetween

Create a predicate which checks whether a value is between two values.

const isBetween5And10Exclusive = isBetween(5, 10, false)
isBetween5And10Exclusive(10)

const isBetween5And10Inclusive = isBetween(5, 10, true)
isBetween5And10Inclusive(1)
type isBetween = (
  a: number,
  b: number,
  inclusive?: boolean,
) => (data: number) => boolean

pluck

Create a function which grabs a key from an object.

const getName = pluck("name")
getName({ name: "Me", age: Infinity })
type pluck = <U, T extends keyof U>(key: T) => (data: U): U[T]

omit

Create a function which removes a key from an object.

const removeName = omit("name")
removeName({ name: "Me", age: Infinity })
type omit = <U, T extends keyof U>(key: T) => (data: U): Omit<U, T>

pipe

Create a function runs a series of functions in order, passing the result of each step to the next in the series.

const incrementToStringAndRepeat = pipe(
  (a: number) => a + 1,
  (b: number) => b.toString(),
  (c: string) => c + c,
)

incrementToStringAndRepeat(100)
type pipe = <A, B, C, D>(a: (data: A) => B, b: (data: B) => C, c: (data: C) => D): (data: A) => D
PreviousF.A.Q.

Last updated 2 years ago

Was this helpful?