Info

This page is automatically generated from source code comments, tests, etc. If there are any mistakes on this page, need to correct them. If you find any mistakes, please report them as an issue.

API

Version: Latest

Cross-functional comparison
NameFonctionRambdaRamdaLodash
F
K
Nnotnotnot
NN
TstubTrue
add
adjust
advance
all
allPass
and
any
anyPass
append
applySpec
assoc
chunk
clonecloneDeep
compose
constructorName
converge
curry
dec
defaultTo
divide
drop
dropLastdropRight
equalequalsequalsisEqual
filter
find
findIndex
flattenDeepflattenflatten
gt
gte
has
headfirst
identity
ifElse
ifElseFnifElseifElse
inc
indexOf
init
is
isEmpty
last
lastIndexOf
lt
lte
match
merge
multiply
none
not
omit
or
over
pathpropsget
pipe
prepend
product
propsproppropget
replaceAll
subtract
sum
tail
take
takeLasttakeRight
tap
tryCatch
uniq
xor

Functions

add

Added from 1.0.0

🔗  subtract  multiply  divide 

Math

Adds first argument and second argument.

Signature:

add: {
    (a: number, b: number): number;
    (a: number): (b: number) => number;
    (a: bigint, b: bigint): bigint;
    (a: bigint): (b: bigint) => bigint;
}
Parameters
ParameterDescription
aThe first input number
bThe second input number

=> The result of a + b

Example 1

// Basic
add(1, 2) // 3

Example 2

// Bigint
add(1n, 2n) // 3n

Example 3

// Curry
const plus2(2)
plus2(-3) // -1

View source on GitHub


advance

Added from 1.7.0

Logic

Returns return value if argument is function; otherwise returns the value as it is.

Signature:

advance: <T>(val: T | AnyFn<any, T>) => T
Parameters
ParameterDescription
valInput any value

=> The result of typeof val === 'function' ? val(): val

Example

advance(1) // 1
advance(() => 1) // 1
Tests
import { assertEquals } from '../dev_deps.ts'
import { advance } from '../src/advance.ts'
import { assertEqual } from './asserts.ts'

Deno.test('advance', () => {
  const table: [unknown, unknown][] = [
    [1, 1],
    [0, 0],
    [() => 1, 1],
    [() => 0, 0]
  ]

  table.forEach(([val, expected]) => {
    assertEquals(advance(val), expected, `add(${val}) -> ${expected}`)
  })

  assertEqual<false>(advance(false))
  assertEqual<1>(advance(1))
  assertEqual<true>(advance(() => true))
  assertEqual<false>(advance(() => false))
})

View source on GitHub


and

Added from 1.1.0

🔗  or  xor 

Logic

Returns true if both arguments are true; otherwise false.

Signature:

and: <T, U>(a: T | AnyFn<any, T>, b: U | AnyFn<any, U>) => T extends FalsyLike ? false : U extends FalsyLike ? false : boolean
Parameters
ParameterDescription
aThe first input any value
bThe second input any value

=> The result of !!a && !!bb (if argument is function, return value)

Remark

If you pass a function as an argument, return value will evaluate.

Example

and(true, true) // true
and(false, true) // false
and(true, false) // false
and(false, false) // false
and(() => 1, () => 2) // true
and(() => 1, () => 0) // false
Tests
import { assertEquals } from '../dev_deps.ts'
import { and } from '../src/and.ts'
import { Falsy } from './../src/types/index.ts'
import { assertEqual } from './asserts.ts'

Deno.test('and', () => {
  const table: [unknown, unknown, boolean][] = [
    [true, true, true],
    [false, true, false],
    [true, false, false],
    [false, false, false],
    [() => true, true, true],
    [() => true, false, false],
    [() => true, () => false, false],
    [() => 1, () => 2, true],
    [() => 0, () => 1, false],
    [true, () => 1, true],
    [true, () => 0, false]
  ]

  table.forEach(([a, b, expected]) => {
    assertEquals(and(a, b), expected, `add(${a}, ${b}) -> ${expected}`)
  })

  assertEqual<false>(and(false as Falsy, false as Falsy))
  assertEqual<false>(and(false as Falsy, Boolean))
  assertEqual<false>(and(Boolean, false as Falsy))
  assertEqual<boolean>(and(Boolean, Boolean))
  assertEqual<false>(
    and(
      () => false as const,
      () => false
    )
  )
  assertEqual<false>(
    and(
      () => false,
      () => false as const
    )
  )
  assertEqual<false>(and(() => false as const, 1))
  assertEqual<false>(and(() => false as const, 1 as const))
})

View source on GitHub


append

Added from 1.2.0

🔗  prepend 

Array

Returns a new list containing the contents of the given list, followed by the given value.

Signature:

append: <T, U>(val: T, list: U[]) => (T | U)[]
Parameters
ParameterDescription
valThe value to add to the end of the new list
listThe list of elements to add a new item to

=> The result of [...list, val]

Example

append('Tom', ['hello']) // ['hello', 'Tom']
append('Tom', []) // ['Tom']
append(['Tom'], ['hello', 'world']) // ['hello', 'world', ['Tom']]
Tests
import { assertEquals } from '../dev_deps.ts'
import { append } from '../src/append.ts'

Deno.test('append', () => {
  const table: [unknown, unknown[], unknown[]][] = [
    [null, [], [null]],
    [undefined, [], [undefined]],
    ['', [], ['']],
    [{}, [], [{}]],
    [0, [], [0]],
    ['a', ['b'], ['b', 'a']],
    ['a', ['b', 'c', 'd'], ['b', 'c', 'd', 'a']],
    [[], [], [[]]],
    [[1], [], [[1]]],
    [[1], [2], [2, [1]]],
    [[1], [2, 3, 4], [2, 3, 4, [1]]]
  ]

  table.forEach(([val, list, expected]) => {
    assertEquals(
      append(val, list),
      expected,
      `append(${val}, ${list}) -> ${expected}`
    )
  })
})

View source on GitHub


chunk

Added from 1.4.0

Array

Return an array of elements split into groups the length of size.

Signature:

chunk: <T extends number, U extends readonly unknown[]>(size: T, array: U) => T extends 0 ? U : `${T}` extends `-${number}` ? U : U extends readonly [
] ? U : U extends readonly (infer R)[] ? R[][] : never
Parameters
ParameterDescription
sizeThe length of each chunk
arrayThe array to process

=> Returns the new array of chunks

Remark

If array can't be split evenly, the final chunk will be the remaining elements.

Example 1

// Basic
chunk(1, ['a', 'b', 'c', 'd']) // [['a'], ['b'], ['c'], ['d']]
chunk(3, ['a', 'b', 'c', 'd']) // [['a', 'b', 'c'], ['d']]
chunk(5, ['a', 'b', 'c', 'd']) // [['a', 'b', 'c', 'd']]

Example 2

// Illegal size
chunk(0, ['a', 'b', 'c']) // ['a', 'b', 'c']
chunk(-3, ['a', 'b', 'c']) // ['a', 'b', 'c']
chunk(5, []) // []
Tests
import { assertEquals } from '../dev_deps.ts'
import { chunk } from '../src/chunk.ts'
import { assertEqual } from './asserts.ts'

Deno.test('chunk', () => {
  const arr = ['a', 'b', 'c', 'd']
  const arr2 = [{}, [], 0, null, undefined]
  const table: [number, unknown[], unknown[][] | unknown[]][] = [
    [1, [], []],
    [0, [], []],
    [2, [], []],
    [1, [''], [['']]],
    [2, [''], [['']]],
    [3, [''], [['']]],
    [4, [''], [['']]],
    [0, [''], ['']],
    [-0, [''], ['']],
    [-1, [''], ['']],
    [-2, [''], ['']],
    [-3, [''], ['']],
    [-4, [''], ['']],
    [1, arr, [['a'], ['b'], ['c'], ['d']]],
    [
      2,
      arr,
      [
        ['a', 'b'],
        ['c', 'd']
      ]
    ],
    [3, arr, [['a', 'b', 'c'], ['d']]],
    [4, arr, [['a', 'b', 'c', 'd']]],
    [5, arr, [['a', 'b', 'c', 'd']]],
    [6, arr, [['a', 'b', 'c', 'd']]],
    [1, arr2, [[{}], [[]], [0], [null], [undefined]]],
    [2, arr2, [[{}, []], [0, null], [undefined]]],
    [
      3,
      arr2,
      [
        [{}, [], 0],
        [null, undefined]
      ]
    ],
    [4, arr2, [[{}, [], 0, null], [undefined]]],
    [5, arr2, [[{}, [], 0, null, undefined]]],
    [6, arr2, [[{}, [], 0, null, undefined]]]
  ]

  table.forEach(([a, b, expected]) => {
    assertEquals(chunk(a, b), expected, `chunk(${a}, ${b}) -> ${expected}`)
  })

  assertEqual<never[]>(chunk(0, []))
  assertEqual<[]>(chunk(0, [] as []))
  assertEqual<readonly []>(chunk(0, [] as const))
  assertEqual<[]>(chunk(1, [] as []))
  assertEqual<string[]>(chunk(-1, ['']))
  assertEqual<string[]>(chunk(-0, ['']))
  assertEqual<string[]>(chunk(0, ['']))
  assertEqual<string[][]>(chunk(1, ['']))
  assertEqual<string[][]>(chunk(2, ['']))
  assertEqual<string[][]>(chunk(3, ['']))
  assertEqual<string[][]>(chunk(4, ['']))
  assertEqual<''[][]>(chunk(1, [''] as const))
  assertEqual<readonly ['a', 'b', 'c', 'd']>(
    chunk(-4, ['a', 'b', 'c', 'd'] as const)
  )
  assertEqual<readonly ['a', 'b', 'c', 'd']>(
    chunk(0, ['a', 'b', 'c', 'd'] as const)
  )
  // TODO: Implement more rigorous type inference
  // assertEqual<[['a'], ['b'], ['c'], ['d']]>(
  //   chunk(1, ['a', 'b', 'c', 'd'] as ['a', 'b', 'c', 'd'])
  // )
  // assertEqual<[['a', 'b'], ['c', 'd']]>(
  //   chunk(2, ['a', 'b', 'c', 'd'] as ['a', 'b', 'c', 'd'])
  // )
  // assertEqual<[['a', 'b', 'c'], ['d']]>(
  //   chunk(3, ['a', 'b', 'c', 'd'] as ['a', 'b', 'c', 'd'])
  // )
  // assertEqual<[['a', 'b', 'c', 'd']]>(
  //   chunk(4, ['a', 'b', 'c', 'd'] as ['a', 'b', 'c', 'd'])
  // )
})

View source on GitHub


constructorName

Added from 1.8.0

Safe getter for constructor.name.

Signature:

constructorName: (val: unknown) => string
Parameters
ParameterDescription
valAny value

=> If val is null or undefined , empty string; otherwise constructor.name

Example

constructorName(null) // ''
constructorName(undefined) // ''
constructorName({}) // 'Object'
constructorName('') // 'String'
Tests
import { assertEquals, isSymbol } from '../dev_deps.ts'
import { constructorName } from '../src/constructorName.ts'

Deno.test('constructorName', () => {
  function fn() {
    true
  }
  class Class {}
  const table: [unknown, string][] = [
    [Object, 'Function'],
    [new Object(), 'Object'],
    [Object(), 'Object'],
    [{}, 'Object'],
    [{ hoge: 'huga' }, 'Object'],
    [new Number(), 'Number'],
    [Number(), 'Number'],
    [Number, 'Function'],
    [1, 'Number'],
    ['', 'String'],
    [String(), 'String'],
    [String, 'Function'],
    [new String(), 'String'],
    [new Boolean(), 'Boolean'],
    [true, 'Boolean'],
    [Boolean, 'Function'],
    [new Boolean(), 'Boolean'],
    [Boolean(), 'Boolean'],
    [undefined, ''],
    [null, ''],
    [Symbol(), 'Symbol'],
    [Symbol, 'Function'],
    [1n, 'BigInt'],
    [BigInt, 'Function'],
    [Array, 'Function'],
    [Array([]), 'Array'],
    [new Array([]), 'Array'],
    [[], 'Array'],
    [Date, 'Function'],
    [new Date(0), 'Date'],
    [Date(), 'String'],
    [/a/, 'RegExp'],
    [RegExp, 'Function'],
    [new RegExp(''), 'RegExp'],
    [Error, 'Function'],
    [Error(''), 'Error'],
    [new Error(), 'Error'],
    [TypeError, 'Function'],
    [TypeError(), 'TypeError'],
    [new TypeError(), 'TypeError'],
    [Set, 'Function'],
    [new Set(), 'Set'],
    [Map, 'Function'],
    [new Map(), 'Map'],
    [() => true, 'Function'],
    [fn, 'Function'],
    [Class, 'Function'],
    [new Class(), 'Class']
  ]
  table.forEach(([val, expected]) => {
    assertEquals(
      constructorName(val),
      expected,
      `constructorName(${isSymbol(val) ? 'symbol' : val}) -> ${expected}`
    )
  })
})

View source on GitHub


curry

Added from 1.9.0

beta

Creates a function that accepts arguments of fn and either invokes fn returning its result, if at least arity number of arguments have been provided, or returns a function that accepts the remaining fn arguments, and so on.

WARNING

This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.

Signature:

curry: <T extends unknown[], R>(fn: (...args: T) => R) => T["length"] extends 0 ? () => R : Curried<T, R>
Parameters
ParameterDescription
fnThe function to curry

=> The new curried function

Remark

Maximum number of arity is 19. Beyond that, the type system will breaks.

Example

const replace = (from: string, to: string, val: string) => val.replace(from, to)
const curriedReplace = curry(replace)
const curriedReplace('hello', 'hi', 'hello world') // 'hi world'
const curriedReplace('hello')('hi', 'hello world') // 'hi world'
const curriedReplace('hello')('hi')('hello world') // 'hi world'

View source on GitHub


dec

Added from 1.1.0

🔗  inc 

Decrements its argument.

Signature:

dec: {
    (val: number): number;
    (val: bigint): bigint;
}
Parameters
ParameterDescription
valinput number or bigint

=> Decremented val

Example

dec(100) // 99
dec(10n) // 9n
Tests
import { assertEquals } from '../dev_deps.ts'
import { dec } from '../src/dec.ts'
import { assertEqual } from './asserts.ts'

Deno.test('dec', () => {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  const table: [number | bigint | any, number | bigint][] = [
    [0, -1],
    [-10, -11],
    [10, 9],
    [0n, -1n],
    [-10n, -11n],
    [10n, 9n]
  ]
  table.forEach(([val, expected]) => {
    assertEquals(dec(val), expected, `dec(${val}) -> ${expected}`)
  })

  assertEqual<number>(dec(1 as const))
  assertEqual<number>(dec(1))
  assertEqual<bigint>(dec(1n as const))
  assertEqual<bigint>(dec(1n))
})

View source on GitHub


defaultTo

Added from 1.4.0

Returns the second argument if it is not null, undefined or NaN; otherwise the first argument is returned.

Signature:

defaultTo: <T extends unknown>(a: T) => <U extends unknown>(b: U) => U extends null | undefined ? T : U extends number ? U : T | U
Parameters
ParameterDescription
aa will be returned instead of default

=> Returns a function that stores the default a value. The function accept b argument. if b is null , undefined or NaN , return a ; otherwise return b

Example

const defaultVal = defaultTo('anonymous')
defaultVal(undefined) // 'anonymous'
defaultVal(null) // 'anonymous'
defaultVal(NaN) // 'anonymous'

defaultVal('Tom') // 'Tom'
Tests
import { assertEquals } from '../dev_deps.ts'
import { defaultTo } from '../src/defaultTo.ts'
import { assertEqual } from './asserts.ts'

Deno.test('defaultTo', () => {
  const defaultValue = 'hello'
  const table: [unknown, unknown, unknown][] = [
    [defaultValue, '', ''],
    [defaultValue, 'world', 'world'],
    [defaultValue, undefined, defaultValue],
    [defaultValue, null, defaultValue],
    [defaultValue, NaN, defaultValue],
    [defaultValue, NaN, defaultValue],
    [defaultValue, 0, 0],
    [defaultValue, {}, {}],
    [defaultValue, [], []]
  ]
  table.forEach(([a, b, expected]) => {
    assertEquals(
      defaultTo(a)(b),
      expected,
      `defaultTo(${a}, ${b}) -> ${expected}`
    )
  })

  assertEqual<string>(defaultTo('')(undefined as undefined))
  assertEqual<string>(defaultTo('')(null as null))
  assertEqual<''>(defaultTo('' as const)(undefined))
  assertEqual<''>(defaultTo('' as const)(null))
  assertEqual<string | number>(defaultTo('')(100))
  assertEqual<string | number>(defaultTo('')(NaN))
  assertEqual<'' | number>(defaultTo('' as const)(NaN))
})

View source on GitHub


divide

Added from 1.0.0

🔗  add  subtract  multiply 

Math

Divide its second argument from its first argument.

Signature:

divide: {
    (a: number, b: number): number;
    (a: number): (b: number) => number;
    (a: bigint, b: bigint): bigint;
    (a: bigint): (b: bigint) => bigint;
} & {
    (a: typeof _, b: number): (a: number) => number;
    (a: typeof _, b: bigint): (a: bigint) => bigint;
}
Parameters
ParameterDescription
aThe first input number
bThe second input number

=> The result of a / b

Remark

Since division is not idempotent, there are two ways to curry.

Example 1

// Number
divide(10, 100) // 0.1

Example 2

// Bigint
divide(1n, 2n) // 3n

Example 3

// First argument curry
const reciprocal = divide(1)
reciprocal(4) // 0.25

Example 4

// Second argument curry
import { _ } from 'fonction'
const half = divide(_, 2)
half(20) // 10

View source on GitHub


equal

Added from 1.7.0

Returns true if its arguments are equivalent, false otherwise. Handles cyclical data structures.

Signature:

equal: <T, U extends T>(a: T, b: U) => boolean
Parameters
ParameterDescription
aInput any value
bInput any value

=> Return true if the reference memory is the same or the property members and their values are the same

Example

equals(-0, 0) // true
equals(NaN, NaN) // true
equals([[[[]]]], [[[[]]]]) // true
equals({ a: { b: [1, 2, 3]}}, { a: { b: [1, 2, 3]}}) // true

View source on GitHub


F

Added from 1.1.0

🔗  T 

A function that always returns false. Any passed in parameters are ignored.

Signature:

F: AnyFn<unknown, false>
Parameters

=> false

Example

F() // false
F(1, 'hello', 'world') // false
Tests
import { assertEquals } from '../dev_deps.ts'
import { F } from '../src/F.ts'
import { assertReturnType } from './asserts.ts'

Deno.test('F', () => {
  const table: unknown[] = [
    [''],
    [1, 2, 3],
    [{}, [], undefined, null],
    [undefined],
    [null]
  ]
  table.forEach((val) => {
    assertEquals(F(val), false, `F(${val}) -> false`)
  })

  assertReturnType<false>(F)
})

View source on GitHub


flattenDeep

Added from 1.5.0

Recursively flattens array.

Signature:

flattenDeep: <T extends readonly unknown[]>(val: T) => FlattenDeep<T>
Parameters
ParameterDescription
valThe array to flatten

=> The result of val.flat(Infinity)

Example

flattenDeep([]) // []
flattenDeep([1, [2, [3, [4]], 5]]) // [1, 2, 3, 4, 5]
Tests
import { assertEquals } from '../dev_deps.ts'
import { FlattenDeep as Flat, flattenDeep } from '../src/flattenDeep.ts'
import { assertEqual } from './asserts.ts'
Deno.test('flattenDeep', () => {
  const table: [unknown[], unknown[]][] = [
    [[], []],
    [[null], [null]],
    [
      [null, undefined, 1, 'hello'],
      [null, undefined, 1, 'hello']
    ],
    [[[]], []],
    [[[1]], [1]],
    [[[1, 2, 3]], [1, 2, 3]],
    [[[1, 2, 3, undefined, null]], [1, 2, 3, undefined, null]],
    [
      [[1, 2, 3, undefined, null], 4, 5, 6],
      [1, 2, 3, undefined, null, 4, 5, 6]
    ],
    [
      [
        [1, 2, 3, undefined, null],
        [4, 5, 6]
      ],
      [1, 2, 3, undefined, null, 4, 5, 6]
    ],
    [
      [[1, 2, 3, undefined, ['hello', 4], null]],
      [1, 2, 3, undefined, 'hello', 4, null]
    ],
    [
      [
        '',
        [1, 2, 3, undefined, ['hello', 4], null],
        [5, 6, ['world']],
        [[7, 8], 9]
      ],
      ['', 1, 2, 3, undefined, 'hello', 4, null, 5, 6, 'world', 7, 8, 9]
    ]
  ]
  table.forEach(([val, expected]) => {
    assertEquals(
      flattenDeep(val),
      expected,
      `flattenDeep(${val}) -> ${expected}`
    )
  })
})

Deno.test('types', () => {
  assertEqual<[], Flat<[]>>()
  assertEqual<never[], Flat<never[]>>()
  // TODO: Success this case
  // assertEqual<never[], Flat<never[][]>>()
  assertEqual<[''], Flat<['']>>()
  assertEqual<[number], Flat<[number]>>()
  assertEqual<[number, string], Flat<[number, string]>>()
  assertEqual<number[], Flat<number[]>>()
  assertEqual<(number | string)[], Flat<(number | string)[]>>()
  assertEqual<readonly [''], Flat<readonly ['']>>()
  assertEqual<readonly ['', 'hello'], Flat<readonly ['', 'hello']>>()
  assertEqual<readonly ['', ''], Flat<readonly ['', readonly ['']]>>()
  assertEqual<
    readonly ['', '', 'hello'],
    Flat<readonly ['', readonly ['', readonly ['hello']]]>
  >()
})

View source on GitHub


gt

Added from 1.1.0

🔗  gte  lt  lte 

Returns true if the first argument is greater than the second; otherwise false.

Signature:

gt: <T extends Ord>(a: T, b: T) => boolean
Parameters
ParameterDescription
aThe first input value
bThe second input value

=> The result of a > b

Example 1

// Number
gt(2, 1) // true
gt(2, 2) // false

Example 2

// Bigint
gt(2n, 1n) // true
gt(2n, 2n) // false

Example 3

// String
gt('z', 'a') // true
gt('a', 'z') // false

Example 4

// Boolean
gt(true, false) // true
gt(false, true) // false
gt(true, true) // false
gt(false, false) // false

Example 5

// Date
gt(new Date('2000/1/2'), new Date('2000/1/1')) // true
gt(new Date('1999/12/31'), new Date('2000/1/1')) // false
gt(new Date('2000/1/1'), new Date('2000/1/1')) // false
Tests
import { assertEquals } from '../dev_deps.ts'
import { gt } from '../src/gt.ts'
import { Ord } from '../src/types/index.ts'

Deno.test('gt', () => {
  const table: [Ord, Ord, boolean][] = [
    [0, 0, false],
    [0, 1, false],
    [1, 0, true],
    [0n, 0n, false],
    [0n, 1n, false],
    [1n, 0n, true],
    ['a', 'a', false],
    ['a', 'z', false],
    ['z', 'a', true],
    ['za', 'a', true],
    [true, true, false],
    [false, true, false],
    [false, false, false],
    [true, false, true],
    [new Date('2000/1/1 00:00:00'), new Date('2000/1/1 00:00:00'), false],
    [new Date('1999/12/31'), new Date('2000/1/1'), false],
    [new Date('2000/1/2'), new Date('2000/1/1'), true]
  ]
  table.forEach(([a, b, expected]) => {
    assertEquals(gt(a, b), expected, `gt(${a}, ${b}) -> ${expected}`)
  })
})

View source on GitHub


gte

Added from 1.1.0

🔗  gt  lt  lte 

Returns true if the first argument is greater than or equal to the second; otherwise false.

Signature:

gte: <T extends Ord>(a: T, b: T) => boolean
Parameters
ParameterDescription
aThe first input value
bThe second input value

=> The result of a >= b

Example 1

// Number
gte(2, 1) // true
gte(2, 2) // true
gte(2, 3) // false

Example 2

// Bigint
gte(2n, 1n) // true
gte(2n, 2n) // true
gte(2n, 3n) // false

Example 3

// String
gte('z', 'a') // true
gte('a', 'a') // true
gte('a', 'z') // false

Example 4

// Boolean
gte(true, false) // true
gte(true, true) // true
gte(false, false) // true
gte(false, true) // false

Example 5

// Date
gte(new Date('2000/1/2'), new Date('2000/1/1')) // true
gte(new Date('2000/1/1'), new Date('2000/1/1')) // true
gte(new Date('1999/12/31'), new Date('2000/1/1')) // false
Tests
import { assertEquals } from '../dev_deps.ts'
import { gte } from '../src/gte.ts'
import { Ord } from '../src/types/index.ts'

Deno.test('gte', () => {
  const table: [Ord, Ord, boolean][] = [
    [0, 1, false],
    [0, 0, true],
    [1, 0, true],
    [0n, 1n, false],
    [0n, 0n, true],
    [1n, 0n, true],
    ['a', 'z', false],
    ['a', 'a', true],
    ['z', 'a', true],
    ['za', 'a', true],
    [false, true, false],
    [false, false, true],
    [true, true, true],
    [true, false, true],
    [new Date('1999/12/31'), new Date('2000/1/1'), false],
    [new Date('2000/1/1 00:00:00'), new Date('2000/1/1 00:00:00'), true],
    [new Date('2000/1/2'), new Date('2000/1/1'), true]
  ]

  table.forEach(([a, b, expected]) => {
    assertEquals(gte(a, b), expected, `gte(${a}, ${b}) -> ${expected}`)
  })
})

View source on GitHub


has

Added from 1.2.0

🔗  props 

Object

Returns whether or not an object has an own property with the specified name.

Signature:

has: <T extends string | number | (string | number)[], U extends Record<PropertyKey, unknown>>(props: T, obj: U) => T extends unknown[] ? boolean : T extends string | number ? U extends Record<T, unknown> ? true : false : never
Parameters
ParameterDescription
propsThe name of the property to check for
objThe check object

=> The result of Object.prototype.hasOwnProperty

Example 1

// Flat
has('hello', { hello: 'world' }) // true
has(0, { 0 : 1}) // true
has('', {}) // false
has('hello', { hi : hello: 'world' }) // false

Example 2

// Nest
hasPath(['hello'], { hello: 'world' }) // true
hasPath([0], { 0: 1 }) // true
hasPath(['hello', 'world'], { hello: { world: '' } } // true

hasPath(['hi'], { hello: '' } ) // false
hasPath(['hi', 'Tom'], { hi: { John: 1 } } ) // false
Tests
import { assertEquals } from '../dev_deps.ts'
import { has } from '../src/has.ts'

Deno.test('has', () => {
  const tablePrimitive: [
    string | number,
    Record<PropertyKey, unknown>,
    boolean
  ][] = [
    ['', {}, false],
    ['', { ' ': '' }, false],
    ['', { ' ': { '': '' } }, false],
    [0, {}, false],
    [0, { 1: '' }, false],
    [0, { 1: { 0: '' } }, false],
    ['', { '': '' }, true],
    ['Hello', { hello: '' }, false],
    ['Hello', { Hello: '' }, true],
    ['hello', { hello: '' }, true],
    [0, { 0: 1 }, true]
  ]

  tablePrimitive.forEach(([a, b, expected]) => {
    assertEquals(has(a, b), expected, `has(${a}, ${b}) -> ${expected}`)
  })

  const tableArray: [
    (string | number)[],
    Record<PropertyKey, unknown>,
    boolean
  ][] = [
    [[], {}, false],
    [[], { '': '' }, false],
    [[0], {}, false],
    [[0], { '': '' }, false],
    [[0], { 0: '' }, true],
    [[0, 0], { 0: { 0: 1 } }, true],
    [[0, 'a'], { 0: { 0: 'b' } }, false],
    [[0, 'a'], { 0: { a: 'b' } }, true],
    [[''], {}, false],
    [[''], { ' ': '' }, false],
    [['a', 'b'], { a: '' }, false],
    [['a', 'b'], { a: { c: '' } }, false],
    [
      ['a', 'b'],
      {
        a: {
          a: ''
        }
      },
      false
    ],
    [['a'], { a: '' }, true],
    [['a'], { a: {} }, true],
    [['a', 'b'], { a: { b: '' } }, true],
    [['a', 'b'], { a: { b: {} } }, true],
    [['a', 'b', 'c'], { a: { b: { c: '' } } }, true],
    [[0, 'a', 'B'], { 0: { a: { B: 'c' } } }, true]
  ]
  tableArray.forEach(([a, b, expected]) => {
    assertEquals(has(a, b), expected, `has(${a}, ${b}) -> ${expected}`)
  })
})

View source on GitHub


Added from 1.2.0

🔗  Last 

ArrayString

Infer the head types.

Signature:

head: <T extends string | readonly unknown[]>(val: T) => Head<T>
Parameters

=> Head element of the T

Example 1

// String
head<''> // ''
head<'hello'> // 'hello'

Example 2

// Array
head<[]> // undefined
head<['hello', 'world']> // 'hello'

View source on GitHub


identity

Added from 1.2.0

Return the parameter supplied to it.

Signature:

identity: <T>(val: T) => T
Parameters
ParameterDescription
valThe value to return

=> The result of val

Example

identity(1) // 1
identity({}) // {}
Tests
import { assertEquals } from '../dev_deps.ts'
import { identity } from '../src/identity.ts'

Deno.test('identity', () => {
  const table: [unknown, unknown][] = [
    ['', ''],
    [0, 0],
    [1n, 1n],
    [() => 1, () => 1],
    [{}, {}],
    [[], []]
  ]
  table.forEach(([val]) => {
    assertEquals(identity(val), val, `identity(${val}) -> ${val}`)
  })
})

View source on GitHub


ifElse

Added from 1.6.0

🔗  ifElseFn 

Logic

Return the onTrue or the onFalse value depending upon the result of the condition val.

Signature:

ifElse: <V, T, F>(val: V | AnyFn<any, V>, onTrue: T | AnyFn<any, T>, onFalse: F | AnyFn<any, F>) => V extends FalsyLike ? F : V extends true ? T : T | F
Parameters
ParameterDescription
valA predicate value
onTrueThe val evaluates to a truthy value
onFalseThe val evaluates to a falsy value

=> The result of !!val ? onTrue : onFalse (if argument is function, return value)

Remark

If you pass a function as an argument, return value will evaluate.

Example

ifElse(true, 1, 0) // 1
ifElse(false, 1, 0) // 0
ifElse(undefined, 1, 0) // 0
ifElse(() => true, () => 1, () => 0) // 1
Tests

import { assertEquals, length, spy } from '../dev_deps.ts'
import { ifElse } from '../src/ifElse.ts'
import { T } from '../src/T.ts'
import { assertEqual } from './asserts.ts'
Deno.test('ifElse', () => {
  const table: [unknown, unknown, unknown, unknown][] = [
    [true, 1, 0, 1],
    [{}, 1, 0, 1],
    [[], 1, 0, 1],
    ['hello', 1, 0, 1],
    [false, 1, 0, 0],
    ['', 1, 0, 0],
    [NaN, 1, 0, 0],
    [undefined, 1, 0, 0],
    [null, 1, 0, 0],
    [0, 1, 0, 0],
    [() => true, 1, 0, 1],
    [() => false, 1, 0, 0],
    [() => 1, 1, 0, 1],
    [() => 0, 1, 0, 0]
  ]

  table.forEach(([val, onTrue, onFalse, expected]) => {
    assertEquals(
      ifElse(val, onTrue, onFalse),
      expected,
      `ifElse(${val}, ${onTrue}, ${onFalse}) -> ${expected}`
    )
  })

  assertEqual<number>(ifElse(true, 1, 0))
  assertEqual<number>(ifElse(true as const, 1, 0))
  assertEqual<1>(ifElse(true as const, 1 as const, 0))
  assertEqual<0>(ifElse(false as const, 1 as const, 0 as const))
  assertEqual<1 | ''>(ifElse(false, 1 as const, '' as const))
  assertEqual<1 | ''>(ifElse({}, 1 as const, '' as const))
  assertEqual<1>(ifElse(() => true as const, 1 as const, '' as const))
  assertEqual<''>(ifElse(() => false as const, 1 as const, '' as const))
  assertEqual<1 | ''>(ifElse(() => 1, 1 as const, '' as const))
})

Deno.test('ifElse:iife', () => {
  const table: [unknown, boolean, boolean, number][] = [
    [true, true, true, 2],
    [true, false, true, 1],
    [true, true, false, 1],
    [false, true, true, 2],
    [false, false, true, 1],
    [false, true, false, 1]
  ]

  table.forEach(([val, onTrue, onFalse, expected]) => {
    const fn = spy()
    ifElse(val, onTrue ? fn() : T, onFalse ? fn() : T)
    assertEquals(
      length(fn.calls),
      expected,
      `ifElse(${val}, fn(), fn()) -> fn.calls.length:${expected}`
    )
  })
})

Deno.test('ifElse:fn', () => {
  const table: [unknown, unknown][] = [
    [true, true],
    [false, undefined]
  ]

  table.forEach(([val, expected]) => {
    const onTrue = spy(() => true)
    ifElse(val, onTrue, T)

    assertEquals(
      onTrue.calls[0]?.returned,
      expected,
      `ifElse(${val}, fn, fn) -> fn.calls[0].returned:${expected}`
    )
  })
})

View source on GitHub


ifElseFn

Added from 1.6.0

🔗  ifElse 

Logic

Creates a function that will process either the onTrue or the onFalse function depending upon the result of the condition predicate.

Signature:

ifElseFn: <V, R, T, F>(condition: (val: V) => R, onTrue: T | ((val: V) => T), onFalse: F | ((val: V) => F)) => (val: V) => R extends true ? T : R extends FalsyLike ? F : T | F
Parameters
ParameterDescription
conditionA predicate function
onTrueAny value or A function to invoke when the condition evaluates to a truthy value
onFalseAny value or A function to invoke when the condition evaluates to a falsy value

=> A new function that will process either the onTrue or the onFalse function depending upon the result of the condition predicate

Example

ifElseFn((x: number) => x > 10, 'big', 'small')(20) // 'big'
const fn = ifElseFn((x: number) => x > 10, (x) => x + 1, (x) => x - 1)
fn(11) // 12
fn(9) // 8
Tests
import { assertEquals } from '../dev_deps.ts'
import { ifElseFn } from '../src/ifElseFn.ts'
import { assertEqual } from './asserts.ts'

Deno.test('ifElseFn', () => {
  const table: [
    (val: any) => boolean,
    ((val: unknown) => boolean) | unknown,
    ((val: unknown) => boolean) | unknown,
    unknown,
    unknown
  ][] = [
    [() => true, 1, 0, 1, 1],
    [() => false, 1, 0, 1, 0],
    [(x: unknown) => !!x, 1, 0, 1, 1],
    [(x: unknown) => !!x, 1, 0, 0, 0],
    [(x: unknown) => !!x, () => 2, 0, 1, 2],
    [(x: unknown) => !!x, () => 2, () => 3, 0, 3],
    [(x: unknown) => !!x, (x: number) => x + 1, () => 3, 1, 2],
    [(x: unknown) => !!x, (x: number) => x + 1, (x: number) => x - 1, 0, -1]
  ]

  table.forEach(([condition, onTrue, onFalse, val, expected]) => {
    assertEquals(
      ifElseFn(condition, onTrue, onFalse)(val),
      expected,
      `ifElseFn(${condition}, ${onTrue}, ${onFalse})(${val}) -> ${expected}`
    )
  })

  assertEqual<number>(ifElseFn(() => true as const, 1, 0)(1))
  assertEqual<1>(ifElseFn(() => true as const, 1 as const, 0 as const)(1))
  assertEqual<0>(ifElseFn(() => false as const, 1 as const, 0 as const)(1))
  assertEqual<0>(ifElseFn(() => '' as const, 1 as const, 0 as const)(1))
  assertEqual<0>(ifElseFn(() => undefined, 1 as const, 0 as const)(1))
  assertEqual<0>(ifElseFn(() => null, 1 as const, 0 as const)(1))
  assertEqual<0>(ifElseFn(() => 0 as const, 1 as const, 0 as const)(1))
  assertEqual<1 | 0>(ifElseFn(() => true as boolean, 1 as const, 0 as const)(1))
  assertEqual<1 | 0>(ifElseFn((x: string) => !!x, 1 as const, 0 as const)(''))
  assertEqual<number>(
    ifElseFn(
      (x: number) => !!x,
      (x: number) => x + 1,
      0 as const
    )(0)
  )
  assertEqual<number>(
    ifElseFn(
      (x: number) => !!x,
      (x: number) => x + 1,
      (x: number) => x - 1
    )(0)
  )
})

View source on GitHub


inc

Added from 1.1.0

🔗  dec 

Increments its argument.

Signature:

inc: {
    (val: number): number;
    (val: bigint): bigint;
}
Parameters
ParameterDescription
valInput number or bigint

=> Incremented val

Example

inc(100) // 101
inc(10n) // 11n
Tests
import { assertEquals } from '../dev_deps.ts'
import { inc } from '../src/inc.ts'
import { assertEqual } from './asserts.ts'
Deno.test('inc', () => {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  const table: [number | bigint | any, number | bigint][] = [
    [0, 1],
    [-10, -9],
    [10, 11],
    [0n, 1n],
    [-10n, -9n],
    [10n, 11n]
  ]
  table.forEach(([val, expected]) => {
    assertEquals(inc(val), expected, `inc(${val}) -> ${expected}`)
  })

  assertEqual<number>(inc(1 as const))
  assertEqual<number>(inc(1))
  assertEqual<bigint>(inc(1n as const))
  assertEqual<bigint>(inc(1n))
})

View source on GitHub


init

Added from 2.0.0

🔗  tail 

ArrayString

Returns all but the init element of the given list or string.

Signature:

init: <T extends string | readonly unknown[]>(val: T) => Init<T>
Parameters
ParameterDescription
valstring or any array object

=> The result of val.slice(0, -1)

Remark

The maximum number of characters for the type system to work properly is 16.

Example 1

// String
init('hello') // 'hell'
init('h') // ''
init('') // ''

Example 2

init([1, 2, 3]) // [1, 2]
init(['hello', 'world']) // ['hello']
init(['hello']) // []
init([]) // []

View source on GitHub


K

Added from 1.1.0

K combinator. Returns a function that always returns the given value.

Signature:

K: <T extends unknown>(val: T) => () => T
Parameters
ParameterDescription
valThe value to wrap in a function

=> Function wrapped val

Example

const k = K('k')
k() // 'k'
Tests
import { assertEquals } from '../dev_deps.ts'
import { K } from '../src/K.ts'

Deno.test('K', () => {
  const table: [unknown][] = [
    [''],
    [{}],
    ['hello'],
    [() => 1],
    [[]],
    [undefined],
    [null]
  ]

  table.forEach(([val]) => {
    assertEquals(K(val)(), val, `K(${val})() -> ${val}`)
  })
})

View source on GitHub


last

Added from 1.0.0

🔗  head 

Returns the last element of the given list or string.

Signature:

last: <T extends string | readonly unknown[]>(val: T) => Last<T>
Parameters
ParameterDescription
valstring or any array object

=> The last element of the val

Remark

The maximum number of characters for the type system to work properly is 24.

Example 1

// String
last('') // ''
last('hello') // 'o'

Example 2

// Array
last('hello', 'new', 'world') // 'world'
last([]) // undefined
last(['one', 2, 3, 4]) // 4

View source on GitHub


lt

Added from 1.1.0

🔗  lte  gt  gte 

Returns true if the first argument is less than the second; otherwise false.

Signature:

lt: <T extends Ord>(a: T, b: T) => boolean
Parameters
ParameterDescription
aThe first input value
bThe second input value

=> The result of a < b

Example 1

// Number
lt(1, 2) // true
lt(2, 2) // false

Example 2

// Bigint
lt(1n, 2n) // true
lt(2n, 2n) // false

Example 3

// String
lt('a', 'z') // true
lt('a', 'a') // false

Example 4

// Boolean
lt(false, true) // true
lt(true, true) // false
lt(false, false) // false
lt(true, false) // false

Example 5

// Date
lt(new Date('1999/12/31'), new Date('2000/1/1')) // true
lt(new Date('2000/1/1'), new Date('2000/1/1')) // false
lt(new Date('2000/1/2'), new Date('2000/1/1')) // false
Tests
import { assertEquals } from '../dev_deps.ts'
import { lt } from '../src/lt.ts'
import { Ord } from '../src/types/index.ts'

Deno.test('lt', () => {
  const table: [Ord, Ord, boolean][] = [
    [0, 0, false],
    [1, 0, false],
    [0, 1, true],
    [0n, 0n, false],
    [1n, 0n, false],
    [0n, 1n, true],
    ['a', 'a', false],
    ['z', 'a', false],
    ['za', 'a', false],
    ['a', 'z', true],
    [true, true, false],
    [false, false, false],
    [true, false, false],
    [false, true, true],
    [new Date('2000/1/1 00:00:00'), new Date('2000/1/1 00:00:00'), false],
    [new Date('2000/1/2'), new Date('2000/1/1'), false],
    [new Date('1999/12/31'), new Date('2000/1/1'), true]
  ]
  table.forEach(([a, b, expected]) => {
    assertEquals(lt(a, b), expected, `lt(${a}, ${b}) -> ${expected}`)
  })
})

View source on GitHub


lte

Added from 1.1.0

🔗  lt  gt  gte 

Returns true if the first argument is less than or equal to the second; otherwise false.

Signature:

lte: <T extends Ord>(a: T, b: T) => boolean
Parameters
ParameterDescription
aThe first input value
bThe second input value

=> The result of a <= b

Example 1

// Number
lte(1, 2) // true
lte(2, 2) // true
lte(2, 1) // false

Example 2

// Bigint
lte(1n, 2n) // true
lte(2n, 2n) // true
lte(2n, 1n) // true

Example 3

// String
lte('a', 'z') // true
lte('a', 'a') // true
lte('z', 'a') // false

Example 4

// Boolean
lte(true, true) // true
lte(false, false) // true
lte(false, true) // true
lte(true, false) // false

Example 5

// Date
lte(new Date('2000/1/1'), new Date('2000/1/1')) // true
lte(new Date('1999/12/31'), new Date('2000/1/1')) // true
lte(new Date('2000/1/2'), new Date('2000/1/1')) // false
Tests
import { assertEquals } from '../dev_deps.ts'
import { lte } from '../src/lte.ts'
import { Ord } from '../src/types/index.ts'

Deno.test('lte', () => {
  const table: [Ord, Ord, boolean][] = [
    [1, 0, false],
    [0, 0, true],
    [0, 1, true],
    [1n, 0n, false],
    [0n, 0n, true],
    [0n, 1n, true],
    ['z', 'a', false],
    ['za', 'a', false],
    ['a', 'a', true],
    ['a', 'z', true],
    [true, false, false],
    [true, true, true],
    [false, false, true],
    [false, true, true],
    [new Date('2000/1/2'), new Date('2000/1/1'), false],
    [new Date('2000/1/1 00:00:00'), new Date('2000/1/1 00:00:00'), true],
    [new Date('1999/12/31'), new Date('2000/1/1'), true]
  ]
  table.forEach(([a, b, expected]) => {
    assertEquals(lte(a, b), expected, `lte(${a}, ${b}) -> ${expected}`)
  })
})

View source on GitHub


multiply

Added from 1.0.0

🔗  add  subtract  divide 

Math

Multiplies first argument and second argument.

Signature:

multiply: {
    (a: number, b: number): number;
    (a: number): (b: number) => number;
    (a: bigint, b: bigint): bigint;
    (a: bigint): (b: bigint) => bigint;
}
Parameters
ParameterDescription
aThe first input number
bThe second input number

=> The result of a * b

Example 1

// Basic
multiply(2, 3) // 6

Example 2

// Bigint
multiply(2n, 3n) // 6n

Example 3

// Curry
const double = multiply(2)
double(4) // 8

View source on GitHub


N

Added from 1.6.0

🔗  NN 

Logic

Returns the ! of its argument.

Signature:

N: <T>(val: T) => T extends FalsyLike ? true : boolean
Parameters
ParameterDescription
valInput any value

=> The result of !val

Remark

The Definition of Falsy - '' - false - 0 - NaN - undefined - null

Example

N('') // true
N(false) // true
N(0) // true
N(NaN) // true
N(undefined) // true
N(null) // true

N({}) // false
N([]) // false
Tests
import { assertEquals } from '../dev_deps.ts'
import { N } from '../src/N.ts'
import { assertEqual } from './asserts.ts'

Deno.test('N', () => {
  const table: [unknown, boolean][] = [
    ['', true],
    [undefined, true],
    [null, true],
    [0, true],
    [NaN, true],
    [false, true],
    [[], false],
    [{}, false],
    ['hello', false],
    [Infinity, false],
    [1, false],
    [-1, false],
    [true, false]
  ]

  table.forEach(([val, expected]) => {
    assertEquals(N(val), expected, `N(${val}) -> ${expected}`)
  })

  assertEqual<true>(N('' as const))
  assertEqual<true>(N(0 as const))
  assertEqual<true>(N(false as const))
  assertEqual<true>(N(undefined))
  assertEqual<true>(N(null))
  assertEqual<boolean>(N(String))
  assertEqual<boolean>(N(Number))
  assertEqual<boolean>(N(NaN))
  assertEqual<boolean>(N(BigInt))
  assertEqual<boolean>(N(Symbol))
  assertEqual<boolean>(N(Date))
  assertEqual<boolean>(N(Object))
  assertEqual<boolean>(N(Array))
})

View source on GitHub


NN

Added from 1.6.0

🔗  N 

Logic

Abbreviation for Not Not. Returns the !! of its argument.

Signature:

NN: <T>(val: T) => T extends FalsyLike ? false : boolean
Parameters
ParameterDescription
valInput any value

=> The result of !!val

Remark

The Definition of Falsy - '' - false - 0 - NaN - undefined - null

Example

NN('') // false
NN(false) // false
NN(0) // false
NN(NaN) // false
NN(undefined) // false
NN(null) // false

NN({}) // true
NN([]) // true
Tests
import { assertEquals } from '../dev_deps.ts'
import { NN } from '../src/NN.ts'
import { assertEqual } from './asserts.ts'

Deno.test('NN', () => {
  const table: [unknown, boolean][] = [
    ['', false],
    [undefined, false],
    [null, false],
    [0, false],
    [NaN, false],
    [false, false],
    [[], true],
    [{}, true],
    ['hello', true],
    [Infinity, true],
    [1, true],
    [-1, true],
    [true, true]
  ]

  table.forEach(([val, expected]) => {
    assertEquals(NN(val), expected, `NN(${val}) -> ${expected}`)
  })

  assertEqual<false>(NN('' as const))
  assertEqual<false>(NN(0 as const))
  assertEqual<false>(NN(false as const))
  assertEqual<false>(NN(undefined))
  assertEqual<false>(NN(null))
  assertEqual<boolean>(NN(String))
  assertEqual<boolean>(NN(Number))
  assertEqual<boolean>(NN(NaN))
  assertEqual<boolean>(NN(BigInt))
  assertEqual<boolean>(NN(Symbol))
  assertEqual<boolean>(NN(Date))
  assertEqual<boolean>(NN(Object))
  assertEqual<boolean>(NN(Array))
})

View source on GitHub


not

Added from 1.3.0

Returns the function as is with return value !.

Signature:

not: <T extends AnyFn<any, unknown>>(fn: T) => (...val: Parameters<T>) => boolean
Parameters
ParameterDescription
valInput any function

=> The result is function what return value with !

Example

not(() => true)() // false
const gt10 = (val: number) => val > 10
not(gt10)(11) // false
Tests
import { assertEquals, isString } from '../dev_deps.ts'
import { not } from '../src/not.ts'
import { AnyFn } from '../src/types/index.ts'
import { assertEqual } from './asserts.ts'

Deno.test('not', () => {
  const table: [AnyFn, unknown[], boolean][] = [
    [() => true, [false], false],
    [() => false, [false], true],
    [(a: string) => a, [''], true],
    [(a: string) => a, [''], true],
    [(a: number, b: number) => a + b, [-1, 1], true],
    [(a: number, b: number) => a + b, [1, 1], false],
    [(a: number, b: number) => a + b, [1, 1, 1], false],
    [isString, [1], true],
    [isString, [''], false]
  ]

  table.forEach(([val, args, expected]) => {
    assertEquals(
      not(val)(...args),
      expected,
      `not(${val})(${args}) -> ${expected}`
    )
  })

  assertEqual<() => boolean>(not(() => true))
  assertEqual<(arg: unknown) => boolean>(not(isString))
  assertEqual<(a: string, b: string) => boolean>(
    not((a: string, b: string) => a + b)
  )
  assertEqual<(...val: unknown[]) => boolean>(not(Array))
})

View source on GitHub


or

Added from 1.1.0

🔗  and  xor 

Logic

Returns true if one or both of its arguments are true; otherwise false.

Signature:

or: <T, U>(a: T | AnyFn<any, T>, b: U | AnyFn<any, U>) => T extends FalsyLike ? U extends FalsyLike ? false : boolean : boolean
Parameters
ParameterDescription
aThe first input any value
bThe second input any value

=> The result of !!a || !!bb (if argument is function, return value)

Remark

If you pass a function as an argument, return value will evaluate.

Example

or(true, true) // true
or(false, true) // true
or(true, false) // true
or(false, false) // false

or(() => 0, () => 1) // true
or(() => 0, () => 0) // false
Tests
import { assertEquals, length, spy } from '../dev_deps.ts'
import { or } from '../src/or.ts'
import { FalsyLike } from './../src/types/index.ts'
import { assertEqual } from './asserts.ts'

Deno.test('or', () => {
  const table: [unknown, unknown, boolean][] = [
    [false, true, true],
    [true, false, true],
    [true, true, true],
    [false, false, false],
    [() => 1, () => 1, true],
    [() => 1, () => 0, true],
    [() => 0, () => 1, true],
    [() => 0, () => 0, false],
    [false, () => 0, false],
    [false, () => 1, true]
  ]
  table.forEach(([a, b, expected]) => {
    assertEquals(or(a, b), expected, `or(${a}, ${b}) -> ${expected}`)
  })

  const fn = spy(() => true)
  const fn2 = spy(() => false)

  or(fn, fn2)
  assertEquals(length(fn2.calls), 0)

  assertEqual<false>(or(false as FalsyLike, false as FalsyLike))
  assertEqual<boolean>(or(Boolean, Boolean))
  assertEqual<boolean>(
    or(
      () => false as const,
      () => true as const
    )
  )
  assertEqual<false>(
    or(
      () => false as const,
      () => false as const
    )
  )

  // Because Truthy can not define.
  assertEqual<boolean>(or(true as const, true as const))
  assertEqual<boolean>(or(true as const, Boolean))
  assertEqual<boolean>(or(Boolean, true as const))
})

View source on GitHub


pipe

Added from 1.8.0

Performs left-to-right function composition.

Signature:

pipe: Pipe
Parameters
ParameterDescription
functionsMulti any functions

=> A function what argument is function[0] argument

Remark

The first argument may have any arity; the remaining arguments must be unary.

Example

const fn = pipe(add , inc)
fn(1, 1) // 3
Tests
/* eslint-disable @typescript-eslint/no-empty-function */
import { assertEquals, isNumber } from '../dev_deps.ts'
import { N } from '../src/N.ts'
import { pipe } from '../src/pipe.ts'
import { AnyFn } from '../src/types/index.ts'
import { assertEqual } from './asserts.ts'

Deno.test('pipe', () => {
  const table: [AnyFn[], unknown, unknown][] = [
    [[() => {}, () => {}], '', undefined],
    [[() => {}, () => true], '', true],
    [[() => {}, () => true], 1, true],
    [[() => 1, (n: number) => n + 1], 1, 2],
    [[isNumber, N], '', true],
    [[N, N], 1, true],
    [[isNumber, N], 1, false],
    [[() => true, () => false, () => true, () => false], 1, false]
  ]

  table.forEach(([[fn1, fn2], argument, expected]) => {
    assertEquals(
      pipe(fn1, fn2)(argument),
      expected,
      `pipe(${fn1}, ${fn2})(${argument}) -> ${expected}`
    )
  })

  assertEqual<boolean>(pipe(isNumber, N)(1))

  const pipetest = pipe(
    (_: number, __: string, ___: number) => {
      return 1 as const
    },
    (a) => a,
    () => [] as [],
    (a) => a,
    (a) => a,
    (a) => a,
    (_) => '' as const,
    (a) => a,
    (a) => a,
    (a) => a,
    (a) => a,
    (a) => a,
    (_) => 1 as const,
    (a) => a,
    (a) => a,
    (_) => '' as const,
    (a) => a
  )
  assertEqual<''>(pipetest(1, '', 1))
})

View source on GitHub


prepend

Added from 1.2.0

🔗  append 

Array

Returns a new list with the given value at the front, followed by the contents of the list.

Signature:

prepend: <T, U>(val: T, list: U[]) => (T | U)[]
Parameters
ParameterDescription
valThe value to add to the front of the new list
listThe list of elements to add a new item to

=> The result of [val, ...list]

Example

prepend('Tom', ['hello']) // ['Tom', 'hello']
prepend('Tom', []) // ['Tom']
prepend(['Tom'], ['hello', 'world']) // [['Tom'], 'hello', 'world']
Tests
import { assertEquals } from '../dev_deps.ts'
import { prepend } from '../src/prepend.ts'

Deno.test('prepend', () => {
  const table: [unknown, unknown[], unknown[]][] = [
    [null, [], [null]],
    [undefined, [], [undefined]],
    ['', [], ['']],
    [{}, [], [{}]],
    [0, [], [0]],
    ['a', ['b'], ['a', 'b']],
    ['a', ['b', 'c', 'd'], ['a', 'b', 'c', 'd']],
    [[], [], [[]]],
    [[1], [], [[1]]],
    [[1], [2], [[1], 2]],
    [[1], [2, 3, 4], [[1], 2, 3, 4]]
  ]
  table.forEach(([val, list, expected]) => {
    assertEquals(
      prepend(val, list),
      expected,
      `prepend(${val}, ${list}) -> ${expected}`
    )
  })
})

View source on GitHub


product

Added from 1.2.0

Math

Multiplies together all the elements of a list.

Signature:

product: {
    (val: [
    ]): 0;
    (val: number[]): number;
    (val: bigint[]): bigint;
}
Parameters
ParameterDescription
vallist An array of numbers

=> The product of all the numbers in the list

Example

product([1, 2, 3, 4, 5]) // 120
product([1n, 2n, 3n, 4n, 5n]) //120n
product([]) // 0
Tests
import { assertEquals } from '../dev_deps.ts'
import { product } from '../src/product.ts'

Deno.test('product', () => {
  const tableNumber: [number[], number][] = [
    [[], 0],
    [[0, 0], 0],
    [[1], 1],
    [[1, 2, 3, 4, 5], 120],
    [[1, -2, 3, -4, 5], 120]
  ]

  tableNumber.forEach(([val, expected]) => {
    assertEquals(product(val), expected, `product(${val}) -> ${expected}`)
  })

  const tableBigint: [bigint[], bigint][] = [
    [[0n], 0n],
    [[1n], 1n],
    [[1n, 2n, 3n, 4n, 5n], 120n],
    [[1n, -2n, 3n, -4n, 5n], 120n]
  ]

  tableBigint.forEach(([val, expected]) => {
    assertEquals(product(val), expected, `product(${val}) -> ${expected}`)
  })
})

View source on GitHub


props

Added from 1.4.0

🔗  has 

Object

Returns a function that when supplied an object returns the indicated property of that object, if it exists.

Signature:

props: <T extends string | number, U extends Record<PropertyKey, unknown>>(val: T, obj: U) => U extends Record<T, unknown> ? U[T] : undefined
Parameters
ParameterDescription
valInput property key
objThe object to query

=> The result of safety obj[val] or obj[val[0]][val[1]][val[...x]]

Example

props('x', { x: 'hello' }) // 'hello'
props(1, { 1: 100 }) // 100
props('x', {}) // undefined
Tests
import { assertEquals } from '../dev_deps.ts'
import { props } from '../src/props.ts'
import { assertEqual } from './asserts.ts'

Deno.test('props', () => {
  const table: [string | number, Record<PropertyKey, unknown>, unknown][] = [
    ['', {}, undefined],
    ['', { ' ': '' }, undefined],
    ['', { ' ': { '': '' } }, undefined],
    [0, {}, undefined],
    [0, { 1: '' }, undefined],
    [0, { 1: { 0: '' } }, undefined],
    ['', { '': '' }, ''],
    ['Hello', { hello: '' }, undefined],
    ['Hello', { Hello: '' }, ''],
    ['hello', { hello: '' }, ''],
    [0, { 0: 1 }, 1]
  ]
  table.forEach(([a, b, expected]) => {
    assertEquals(props(a, b), expected, `props(${a}, ${b}) -> ${expected}`)
  })

  assertEqual<undefined>(props('', {}))
  assertEqual<undefined>(props('a', {}))
  assertEqual<string>(props('a', { a: 'b' }))
  assertEqual<undefined>(props(0, { a: 'b' }))
  assertEqual<1>(props(0, { 0: 1 as const }))
  assertEqual<number>(props(0, { 0: 1 }))
  assertEqual<'b'>(props('a', { a: 'b' as const }))
})

View source on GitHub


subtract

Added from 1.0.0

🔗  add  multiply  divide 

Math

Subtracts its second argument from its first argument.

Signature:

subtract: {
    (a: number, b: number): number;
    (a: number): (b: number) => number;
    (a: bigint, b: bigint): bigint;
    (a: bigint): (b: bigint) => bigint;
} & {
    (a: typeof _, b: number): (a: number) => number;
    (a: typeof _, b: bigint): (a: bigint) => bigint;
}
Parameters
ParameterDescription
aThe first input number
bThe second input number

=> The result of a - b

Remark

Since subtraction is not idempotent, there are two ways to curry.

Example 1

// Number
subtract(2, 1) // 1

Example 2

// Bigint
subtract(3n, 2n) //1n

Example 3

// First argument curry
const from5Minus = subtract(5)
from5Minus(10) // -5

Example 4

// Second argument curry
import { _ } from 'fonction'
const minus5 = (_, 5)
minus5(20) // 15

View source on GitHub


sum

Added from 1.0.0

Math

Adds together all the elements of a list.

Signature:

sum: {
    (val: [
    ]): 0;
    (val: number[]): number;
    (val: bigint[]): bigint;
}
Parameters
ParameterDescription
vallist An array of numbers

=> The sum of all the numbers in the list

Example

sum([1, 2, 3, 4, 5]) // 15
sum([1n, 2n, 3n, 4n, 5n]) // 15n
sum([]) // 0
Tests
import { assertEquals } from '../dev_deps.ts'
import { sum } from '../src/sum.ts'

Deno.test('sum', () => {
  const tableNumber: [number[], number][] = [
    [[], 0],
    [[0, 0], 0],
    [[1], 1],
    [[1, 2, 3, 4, 5], 15],
    [[1, -2, 3, -4, 5], 3]
  ]
  tableNumber.forEach(([val, expected]) => {
    assertEquals(sum(val), expected, `sum(${val}) -> ${expected}`)
  })

  const tableBigint: [bigint[], bigint][] = [
    [[0n, 0n], 0n],
    [[1n], 1n],
    [[1n, 2n, 3n, 4n, 5n], 15n],
    [[1n, -2n, 3n, -4n, 5n], 3n]
  ]

  tableBigint.forEach(([val, expected]) => {
    assertEquals(sum(val), expected, `sum(${val}) -> ${expected}`)
  })
})

View source on GitHub


T

Added from 1.1.0

🔗  F 

A function that always returns true. Any passed in parameters are ignored.

Signature:

T: AnyFn<unknown, true>
Parameters

=> True

Example

T() // true
T(1, 'hello', 'world') // true
Tests
import { assertEquals } from '../dev_deps.ts'
import { T } from '../src/T.ts'
import { assertReturnType } from './asserts.ts'

Deno.test('T', () => {
  const table: unknown[] = [
    [''],
    [1, 2, 3],
    [{}, [], undefined, null],
    [undefined],
    [null]
  ]
  table.forEach((val) => {
    assertEquals(T(val), true, `T(${val}) -> true`)
  })

  assertReturnType<true>(T)
})

View source on GitHub


tail

Added from 1.2.0

🔗  head 

ArrayString

Returns all but the first element of the given list or string.

Signature:

tail: <T extends string | readonly unknown[]>(val: T) => Tail<T>
Parameters
ParameterDescription
valstring or any array object

=> The result of val.slice(1, Infinity)

Example 1

// String
tail('hello') // 'ello'
tail('h') // ''
tail('') // ''

Example 2

tail([1, 2, 3]) // [2, 3]
tail(['hello', 'world']) // ['world']
tail(['hello']) // []
tail([]) // []

View source on GitHub


take

Added from 1.6.0

🔗  takeLast 

StringArray

Return a slice of string or array with n elements taken from the beginning.

Signature:

take: <T extends string | readonly unknown[]>(howMany: number, val: T) => T
Parameters
ParameterDescription
howManyThe number of elements to take
valString or Array to query

=> The slice of array

Example 1

// String
take(3, 'hello') // 'hel'

Example 2

// Array
take(3, [1, 2, 3, 4]) // [1, 2, 3]
Tests
import { assertEquals } from '../dev_deps.ts'
import { take } from '../src/take.ts'
import { assertEqual } from './asserts.ts'

Deno.test('take', () => {
  const table: [number, unknown[] | string, unknown[] | string][] = [
    [0, [], []],
    [Infinity, [], []],
    [Infinity, [], []],
    [Infinity, [1], [1]],
    [Infinity, [1, 2, 3, 4], [1, 2, 3, 4]],
    [3, [1, 2, 3, 4], [1, 2, 3]],
    [2, [1, 2, 3, 4], [1, 2]],
    [100, [1, 2, 3, 4], [1, 2, 3, 4]],
    [0, [1, 2, 3, 4], []],
    [-0, [1, 2, 3, 4], []],
    [-1, [1, 2, 3, 4], [4]],
    [-2, [1, 2, 3, 4], [3, 4]],
    [-3, [1, 2, 3, 4], [2, 3, 4]],
    [-4, [1, 2, 3, 4], [1, 2, 3, 4]],
    [-Infinity, [1, 2, 3, 4], [1, 2, 3, 4]],
    [0, 'hello', ''],
    [-0, 'hello', ''],
    [1, 'hello', 'h'],
    [3, 'hello', 'hel'],
    [Infinity, 'hello', 'hello'],
    [100, 'hello', 'hello'],
    [-1, 'hello', 'o'],
    [-4, 'hello', 'ello'],
    [-5, 'hello', 'hello'],
    [-Infinity, 'hello', 'hello']
  ]
  table.forEach(([howMany, val, expected]) => {
    assertEquals(
      take(howMany, val),
      expected,
      `take(${howMany}, ${val}) -> ${expected}`
    )
  })

  assertEqual<never[]>(take(0, []))
  assertEqual<[]>(take(0, [] as []))
  assertEqual<readonly []>(take(0, [] as const))
  assertEqual<''>(take(0, ''))
  assertEqual<''>(take(0, '' as const))
})

View source on GitHub


takeLast

Added from 1.6.0

🔗  take 

StringArray

Return a slice of string or array with n elements taken from the end.

Signature:

takeLast: <T extends string | readonly unknown[]>(howMany: number, val: T) => T
Parameters
ParameterDescription
howManyThe number of elements to take
valString or Array to query

=> The slice of array

Example 1

// String
takeLast(3, 'hello') // 'llo'

Example 2

// Array
takeLast(3, [1, 2, 3, 4]) // [2, 3, 4]
Tests
import { assertEquals } from '../dev_deps.ts'
import { takeLast } from '../src/takeLast.ts'
import { assertEqual } from './asserts.ts'

Deno.test('takeLast', () => {
  const table: [number, unknown[] | string, unknown[] | string][] = [
    [0, [], []],
    [Infinity, [], []],
    [Infinity, [], []],
    [Infinity, [1], [1]],
    [Infinity, [1, 2, 3, 4], [1, 2, 3, 4]],
    [3, [1, 2, 3, 4], [2, 3, 4]],
    [2, [1, 2, 3, 4], [3, 4]],
    [100, [1, 2, 3, 4], [1, 2, 3, 4]],
    [0, [1, 2, 3, 4], []],
    [-0, [1, 2, 3, 4], []],
    [-1, [1, 2, 3, 4], [1]],
    [-2, [1, 2, 3, 4], [1, 2]],
    [-3, [1, 2, 3, 4], [1, 2, 3]],
    [-4, [1, 2, 3, 4], [1, 2, 3, 4]],
    [-Infinity, [1, 2, 3, 4], [1, 2, 3, 4]],
    [0, 'hello', ''],
    [-0, 'hello', ''],
    [1, 'hello', 'o'],
    [3, 'hello', 'llo'],
    [Infinity, 'hello', 'hello'],
    [100, 'hello', 'hello'],
    [-1, 'hello', 'h'],
    [-4, 'hello', 'hell'],
    [-5, 'hello', 'hello'],
    [-Infinity, 'hello', 'hello']
  ]
  table.forEach(([howMany, val, expected]) => {
    assertEquals(
      takeLast(howMany, val),
      expected,
      `takeLast(${howMany}, ${val}) -> ${expected}`
    )
  })

  assertEqual<never[]>(takeLast(0, []))
  assertEqual<[]>(takeLast(0, [] as []))
  assertEqual<readonly []>(takeLast(0, [] as const))
  assertEqual<''>(takeLast(0, ''))
  assertEqual<''>(takeLast(0, '' as const))
})

View source on GitHub


tap

Added from 1.9.0

Runs the given function with the supplied value, then returns the value.

Signature:

tap: <T>(fn: Arity1Fn<T, unknown>) => <R extends T>(val: R) => R
Parameters
ParameterDescription
fnThe function to call with val . The return value of fn will be thrown away.

=> The result of (val) => fn(val)

Example

tap(console.log)('hello') // hello
// log: hello

View source on GitHub


tryCatch

Added from 1.8.0

Logic

tryCatch takes two functions, a tryer and a catcher. The returned function evaluates the tryer; if it does not throw, it simply returns the result. If the tryer does throw, the returned function evaluates the catcher function and returns its result.

Signature:

tryCatch: <R, E, P = unknown>(tryer: AnyFn<any, R>, catcher?: E | AnyFn<P, E> | undefined) => R | E
Parameters
ParameterDescription
tryerThe function that may throw.
catcherThe function that will be evaluated if tryer throws.

=> - The result of try { tryer() } catch(e) { catcher(e) }

Example

tryCatch(() => { throw Error('error') }) // Error('error')
tryCatch(() => { throw Error('error') }, 0) // 0
tryCatch(() => { throw Error('error') }, (e: Error) => e.message ) // 'error'
Tests
import { assertEquals } from '../dev_deps.ts'
import { tryCatch } from '../src/tryCatch.ts'
import { AnyFn } from '../src/types/index.ts'
import { assertEqual } from './asserts.ts'

Deno.test('tryCatch', () => {
  const table: [AnyFn, unknown][] = [
    [() => true, true],
    [() => false, false],
    [
      () => {
        throw Error('hello')
      },
      Error('hello')
    ]
  ]
  table.forEach(([val, expected]) => {
    assertEquals(tryCatch(val), expected, `tryCatch(${val}) -> ${expected}`)
  })

  const table2: [AnyFn, unknown | AnyFn, unknown][] = [
    [() => true, () => false, true],
    [() => true, false, true],
    [
      () => {
        throw Error('hello')
      },
      false,
      false
    ],
    [
      () => {
        throw Error('hello')
      },
      (e: Error) => e.message,
      'hello'
    ],
    [
      () => {
        throw Error('hello')
      },
      () => 1,
      1
    ]
  ]
  table2.forEach(([tryer, catcher, expected]) => {
    assertEquals(
      tryCatch(tryer, catcher),
      expected,
      `tryCatch(${tryer}, ${catcher}) -> ${expected}`
    )
  })

  assertEqual<unknown>(tryCatch(() => true))
  assertEqual<true | Error>(tryCatch<true, Error>(() => true))
  assertEqual<1 | Error>(tryCatch<1, Error>(() => 1))
  assertEqual<unknown>(
    tryCatch(() => {
      throw Error('hello')
    })
  )
  assertEqual<never | Error>(
    tryCatch<never, Error>(() => {
      throw Error('hello')
    })
  )
  assertEqual<0 | 1>(tryCatch(() => 1, 0))
  assertEqual<0 | 1>(
    tryCatch(
      () => 1,
      () => 0
    )
  )
  assertEqual<1 | Error>(
    tryCatch(
      () => 1,
      (e: Error) => e
    )
  )
  assertEqual<1 | string>(
    tryCatch(
      () => 1,
      (e: Error) => e.message
    )
  )
})

View source on GitHub


uniq

Added from 1.8.0

🔗  equal 

Array

Returns a new Array containing only one copy of each element in the original array. equal is used to determine equality.

Signature:

uniq: <T extends unknown>(val: readonly T[]) => T[]
Parameters
ParameterDescription
valInput any array

=> The list of unique items

Example

uniq([1, 2, 1, 1]) // [1, 2]
uniq([{}, {}, [], []]) // [{}, []]
uniq([[1, 2, 3], [1, 2, 3]]) // [[1, 2, 3]]
Tests
import { assertEquals } from '../dev_deps.ts'
import { uniq } from '../src/uniq.ts'
import { assertEqual } from './asserts.ts'

Deno.test('uniq', () => {
  const table: [unknown[], unknown[]][] = [
    [[], []],
    [
      ['', '', '', '', '', 2, '', 1],
      ['', 2, 1]
    ],
    [
      [null, undefined, 1n, null, undefined, 2n],
      [null, undefined, 1n, 2n]
    ],
    [
      [1, 2, 3],
      [1, 2, 3]
    ],
    [
      [1, 1, 2, 2, 3, 3],
      [1, 2, 3]
    ],
    [[{}, {}, {}], [{}]],
    [
      [[], {}, [], {}],
      [[], {}]
    ],
    [
      [[], {}, [], {}],
      [[], {}]
    ],
    [
      [[1, 2, 3], { hoge: 'huga' }, [], { hage: 'hoge' }],
      [[1, 2, 3], { hoge: 'huga' }, [], { hage: 'hoge' }]
    ],
    [
      [
        [1, [], [1, [2, [3]]]],
        { hoge: 'huga', 1: 0 },
        [1, [], [1, [2, [3]]]],
        { hoge: 'huga', 1: 0, 2: 3 }
      ],
      [
        [1, [], [1, [2, [3]]]],
        { hoge: 'huga', 1: 0 },
        { hoge: 'huga', 1: 0, 2: 3 }
      ]
    ]
  ]
  table.forEach(([val, expected]) => {
    assertEquals(uniq(val), expected, `uniq(${val}) -> ${expected}`)
  })

  assertEqual<never[]>(uniq([]))
  assertEqual<string[]>(uniq(['']))
  assertEqual<''[]>(uniq([''] as const))
  assertEqual<('' | '1' | '2')[]>(uniq(['', '1', '2'] as const))
  assertEqual<('' | '1' | '2')[]>(uniq(['', '1', '2', '1', '2'] as const))
})

View source on GitHub


xor

Added from 1.1.0

🔗  and  or 

Logic

Returns true if one of the arguments is truthy and the other is falsy; otherwise false.

Signature:

xor: <T, U>(a: T | AnyFn<any, T>, b: U | AnyFn<any, U>) => T extends FalsyLike ? U extends FalsyLike ? false : boolean : boolean
Parameters
ParameterDescription
aThe first input any value
bThe second input any value

=> The result of !a !== !b (if argument is function, return value)

Example

xor(true, false) // true
xor(false, true) // true
xor(true, true) // false
xor(false, false) // false
xor(() => 1, () => 0) // true
xor(() => 0, () => 0) // false
Tests
import { assertEquals } from '../dev_deps.ts'
import { xor } from '../src/xor.ts'
import { Falsy } from './../src/types/index.ts'
import { assertEqual } from './asserts.ts'

Deno.test('xor', () => {
  const table: [unknown, unknown, boolean][] = [
    [false, true, true],
    [true, false, true],
    [true, true, false],
    [false, false, false],
    [() => false, () => true, true],
    [() => true, () => false, true],
    [() => true, () => true, false],
    [() => false, () => false, false],
    [() => false, () => false, false],
    [() => 1, () => 0, true],
    [() => 0, () => 0, false],
    [true, () => false, true]
  ]
  table.forEach(([a, b, expected]) => {
    assertEquals(xor(a, b), expected, `xor(${a}, ${b}) -> ${expected}`)
  })

  assertEqual<false>(xor(false as Falsy, false as Falsy))
  assertEqual<boolean>(xor(Boolean, Boolean))
  assertEqual<false>(
    xor(
      () => 0 as const,
      () => 0 as const
    )
  )
  assertEqual<boolean>(
    xor(
      () => 0 as const,
      () => 1
    )
  )

  // Because Truthy can not define.
  assertEqual<boolean>(xor(true as const, true as const))
  assertEqual<boolean>(xor(true as const, Boolean))
  assertEqual<boolean>(xor(Boolean, true as const))
})

View source on GitHub


Types

AnyFn

Added from 1.0.0

Type of any function.

Signature:

type AnyFn<T = any, U = unknown> = (...args: T[]) => U;

View source on GitHub


Arity1Fn

Added from 1.9.0

Type of arity 1 function.

Signature:

type Arity1Fn<T = any, U = unknown> = (args: T) => U;

View source on GitHub


Empty

Added from 1.3.0

Alias

Alias for Empty values

Signature:

type Empty = "" | [
] | {};

View source on GitHub


FalsyLike

Added from 1.6.0

Alias

Alias for Falsy values.

Signature:

type FalsyLike = false | "" | 0 | 0n | null | undefined;

Remark

This is not a strict Falsy. TypeScript type system cannot define NaN.

View source on GitHub


FlattenDeep

Added from 1.5.0

Array

Infer deep flatted array.

Signature:

type FlattenDeep<T extends readonly unknown[]> = T extends readonly [
    infer A,
    ...infer Rest
] ? A extends readonly unknown[] ? [
    ...FlattenDeep<A>,
    ...FlattenDeep<Rest>
] : [
    A,
    ...FlattenDeep<Rest>
] : [
    ...T
];
Parameters

=> Deep flatted array

Example

FlattenDeep<[]> // []
FlattenDeep<[[1, [2, [3, [4]], 5]]> // [1, 2, 3, 4, 5]

View source on GitHub


Head

Added from 2.0.0

🔗  Last 

ArrayString

Infer the head types.

Signature:

type Head<T extends readonly unknown[] | string> = T extends string ? T extends `${infer F}${string}` ? F : T extends "" ? "" : string : T extends readonly [
    infer U,
    ...infer _
] ? U : T[0] | undefined;
Parameters

=> Head element of the T

Example 1

// String
Head<string> // string
Head<''> // ''
Head<'hello'> // 'h'

Example 2

// Array
Head<[] | never[] | readonly [] | readonly never[]> // undefined
Head<['hello', 'world']> // 'hello'
Head<string | number[]> // string | number | undefined

View source on GitHub


Init

Added from 2.1.0

🔗  Tail 

ArrayString

Infer the init types.

Signature:

type Init<T extends string | readonly unknown[]> = T extends string ? InitString<T> : T extends readonly [
    ...infer I,
    unknown
] ? I : T;

Example 1

// String
Init<string> // string
Init<''> // ''
Init<'hello'> // 'hell'

Example 2

// Array
Init<[] | never[] | readonly [] | readonly never[]> // []
Init<['hello']> // []
Init<['hello', 'world']> // ['hello']

View source on GitHub


Last

Added from 1.4.0

🔗  head 

ArrayString

Infer the last types.

Signature:

type Last<T extends string | readonly unknown[]> = T extends string ? LastString<T> : T extends readonly [
    ...infer _,
    infer L
] ? L : T[T["length"]] | undefined;

Example 1

// String
Last<string> // string
Last<''> // ''
Last<'hello'> // 'o'

Example 2

// Array
Last<[] | never[] | readonly [] | readonly never[]> // undefined
Last<['hello', 'world']> // 'world'
Last<string | number[]> // string | number | undefined

View source on GitHub


Ord

Added from 1.1.0

Alias

Abbreviation for Ordinal.

Signature:

type Ord = string | number | bigint | boolean | Date;

View source on GitHub


Primitive

Added from 1.0.0

Alias

Alias for Primitive values types.

Signature:

type Primitive = string | number | bigint | boolean | symbol | undefined | null;

View source on GitHub


Space

Added from 1.5.0

Alias

Alias for Space values.

Signature:

type Space = " " | "\n" | "\t";

View source on GitHub


Tail

Added from 2.1.0

🔗  Init 

ArrayString

Infer the tail types.

Signature:

type Tail<T extends string | readonly unknown[]> = T extends string ? T extends `${string}${infer R}` ? R : T extends "" ? "" : string : T extends readonly [
    unknown,
    ...infer R
] ? R : T;

Example 1

// String
Tail<string> // string
Tail<''> // ''
Tail<'a'> // ''
Tail<'hello'> // 'ello'

Example 2

// Array
Tail<[] | never[] | readonly [] | readonly never[]> // []
Tail<['hello']> // []
Tail<['hello', 'world']> // ['world']

View source on GitHub


TypedArray

Added from 2.1.0

beta

Alias for TypedArray

WARNING

This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.

Signature:

type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array;

View source on GitHub


Constants

_

Added from 1.6.1

Placeholder to indicate that the argument is not explicitly set.

Signature:

_: unique symbol
Parameters

=> The results of Symbol('_')

View source on GitHub


Latest

Last Updated: