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: 2.1.0-beta.2
Functions
add
Added from 1.0.0
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
Parameter | Description |
---|---|
a | The first input number |
b | The 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
advance
Added from 1.7.0
Returns return value if argument is function
; otherwise returns the value as it is.
Signature:
advance: <T>(val: T | AnyFn<any, T>) => T
Parameters
Parameter | Description |
---|---|
val | Input any value |
=>
The result of typeof val === 'function' ? val(): val
Example
advance(1) // 1
advance(() => 1) // 1
and
Added from 1.1.0
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
Parameter | Description |
---|---|
a | The first input any value |
b | The 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
append
Added from 1.2.0
🔗 prepend 🔗
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
Parameter | Description |
---|---|
val | The value to add to the end of the new list |
list | The 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']]
chunk
Added from 1.4.0
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
Parameter | Description |
---|---|
size | The length of each chunk |
array | The 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, []) // []
constructorName
Added from 1.8.0
Safe getter for constructor.name
.
Signature:
constructorName: (val: unknown) => string
Parameters
Parameter | Description |
---|---|
val | Any value |
=>
If val
is null
or undefined
, empty string; otherwise constructor.name
Example
constructorName(null) // ''
constructorName(undefined) // ''
constructorName({}) // 'Object'
constructorName('') // 'String'
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
Parameter | Description |
---|---|
fn | The 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'
dec
Added from 1.1.0
🔗 inc
Decrements its argument.
Signature:
dec: {
(val: number): number;
(val: bigint): bigint;
}
Parameters
Parameter | Description |
---|---|
val | input number or bigint |
=>
Decremented val
Example
dec(100) // 99
dec(10n) // 9n
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
Parameter | Description |
---|---|
a | a 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'
divide
Added from 1.0.0
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
Parameter | Description |
---|---|
a | The first input number |
b | The 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
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
Parameter | Description |
---|---|
a | Input any value |
b | Input 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
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
flattenDeep
Added from 1.5.0
Recursively flattens array.
Signature:
flattenDeep: <T extends readonly unknown[]>(val: T) => FlattenDeep<T>
Parameters
Parameter | Description |
---|---|
val | The array to flatten |
=>
The result of val.flat(Infinity)
Example
flattenDeep([]) // []
flattenDeep([1, [2, [3, [4]], 5]]) // [1, 2, 3, 4, 5]
gt
Added from 1.1.0
Returns true
if the first argument is greater than the second; otherwise false
.
Signature:
gt: <T extends Ord>(a: T, b: T) => boolean
Parameters
Parameter | Description |
---|---|
a | The first input value |
b | The 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
gte
Added from 1.1.0
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
Parameter | Description |
---|---|
a | The first input value |
b | The 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
has
Added from 1.2.0
🔗 props
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
Parameter | Description |
---|---|
props | The name of the property to check for |
obj | The 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
head
Added from 1.2.0
🔗 Last
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'
identity
Added from 1.2.0
Return the parameter supplied to it.
Signature:
identity: <T>(val: T) => T
Parameters
Parameter | Description |
---|---|
val | The value to return |
=>
The result of val
Example
identity(1) // 1
identity({}) // {}
ifElse
Added from 1.6.0
🔗 ifElseFn
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
Parameter | Description |
---|---|
val | A predicate value |
onTrue | The val evaluates to a truthy value |
onFalse | The 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
ifElseFn
Added from 1.6.0
🔗 ifElse
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
Parameter | Description |
---|---|
condition | A predicate function |
onTrue | Any value or A function to invoke when the condition evaluates to a truthy value |
onFalse | Any 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
inc
Added from 1.1.0
🔗 dec
Increments its argument.
Signature:
inc: {
(val: number): number;
(val: bigint): bigint;
}
Parameters
Parameter | Description |
---|---|
val | Input number or bigint |
=>
Incremented val
Example
inc(100) // 101
inc(10n) // 11n
init
Added from 2.0.0
🔗 tail
Returns all but the init element of the given list or string.
Signature:
init: <T extends string | readonly unknown[]>(val: T) => Init<T>
Parameters
Parameter | Description |
---|---|
val | string 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([]) // []
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
Parameter | Description |
---|---|
val | The value to wrap in a function |
=>
Function wrapped val
Example
const k = K('k')
k() // 'k'
last
Added from 1.0.0
🔗 first
Returns the last element of the given list or string.
Signature:
last: <T extends string | readonly unknown[]>(val: T) => Last<T>
Parameters
Parameter | Description |
---|---|
val | string 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
lt
Added from 1.1.0
Returns true
if the first argument is less than the second; otherwise false
.
Signature:
lt: <T extends Ord>(a: T, b: T) => boolean
Parameters
Parameter | Description |
---|---|
a | The first input value |
b | The 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
lte
Added from 1.1.0
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
Parameter | Description |
---|---|
a | The first input value |
b | The 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
multiply
Added from 1.0.0
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
Parameter | Description |
---|---|
a | The first input number |
b | The 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
N
Added from 1.6.0
🔗 NN
Returns the !
of its argument.
Signature:
N: <T>(val: T) => T extends FalsyLike ? true : boolean
Parameters
Parameter | Description |
---|---|
val | Input 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
NN
Added from 1.6.0
🔗 N
Abbreviation for Not Not. Returns the !!
of its argument.
Signature:
NN: <T>(val: T) => T extends FalsyLike ? false : boolean
Parameters
Parameter | Description |
---|---|
val | Input 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
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
Parameter | Description |
---|---|
val | Input any function |
=>
The result is function what return value with !
Example
not(() => true)() // false
const gt10 = (val: number) => val > 10
not(gt10)(11) // false
or
Added from 1.1.0
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
Parameter | Description |
---|---|
a | The first input any value |
b | The 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
pipe
Added from 1.8.0
Performs left-to-right function composition.
Signature:
pipe: Pipe
Parameters
Parameter | Description |
---|---|
functions | Multi 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
prepend
Added from 1.2.0
🔗 append
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
Parameter | Description |
---|---|
val | The value to add to the front of the new list |
list | The 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']
product
Added from 1.2.0
Multiplies together all the elements of a list.
Signature:
product: {
(val: [
]): 0;
(val: number[]): number;
(val: bigint[]): bigint;
}
Parameters
Parameter | Description |
---|---|
val | list 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
props
Added from 1.4.0
🔗 has
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
Parameter | Description |
---|---|
val | Input property key |
obj | The 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
subtract
Added from 1.0.0
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
Parameter | Description |
---|---|
a | The first input number |
b | The 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
sum
Added from 1.0.0
Adds together all the elements of a list.
Signature:
sum: {
(val: [
]): 0;
(val: number[]): number;
(val: bigint[]): bigint;
}
Parameters
Parameter | Description |
---|---|
val | list 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
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
tail
Added from 1.2.0
🔗 head
Returns all but the first element of the given list or string.
Signature:
tail: <T extends string | readonly unknown[]>(val: T) => Tail<T>
Parameters
Parameter | Description |
---|---|
val | string 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([]) // []
take
Added from 1.6.0
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
Parameter | Description |
---|---|
howMany | The number of elements to take |
val | String 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]
takeLast
Added from 1.6.0
🔗 take
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
Parameter | Description |
---|---|
howMany | The number of elements to take |
val | String 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]
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
Parameter | Description |
---|---|
fn | The 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
tryCatch
Added from 1.8.0
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
Parameter | Description |
---|---|
tryer | The function that may throw. |
catcher | The 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'
uniq
Added from 1.8.0
🔗 equal
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
Parameter | Description |
---|---|
val | Input 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]]
xor
Added from 1.1.0
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
Parameter | Description |
---|---|
a | The first input any value |
b | The 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
Types
AnyFn
Added from 1.0.0
Type of any function.
Signature:
type AnyFn<T = any, U = unknown> = (...args: T[]) => U;
Arity1Fn
Added from 1.9.0
Type of arity 1 function.
Signature:
type Arity1Fn<T = any, U = unknown> = (args: T) => U;
Empty
Added from 1.3.0
Alias for Empty values
Signature:
type Empty = "" | [
] | {};
FalsyLike
Added from 1.6.0
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
.
Remark
Alias
FlattenDeep
Added from 1.5.0
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]
Head
Added from 2.0.0
🔗 Last
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 never[] | [
] ? undefined : 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
Init
Added from 2.1.0
🔗 Tail
Infer the init types.
Signature:
type Init<T extends string | readonly unknown[]> = T extends string ? InitString<T> : T extends readonly never[] | [
] ? [
] : 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', 'world']> // 'hello'
Last
Added from 1.4.0
🔗 First
Infer the last types.
Signature:
type Last<T extends string | readonly unknown[]> = T extends string ? LastString<T> : T extends never[] | [
] ? undefined : 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
Ord
Added from 1.1.0
Abbreviation for Ordinal.
Signature:
type Ord = string | number | bigint | boolean | Date;
Primitive
Added from 1.0.0
Alias for Primitive values types.
Signature:
type Primitive = string | number | bigint | boolean | symbol | undefined | null;
Space
Added from 1.5.0
Alias for Space values.
Signature:
type Space = " " | "\n" | "\t";
Tail
Added from 2.1.0
🔗 Init
beta
Infer the tail types.
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 Tail<T extends string | readonly unknown[]> = T extends string ? T extends `${string}${infer R}` ? R : T extends "" ? "" : string : T extends readonly never[] | [
] ? [
] : T extends readonly [
unknown,
...infer R
] ? R : T;
Example 1
// String
Tail<string> // string
Tail<''> // ''
Tail<'hello'> // 'ello'
Example 2
// Array
Tail<[] | never[] | readonly [] | readonly never[]> // []
Tail<['hello', 'world']> // 'world'
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;
Constants
_
Added from 1.6.1
Placeholder to indicate that the argument is not explicitly set.
Signature:
_: unique symbol
Parameters
=>
The results of Symbol('_')
2.1.0-beta.2