array

Checks if the given array is present and it is not empty (contains at least one element).

Type signature

(xs?: any[]) => boolean

Examples

any([]);
// ⇒ false
Try in REPL
any([1, 2, 3]);
// ⇒ true
Try in REPL

Questions

  • How to check if an array is empty?
  • How to check if an array is empty or null or undefined?
  • How to check if an array is empty or not?
  • How to check if an array is empty or doesn't exist?

TypeScript sourceJavaScript source

Checks if the given arguments are all `Arrays`.

Type signature

(...xs: any[]) => boolean

Examples

are([2, 3]);
// ⇒ true
Try in REPL
are([1, 2, 3], []);
// ⇒ true
Try in REPL
are([1, 2, 3], 8, [1, 3], "test");
// ⇒ false
Try in REPL

Questions

  • How to check if all the given values are arrays?

TypeScript sourceJavaScript source

Splits the given array into an array of chunks of up to the given length.

Type signature

(count: number) => (xs: any[]) => any[]

Examples

chunk(2)(["a", "b", "c", "d"]);
// ⇒ [['a', 'b'], ['c', 'd']]
Try in REPL
chunk(3)(["a", "b", "c", "d"]);
// ⇒ [['a', 'b', 'c'], ['d']]
Try in REPL

Questions

  • How to split an array into chunks?
  • How to split an array into chunks of the same size?

TypeScript sourceJavaScript source

Computes a set difference between the two given arrays.

Type signature

(xs: any[], ys: any[]) => any[]

Examples

difference(
  [1, 2, 3, 4, 5, 6],
  [2, 4]
);
// ⇒ [1, 3, 5, 6]
Try in REPL

Questions

  • How to find elements which are present in the first array and not in the second?

TypeScript sourceJavaScript source

Checks if two arrays are not equal.

Type signature

(xs?: any[], ys?: any[]) => boolean

Examples

differs([1, 2, 3], [1, 2]);
// ⇒ true
Try in REPL
differs([1, 2, 3], [1, 2, 3]);
// ⇒ false
Try in REPL

Questions

  • How to check if two arrays differ?
  • How to check if two arrays are not equal?
  • How to check if two arrays are equal or not?

TypeScript sourceJavaScript source

Lists all the duplicated values in the given array.

Type signature

(xs: any[]) => any[]

Examples

duplicates([
  1,
  2,
  3,
  4,
  3,
  4,
  3,
  6,
]);
// ⇒ [3, 4, 3]
Try in REPL

Questions

  • How to find duplicates in an array?

TypeScript sourceJavaScript source

Empty array.

Type signature

any[]

Examples

empty;
// ⇒ []
Try in REPL

Questions

  • How to get an empty array?

TypeScript sourceJavaScript source

Takes exactly the given count of elements.

Type signature

(count: number) => (xs: any[]) => any[]

Examples

exact(5)([1, 2, 3]);
// ⇒ [1, 2, 3, undefined, undefined]
Try in REPL
exact(2)([1, 2, 3]);
// ⇒ [1, 2]
Try in REPL

Questions

  • How to get exactly N elements out of an array?

TypeScript sourceJavaScript source

Filters out the given value.

Type signature

(y: any) => (xs: any[]) => any[]

Examples

except(2)([1, 2, 3, 4, 5]);
// ⇒ [1, 3, 4, 5]
Try in REPL
except(2)([1, 2, 2, 4, 2]);
// ⇒ [1, 4]
Try in REPL

Questions

  • How to get all the values of an array except the given one?

TypeScript sourceJavaScript source

Filters the given array with the given predicate just like Array.filter but does it in-place thus mutates the original array.

Type signature

(
  f: (value: any, index: number, context: any[]) => boolean
) => (xs: any[]) => any[]

Examples

const xs = [1, 2, 3, 4, 5, 6, 7];
const odd = (x) => x % 2 === 1;

const ys = filterInPlace(odd)(xs);

ys === xs;
// ⇒ true
ys;
// ⇒ [1, 3, 5, 7]
Try in REPL

Questions

  • How to filter an array in place?

TypeScript sourceJavaScript source

Finds an element by a predicate function within the given array, otherwise, it returns the given fallback value or undefined when fallback is not present.

Type signature

(
  predicate: (value: any, index: number, context: any[]) => boolean,
  fallback?: any
) => (xs: any[]) => any

Examples

find((x) => x > 2)([1, 2, 3, 5, 7]);
// ⇒ 3
Try in REPL
find((x) => x > 2)([
  1,
  2,
  -3,
  -5,
  -7,
]);
// ⇒ undefined
Try in REPL

Questions

  • How to find an element of an array by a given predicate?

TypeScript sourceJavaScript source

Returns the first element or undefined when there are no elements in the given array.

Type signature

([x]: [any]) => any

Examples

first([1, 2, 3]);
// ⇒ 1
Try in REPL
first([]);
// ⇒ undefined
Try in REPL

Questions

  • How to get the first element of an array?

TypeScript sourceJavaScript source

Maps and flattens the result.

Type signature

(
  f: (value: any, index: number, context: any[]) => any
) => (xs: any[]) => any

Examples

flatMap((text) => [...text])([
  "test",
  "123",
]);
// ⇒ ["t", "e", "s", "t", "1", "2", "3"]
Try in REPL

Questions

  • How to flat map an array?
  • How to map and then flatten an array?

TypeScript sourceJavaScript source

Flattens the nested arrays by a single level.

Type signature

(xs: any) => any[]

Examples

flatten([1, [2, 3], 4, [5, 6]]);
// ⇒ [1, 2, 3, 4, 5, 6]
Try in REPL
flatten([
  1,
  [2, [3, 6]],
  4,
  [5, 6],
]);
// ⇒ [1, 2, [3, 6], 4, 5, 6]
Try in REPL

Questions

  • How to flatten an array?

TypeScript sourceJavaScript source

Inserts the given item to the array at a specific index.

Type signature

(
  index: number
) => (item: any) => ([...xs]: any[]) => any[]

Examples

insert(0)("d")(["a", "b", "c"]);
// ⇒ ['d', 'a', 'b', 'c']
Try in REPL
insert(1)("d")(["a", "b", "c"]);
// ⇒ ['a', 'd', 'b', 'c']
Try in REPL

Questions

  • How to insert an element to an array at a given position?

TypeScript sourceJavaScript source

Finds common elements between both arrays.

Type signature

(xs: any[], ys: any[]) => any[]

Examples

intersection(
  [1, 2, 3, 4, 5],
  [5, 5, 3, 2]
);
// ⇒ [2, 3, 5]
Try in REPL

Questions

  • How to find common elements present in both arrays?

TypeScript sourceJavaScript source

Checks if the given argument is an array.

Type signature

(value?: any) => boolean

Examples

is([1, 2, 3]);
// ⇒ true
Try in REPL
is({ a: 5 });
// ⇒ false
Try in REPL

Questions

  • How to check if a value is an array?

TypeScript sourceJavaScript source

Returns the last element or undefined when there are no elements in the given array.

Type signature

(xs: any[]) => any

Examples

last([1, 2, 3]);
// ⇒ 3
Try in REPL
last([]);
// ⇒ undefined
Try in REPL

Questions

  • How to get the last element of an array?

TypeScript sourceJavaScript source

Returns the number of elements in the given array.

Type signature

(xs: any[]) => number

Examples

length([true, 1]);
// ⇒ 2
Try in REPL
length([1, 2, 3]);
// ⇒ 3
Try in REPL
length([]);
// ⇒ 0
Try in REPL

Questions

  • How to check an array's length?
  • How to compute an array's length?
  • How to check the size of an array?
  • How to check the number of elements in an array?

TypeScript sourceJavaScript source

Checks if lengths of the given arrays differ.

Type signature

(a: any[], b: any[]) => boolean

Examples

lengthDiffers([1, 2, 3], [1, 2]);
// ⇒ true
Try in REPL
lengthDiffers([6, 7], [1, 2]);
// ⇒ false
Try in REPL

Questions

  • How to check if array lengths differ?
  • How to check if the given arrays have different lengths?

TypeScript sourceJavaScript source

Maps the given array with the given functions.

Type signature

(...fs: ((x: any) => any)[]) => (xs: any) => any

Examples

map((x) => x * x)([1, 2, 3]);
// ⇒ [1, 4, 9]
Try in REPL
map(
  (x) => x * x,
  (x) => x + 1
)([1, 2, 3]);
// ⇒ [2, 5, 10]
Try in REPL

Questions

  • How to map an array?

TypeScript sourceJavaScript source

Returns the middle element or the right one when the number of elements is even.

Type signature

(xs: any[]) => any

Examples

midpoint([1, 2, 3, 4, 5]);
// ⇒ 3
Try in REPL
midpoint([1, 2, 3, 4]);
// ⇒ 3
Try in REPL

Questions

  • How to get the element in the middle of an array?
  • How to get the middle element of an array?

TypeScript sourceJavaScript source

Computes minimum and maximum values of the given array in a single run.

Type signature

(xs: number[]) => number[]

Examples

minMax([
  10,
  5,
  3,
  -5,
  -4,
  23,
  32,
  8,
  1,
  0,
]);
// ⇒ [-5, 32]
Try in REPL
minMax([1]);
// ⇒ [1, 1]
Try in REPL
minMax([]);
// ⇒ [undefined, undefined]
Try in REPL

Questions

  • How to find the minimum and maximum values of an array?
  • How to get the min/max element of an array?

TypeScript sourceJavaScript source

Checks if the given array contains more than one element.

Type signature

(xs: any) => boolean

Examples

multiple([1, 2, 3]);
// ⇒ true
Try in REPL
multiple([1, 2]);
// ⇒ true
Try in REPL
multiple([1]);
// ⇒ false
Try in REPL
multiple([]);
// ⇒ false
Try in REPL

Questions

  • How to check if an array contains multiple elements?
  • How to check whether multiple values exist within an array?

TypeScript sourceJavaScript source

Checks if the given array is empty.

Type signature

(xs?: any) => boolean

Examples

none([]);
// ⇒ true
Try in REPL
none([1, 2, 3]);
// ⇒ false
Try in REPL

Questions

  • How to check if an array is empty?

TypeScript sourceJavaScript source

Partitions the given array to the ones that pass the given predicate function and the ones that do not. By [convention of the Haskell's Data.Either](http://hackage.haskell.org/package/base-4.12.0.0/docs/Data-Either.html), values that pass the predicate are placed at the right.

Type signature

(predicate: (x: any) => boolean) => (xs: any[]) => any

Examples

partition((x) => x % 2 === 1)([
  1,
  2,
  3,
  4,
  5,
]);
// ⇒ [[2, 4], [1, 3, 5]])
Try in REPL

Questions

  • How to partition an array based on a condition?
  • How to divide an array by a filter function?

TypeScript sourceJavaScript source

Returns the given array without the last element.

Type signature

(xs: any[]) => any[]

Examples

pop([1, 2, 3, 4]); // ⇒ [1, 2, 3]
Try in REPL
pop([]); // ⇒ []
Try in REPL

Questions

  • How to get an array without the last element?
  • How to remove the last element from an array?

TypeScript sourceJavaScript source

Generates an array of numbers from 0 to n - 1.

Type signature

(n: any) => number[]

Examples

range(3);
// ⇒ [0, 1, 2]
Try in REPL

Questions

  • How to create an array of all integers from 0 to N exclusive?

TypeScript sourceJavaScript source

Removes an element at the given index from the given array.

Type signature

(index: number) => (xs: any[]) => any[]

Examples

removeAt(3)([1, 2, 3, 4, 5, 6]);
// ⇒ [1, 2, 3, 5, 6]
Try in REPL

Questions

  • How to remove an item from an array at a particular index?

TypeScript sourceJavaScript source

Repeats the given element by the given count of times.

Type signature

(count: number) => (value: any) => any[]

Examples

repeat(3)("test");
// ⇒ ["test", "test", "test"]
Try in REPL

Questions

  • How to repeat a value N times?

TypeScript sourceJavaScript source

Reverses the given array without mutating it (in contrast to Array.reverse).

Type signature

(xs: any) => any[]

Examples

reverse([1, 2, 3, 4, 5]);
// ⇒ [5, 4, 3, 2, 1]
Try in REPL

Questions

  • How to reverse an array without mutating it?

TypeScript sourceJavaScript source

Reverses the given array when enabled.

Type signature

(enabled: boolean) => (xs: any) => any

Examples

reverseIf(true)([1, 2, 3, 4, 5]);
// ⇒ [5, 4, 3, 2, 1]
Try in REPL
reverseIf(false)([1, 2, 3, 4, 5]);
// ⇒ [1, 2, 3, 4, 5]
Try in REPL

Questions

  • How to reverse an array without mutating it only when a condition is satisfied?

TypeScript sourceJavaScript source

Returns the second element or undefined when there are less than two elements in the given array.

Type signature

(xs: any[]) => any

Examples

second([1, 2, 3, 4, 5]);
// ⇒ 2
Try in REPL
second([1]);
// ⇒ undefined
Try in REPL
second([]);
// ⇒ undefined
Try in REPL

Questions

  • How to get the second element of an array?

TypeScript sourceJavaScript source

Returns the second to last element or undefined when there are less than two elements in the given array.

Type signature

(xs: any[]) => any

Examples

secondToLast([1, 2, 3, 4, 5]);
// ⇒ 4
Try in REPL
secondToLast([1]);
// ⇒ undefined
Try in REPL
secondToLast([]);
// ⇒ undefined
Try in REPL

Questions

  • How to get the second to last element of an array?

TypeScript sourceJavaScript source

Shifts the given array to the left and circulates the elements back by modulo of the array's length.

Type signature

(count: number) => (xs: any[]) => any[]

Examples

shift(1)([1, 2, 3, 4, 5]);
// ⇒ [2, 3, 4, 5, 1]
Try in REPL
shift(2)([1, 2, 3, 4, 5]);
// ⇒ [3, 4, 5, 1, 2]
Try in REPL
shift(3)([1, 2, 3, 4, 5]);
// ⇒ [4, 5, 1, 2, 3]
Try in REPL

Questions

  • How to shift an array?

TypeScript sourceJavaScript source

Shuffles the given array in random order with Math.random as the default.

Type signature

(xs: any, random?: () => number) => any[]

Examples

let i = 0;

const random = () =>
  [
    0.013606630487694282,
    0.21052486239086554,
    0.28299838254636556,
    0.696161009199874,
    0.32165320593537117,
  ][i++];

shuffle([1, 2, 3, 4, 5], random); // => [3, 5, 4, 2, 1]
Try in REPL

Questions

  • How to shuffle an array?

TypeScript sourceJavaScript source

Shuffles the given array in-place in random order with Math.random as the default.

Type signature

(xs: any[], random?: () => number) => any[]

Examples

let i = 0;

const random = () =>
  [
    0.013606630487694282,
    0.21052486239086554,
    0.28299838254636556,
    0.696161009199874,
    0.32165320593537117,
  ][i++];

shuffleInPlace(
  [1, 2, 3, 4, 5],
  random
); // => [3, 5, 4, 2, 1]
Try in REPL

Questions

  • How to shuffle an array in place?

TypeScript sourceJavaScript source

Checks if the given array contains exactly one element.

Type signature

(xs: any[]) => boolean

Examples

single([1]);
// ⇒ true
Try in REPL
single([1, 2, 3]);
// ⇒ false
Try in REPL
single([]);
// ⇒ false
Try in REPL

Questions

  • How to check if an array contains only one element?

TypeScript sourceJavaScript source

Skips the given count of elements from the given array.

Type signature

(count: number) => (xs: any[]) => any[]

Examples

skip(2)([1, 2, 3, 4, 5]);
// ⇒ [3, 4, 5]
Try in REPL

Questions

  • How to skip the first few elements of an array?

TypeScript sourceJavaScript source

Returns a new array composed of tuples of the given sliding window length of consecutive elements.

Type signature

(count: number) => (xs: any[]) => any[][]

Examples

slidingWindow(2)([1, 2, 3, 4]);
// ⇒ [[1, 2], [2, 3], [3, 4]]
Try in REPL
slidingWindow(3)([1, 2, 3, 4, 5]);
// ⇒ [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
Try in REPL
slidingWindow(1)([
  1,
  2,
  3,
  4,
  5,
  6,
]);
// ⇒ [[1], [2], [3], [4], [5], [6]]
Try in REPL

Questions

  • How to iterate an array pairwise?

TypeScript sourceJavaScript source

Sorts the given array without mutating it.

Type signature

(
  f?: (a: any, b: any) => number
) => (xs: any[]) => any[]

Examples

sort((a, b) => a - b)([
  13,
  79,
  20,
  69,
  44,
  67,
  18,
  95,
  26,
  55,
]);
// ⇒ [13, 18, 20, 26, 44, 55, 67, 69, 79, 95]
Try in REPL

Questions

  • How to sort an array without mutating it?

TypeScript sourceJavaScript source

Sums the given array of numbers.

Type signature

(xs: number[]) => number

Examples

sum([1, 2, 3, 4, 5]);
// ⇒ 15
Try in REPL

Questions

  • How to sum elements of an array?

TypeScript sourceJavaScript source

Takes up to a given count of elements.

Type signature

(count: number) => (xs: any[]) => any[]

Examples

take(2)([1, 2, 3, 4, 5]);
// ⇒ [1, 2]
Try in REPL
take(10)([1, 2, 3, 4, 5]);
// ⇒ [1, 2, 3, 4, 5]
Try in REPL

Questions

  • How to get the first N number of elements from an array?

TypeScript sourceJavaScript source

Returns unique elements of the given array.

Type signature

(xs: any[]) => any[]

Examples

unique([1, 2, 3, 4, 3, 4, 3, 6]);
// ⇒ [1, 2, 3, 4, 6]
Try in REPL

Questions

  • How to find all unique values in an array?

TypeScript sourceJavaScript source

Filters out duplicated values based on the result of the given key selector.

Type signature

(f: (x: any) => any) => (xs: any[]) => any[]

Examples

uniqueBy(({ id }) => id)([
  { id: 1, value: "a" },
  { id: 2, value: "b" },
  { id: 1, value: "c" },
]);
// ⇒ [{ id: 1, value: 'c' }, { id: 2, value: 'b' }]
Try in REPL

Questions

  • How to find all unique values in an array by some predicate?

TypeScript sourceJavaScript source

Zips the given arrays together into pairs.

Type signature

(xs: any[], ys: any[]) => any[][]

Examples

zip([1, 2, 3], [4, 5, 6]);
// ⇒ [[1, 4],[2, 5],[3, 6]]
Try in REPL

Questions

  • How to zip two arrays?

TypeScript sourceJavaScript source

Zips the given arrays together into pairs.

Type signature

(...xs: any[][]) => any[][]

Examples

zipN([1, 2, 3], [4, 5, 6]);
// ⇒ [[1, 4], [2, 5], [3, 6]]
Try in REPL
zipN(
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
);
// ⇒ [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Try in REPL
zipN([1, 2], [4, 5, 6], [7, 8, 9]);
// ⇒ [[1, 4, 7],[2, 5, 8]]
Try in REPL

Questions

  • How to zip multiple arrays?

TypeScript sourceJavaScript source

Zips the given arrays together with the given function.

Type signature

(
  f?: (x: any, y: any) => any[]
) => (xs: any[], ys: any[]) => any[][]

Examples

zipWith((x, y) => x * x + y)(
  [1, 2, 3],
  [4, 5, 6]
);
// ⇒ [5, 9, 15]
Try in REPL

Questions

  • How to zip two arrays with a given function?

TypeScript sourceJavaScript source

async

Makes the function run after the given period of not being called. Useful to delay input submission for auto-complete etc.

Type signature

(
  f: (...args: any[]) => any,
  wait: number
) => (...args: any[]) => void

Examples

const f = () => console.log("Test");

const debounced = debounce(f, 2000);

debounced();
setTimeout(debounced, 1000);
setTimeout(debounced, 3000);
Try in REPL

Questions

  • How to make function fire after some time not being called?
  • How to debounce input events?
  • How to debounce a function?

TypeScript sourceJavaScript source

When awaited, delays the execution by the given number of milliseconds.

Type signature

(duration: number) => Promise<unknown>

Examples

delay(2000)(() =>
  console.log("Test")
);
Try in REPL

Questions

  • How to delay a function?
  • What is the JavaScript version of sleep()?

TypeScript sourceJavaScript source

Runs the given tasks in a sequence.

Type signature

(
  tasks: {
    (): Promise<any>;
  }[]
) => Promise<any[]>

Examples

const f = () =>
  new Promise((resolve) =>
    setTimeout(resolve, 1000)
  );
const g = () =>
  new Promise((resolve) =>
    setTimeout(resolve, 2000)
  );

sequence([f, g]).then(() =>
  console.log("Done")
);
Try in REPL

Questions

  • How to run async tasks sequentially?

TypeScript sourceJavaScript source

date

Clamps the given date to the given date range.

Type signature

(min: Date, max: Date) => (date: Date) => Date

Examples

const date = new Date(
  "2019-06-15T13:54:33.232Z"
);
const min = new Date(
  "2019-02-23T13:54:33.232Z"
);
const max = new Date(
  "2019-03-13T13:54:33.232Z"
);

clamp(min, max)(date);
// => new Date("2019-03-13T13:54:33.232Z")
Try in REPL

Questions

  • How to clamp a date to the desired date range?
  • How to enforce a date to be in a given date range?

TypeScript sourceJavaScript source

Clones the given Date object.

Type signature

(date: Date) => Date

Examples

const date = new new Date(
  "2019-04-24T13:54:33.232Z"
)();
const cloned = clone(date);

cloned !== date &&
  cloned.valueOf() ===
    date.valueOf();
// ⇒ true
Try in REPL

Questions

  • How to clone a Date object?

TypeScript sourceJavaScript source

Computes a signed difference between two Date objects as milliseconds.

Type signature

(a: Date, b: Date) => number

Examples

dateDiff(
  new Date(
    "2017-01-01T13:00:00.000Z"
  ),
  new Date(
    "2017-01-01T12:00:00.000Z"
  )
);
// ⇒ 3600000
Try in REPL

Questions

  • How to compute Date difference?

TypeScript sourceJavaScript source

Checks if the given date is between the given date range (inclusive).

Type signature

(from: Date, to: Date) => (date: Date) => boolean

Examples

dateInRange(
  new Date(
    "2018-06-10T12:00:00.000Z"
  ),
  new Date(
    "2018-06-20T12:00:00.000Z"
  )
)(
  new Date(
    "2018-06-15T12:00:00.000Z"
  )
);
// ⇒ true
Try in REPL

Questions

  • How to check if a date is within a given date range?

TypeScript sourceJavaScript source

Returns a local day range at a particular Date.

Type signature

(date: Date) => Date[]

Examples

const date = new Date(
  "2018-12-31T13:54:33.232Z"
);

dayRange(date);
// ⇒ [startOfDay(date), endOfDay(date)]
Try in REPL

Questions

  • How to find a date range of a given day?

TypeScript sourceJavaScript source

Returns an array of days in a particular months. Number of days in February varies if it is a leap year or not.

Type signature

(
  leapYear: boolean
) => [
  number,
  number,
  number,
  number,
  number,
  number,
  number,
  number,
  number,
  number,
  number,
  number
]

Examples

daysInMonths(false);
// ⇒ [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
Try in REPL
daysInMonths(true);
// ⇒ [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
Try in REPL

Questions

  • How to find out how many days are in a particular month?
  • How to find out how many days there are in a leap year?

TypeScript sourceJavaScript source

Calculates the number of days in a particular year. Varies by the leap year.

Type signature

(year: number) => 366 | 365

Examples

daysInYear(2019);
// ⇒ 365
Try in REPL
daysInYear(2020);
// ⇒ 366
Try in REPL

Questions

  • How many days are in a particular year?
  • How many days are in a leap year?
  • How many days are in a common year?

TypeScript sourceJavaScript source

Displays padded time string.

Type signature

(
  source: [number, number, number],
  showSeconds: boolean
) => string

Examples

displayTime([5, 12, 16], false);
// ⇒ 05:12
Try in REPL
displayTime([5, 12, 16], true);
// ⇒ 05:12:16
Try in REPL

Questions

  • How to display padded time?

TypeScript sourceJavaScript source

Returns a local Date of an end of the day at a particular Date.

Type signature

(date: Date) => Date

Examples

endOfDay(
  new Date(
    "2018-12-31T13:54:33.232Z"
  )
);
// ⇒ new Date(new Date("2019-01-01T00:00:00.000Z").valueOf() + new Date("2018-12-31T13:54:33.232Z").getTimezoneOffset() * 60 * 1000)
Try in REPL

Questions

  • How to find a date of an end of a given day?

TypeScript sourceJavaScript source

Formats a given date as a simple YYYY-MM-DD string.

Type signature

(date: Date) => string

Examples

formatDate(
  new Date("2019-02-24T01:12:34")
);
// ⇒ "2019-02-24"
Try in REPL

Questions

  • How to render a date in a YYYY-MM-DD format?

TypeScript sourceJavaScript source

Formats a given date as a simple YYYY-MM-DD HH:MM(:SS) string.

Type signature

(sourceDate: Date, showSeconds?: boolean) => string

Examples

formatDateTime(
  new Date("2019-02-24T01:12:34")
);
// ⇒ "2019-02-24 01:12"
Try in REPL
formatDateTime(
  new Date("2019-02-24T01:12:34"),
  true
);
// ⇒ "2019-02-24 01:12:34"
Try in REPL

Questions

  • How to render a date in a YYYY-MM-DD HH:MM format?
  • How to render a date in a YYYY-MM-DD HH:MM:SS format?

TypeScript sourceJavaScript source

Formats a duration in milliseconds to a padded time string.

Type signature

(duration: number, showSeconds?: boolean) => string

Examples

formatDuration(26100000);
// ⇒ 07:15
Try in REPL
formatDuration(26136000, true);
// ⇒ 07:15:36
Try in REPL

Questions

  • How to render a formatted duration?

TypeScript sourceJavaScript source

Formats a given date as a simple HH:MM(:SS) string.

Type signature

(date: Date, showSeconds?: boolean) => string

Examples

formatTime(
  new Date("2019-02-24T01:12:34")
);
// ⇒ "01:12"
Try in REPL
formatTime(
  new Date("2019-02-24T01:12:34"),
  true
);
// ⇒ "01:12:34"
Try in REPL

Questions

  • How to render a date in a HH:MM format?
  • How to render a date in a HH:MM:SS format?

TypeScript sourceJavaScript source

Converts the given day count to milliseconds.

Type signature

(days: number) => number

Examples

fromDays(1);
// ⇒ 86400000
Try in REPL

Questions

  • How to find how many milliseconds are in a given number of days?

TypeScript sourceJavaScript source

Converts the given hour count to milliseconds.

Type signature

(hours: number) => number

Examples

fromHours(1);
// ⇒ 3600000
Try in REPL

Questions

  • How to find how many milliseconds are in a given number of hours?

TypeScript sourceJavaScript source

Converts the given minute count to milliseconds.

Type signature

(minutes: number) => number

Examples

fromMinutes(1);
// ⇒ 60000
Try in REPL

Questions

  • How to find how many milliseconds are in a given number of minutes?

TypeScript sourceJavaScript source

Converts the given second count to milliseconds.

Type signature

(seconds: number) => number

Examples

fromSeconds(1);
// ⇒ 1000
Try in REPL

Questions

  • How to find how many milliseconds are in a given number of seconds?

TypeScript sourceJavaScript source

Joins a date-time pair into a date-time string.

Type signature

(date: string, time: string) => string

Examples

joinDateTime(
  "2019-01-15",
  "13:54:33.232Z"
);
// ⇒ "2019-01-15T13:54:33.232Z"
Try in REPL

Questions

  • How to join date and time to get ISO-compliant date-time string?

TypeScript sourceJavaScript source

Detects if a given year is a leap year.

Type signature

(year: number) => boolean

Examples

leapYear(2020);
// ⇒ true
Try in REPL
leapYear(2019);
// ⇒ false
Try in REPL

Questions

  • How to find if the given year is a leap year?

TypeScript sourceJavaScript source

Parses HH:MM string into hours and minutes.

Type signature

(text?: string) => [number, number]

Examples

parseHourMinutePair("12:34");
// ⇒ [12, 34]
Try in REPL

Questions

  • How to parse time string into hours and minutes?

TypeScript sourceJavaScript source

Splits a date-time string into a date-time pair.

Type signature

(dateTimeString: string) => [string, string]

Examples

splitDateTime(
  "2019-01-15T13:54:33.232Z"
);
// ⇒ ["2019-01-15", "13:54:33.232Z"]
Try in REPL

Questions

  • How to split ISO-compliant date-time string into a date and time pair?

TypeScript sourceJavaScript source

Returns a local Date of a start of the day at a particular Date.

Type signature

(date: Date) => Date

Examples

endOfDay(
  new Date(
    "2019-01-01T13:54:33.232Z"
  )
);
// ⇒ new Date(new Date("2019-01-01T00:00:00.000Z").valueOf() + new Date("2019-01-01T13:54:33.232Z").getTimezoneOffset() * 60 * 1000)
Try in REPL

Questions

  • How to find a date of the start of a given day?

TypeScript sourceJavaScript source

Subtracts the given number of days from the given Date object.

Type signature

(sourceDate: Date, numberOfDays: number) => Date

Examples

subtractDays(
  new Date(
    "2019-01-15T13:54:33.232Z"
  ),
  1
);
// ⇒ new Date("2019-01-14T13:54:33.232Z")
Try in REPL

Questions

  • How to subtract days from a given date?

TypeScript sourceJavaScript source

Extracts padded YYYY-MM-DD date string out of the given date object.

Type signature

(date: Date) => string

Examples

toDate(
  new Date(
    "2019-01-15T12:00:00.000Z"
  )
);
// ⇒ "2019-01-15"
Try in REPL

Questions

  • How to get only the date from a Date object?

TypeScript sourceJavaScript source

Converts the given array of values into Dates using the Date constructor.

Type signature

(xs: (string | number | Date)[]) => Date[]

Examples

toDates([
  "2019-01-15T13:54:33.232Z",
  new Date(
    "2019-01-15T13:54:33.232Z"
  ).valueOf(),
  new Date(
    "2019-01-15T13:54:33.232Z"
  ),
]);
// ⇒ [new Date("2019-01-15T13:54:33.232Z"), new Date("2019-01-15T13:54:33.232Z"), new Date("2019-01-15T13:54:33.232Z")]
Try in REPL

Questions

  • How to convert an array of string and timestamps into an array of Date objects?

TypeScript sourceJavaScript source

Converts milliseconds into days.

Type signature

(milliseconds: number) => number

Examples

toDays(86400000);
// ⇒ 1
Try in REPL

Questions

  • How to convert milliseconds into days?

TypeScript sourceJavaScript source

Converts milliseconds into hours.

Type signature

(milliseconds: number) => number

Examples

toHours(3600000);
// ⇒ 1
Try in REPL

Questions

  • How to convert milliseconds into hours?

TypeScript sourceJavaScript source

Returns an ISO-compliant date-time string.

Type signature

(x: Date) => string

Examples

toISO(
  new Date(
    "2019-04-24T13:54:33.232Z"
  )
);
// ⇒ "2019-04-24T13:54:33.232Z"
Try in REPL

Questions

  • How to convert Date object to ISO-compliant date string?

TypeScript sourceJavaScript source

Converts milliseconds into minutes.

Type signature

(milliseconds: number) => number

Examples

toMinutes(60000);
// ⇒ 1
Try in REPL

Questions

  • How to convert milliseconds into minutes?

TypeScript sourceJavaScript source

Converts milliseconds into seconds.

Type signature

(milliseconds: number) => number

Examples

toSeconds(1000);
// ⇒ 1
Try in REPL

Questions

  • How to convert milliseconds into seconds?

TypeScript sourceJavaScript source

Checks if the given date is present and it is valid.

Type signature

(date?: any) => boolean

Examples

valid(
  new Date(
    "2020-01-31T09:52:31.618Z"
  )
);
// ⇒ true
Try in REPL
valid(
  new Date("2020-01-42:52:31.618Z")
);
// ⇒ false
Try in REPL
valid(new Date("test"));
// ⇒ false
Try in REPL
valid(undefined);
// ⇒ false
Try in REPL

Questions

  • How to check if a Date is valid or not?

TypeScript sourceJavaScript source

debug

Asserts given conditions.

Type signature

(
  condition: boolean,
  callbackOrMessage:
    | {
        (): void;
      }
    | string
) => void

Examples

assert(true === false);
// ⇒ TypeError("Assertion failed!")
Try in REPL

Questions

  • How to assert a condition?
  • How to throw when a condition is not satisfied?

TypeScript sourceJavaScript source

Computes a deep difference between the two values (primitives, objects, arrays, etc.).

Type signature

(
  obj1?: {
    [index: string]: any;
  },
  obj2?: {
    [index: string]: any;
  }
) => object

Examples

diff({ a: 1 }, { a: 2 });
// ⇒ { a: { data: [1, 2], type: '~' }}
Try in REPL

Questions

  • How to compute a diff?
  • How to compute a deep diff?
  • How to compute a diff between two objects?
  • How to compute a diff between two arrays?

TypeScript sourceJavaScript source

base64url

Decodes the given Base64URL back into a string.

Type signature

(text: string, context?: DecodeContext) => string

Examples

decode("PDw_Pz8-Pg");
// ⇒ "<<???>>"
Try in REPL

Questions

  • How to decode Base64URL?

TypeScript sourceJavaScript source

Decodes the given Base64URL back into a byte array.

Type signature

(text: string, context?: DecodeContext) => number[]

Examples

decodeBytes("w4Jnw6vCp20-bBsQfA");
// ⇒ [0xc2, 0x67, 0xeb, 0xa7, 0x6d, 0x3e, 0x6c, 0x1b, 0x10, 0x7c]
Try in REPL

Questions

  • How to decode Base64URL into a byte array?

TypeScript sourceJavaScript source

Encodes the given string into Base64URL.

Type signature

(text: string, context?: EncodeContext) => string

Examples

encode("<<???>>");
// ⇒ "PDw_Pz8-Pg"
Try in REPL

Questions

  • How to encode a string as Base64URL?

TypeScript sourceJavaScript source

Encodes the given bytes into Base64URL.

Type signature

(bytes: number[], context?: EncodeContext) => string

Examples

encodeBytes([
  0xc2,
  0x67,
  0xeb,
  0xa7,
  0x6d,
  0x3e,
  0x6c,
  0x1b,
  0x10,
  0x7c,
]);
// ⇒ "w4Jnw6vCp20-bBsQfA"
Try in REPL

Questions

  • How to encode bytes as Base64URL?

TypeScript sourceJavaScript source

Converts Base64 string into Base64URL one.

Type signature

(base64: string) => string

Examples

fromBase64("PDw/Pz8+Pg==");
// ⇒ "PDw_Pz8-Pg"
Try in REPL

Questions

  • How to convert Base64 to Base64URL?

TypeScript sourceJavaScript source

Converts Base64URL string into Base64 one.

Type signature

(base64Url: string) => string

Examples

toBase64("PDw_Pz8-Pg");
// ⇒ "PDw/Pz8+Pg=="
Try in REPL

Questions

  • How to convert Base64URL to Base64?

TypeScript sourceJavaScript source

byteString

Converts a string to a byte array.

Type signature

(byteString: string) => number[]

Examples

from("PQR");
// ⇒ [80, 81, 82]
Try in REPL

Questions

  • How to convert a string into a byte array?

TypeScript sourceJavaScript source

Coverts a byte array into a string.

Type signature

(bytes: number[]) => string

Examples

to([0x50, 0x51, 0x52]);
// ⇒ "PQR"
Try in REPL

Questions

  • How to convert a byte array to string?

TypeScript sourceJavaScript source

file

Checks if the given string is a valid Windows file name.

Type signature

(name: string) => boolean

Examples

validName("my:file.png");
// ⇒ false
Try in REPL
validName("file.txt");
// ⇒ true
Try in REPL
validName("../file.txt");
// ⇒ false
Try in REPL
validName("COM1");
// ⇒ false
Try in REPL

Questions

  • How to find valid Windows file name?
  • How to check if a given string is a legal/valid file name under Windows?

TypeScript sourceJavaScript source

function

Composes multiple functions into a higher-order one. Goes right to left.

Type signature

(...fs: ((x: any) => any)[]) => (x: any) => any

Examples

compose(
  (x) => x * x,
  (x) => x + 1
)(3);
// ⇒ 16
Try in REPL

Questions

  • How to compose functions?

TypeScript sourceJavaScript source

Returns the given constant no matter the input.

Type signature

(x: any) => any

Examples

constant(3)("anything");
// ⇒ 3
Try in REPL

Questions

  • How to create a function that always returns the same value despite given arguments?

TypeScript sourceJavaScript source

Always return the given value.

Type signature

(x: any) => any

Examples

identity(5);
// ⇒ 5
Try in REPL
identity("test");
// ⇒ "test"
Try in REPL

Questions

  • How to use the identity function?
  • Where and why is identity function useful?

TypeScript sourceJavaScript source

Memoizes the function result so it is not computed for the same parameters. Uses deep equality.

Type signature

(f: (...xs: any[]) => any) => (...args: any[]) => any

Examples

const f = (x) => {
  console.log(x);
  return x + 1;
};

const memoized = memoize(f);

memoized(5);
memoized(5);
memoized(5);
memoized(3);
Try in REPL

Questions

  • How to memoize a function?

TypeScript sourceJavaScript source

Memoizes the function result so it is not computed for the same parameters. Uses shallow equality.

Type signature

(f: (...xs: any[]) => any) => (...args: any[]) => any

Examples

const f = ({ x }) => {
  console.log(x);
  return x + 1;
};

const memoized = memoizeShallow(f);

memoized({ x: 5 });
memoized({ x: 5 });
memoized({ x: 5 });
memoized({ x: 3 });
Try in REPL

Questions

  • How to memoize a function with shallow equality?

TypeScript sourceJavaScript source

Memoizes the function result so it is not computed for the same parameters. Uses the given equality function.

Type signature

(
  equals: (x: any, y: any) => boolean
) => (f: (...xs: any[]) => any) => (...args: any[]) => any

Examples

const f = ({ x }) => {
  console.log(x);
  return x + 1;
};

const memoized = memoizeWith(
  (a, b) => a.x === b.x
)(f);

memoized({ x: 5 });
memoized({ x: 5 });
memoized({ x: 5 });
memoized({ x: 3 });
Try in REPL

Questions

  • How to memoize a function with a custom equality function?

TypeScript sourceJavaScript source

It does exactly nothing.

Type signature

() => void

Examples

noOp("anything");
// ⇒ undefined
Try in REPL

Questions

  • How to create a function that does nothing?

TypeScript sourceJavaScript source

Inverts the given function result.

Type signature

(
  f: (...xs: any[]) => any
) => (...args: any[]) => boolean

Examples

not(x > 10)(15);
// ⇒ true
Try in REPL

Questions

  • How to invert a boolean function?

TypeScript sourceJavaScript source

Pipes an input through given functions.

Type signature

(...fs: ((x: any) => any)[]) => (x: any) => any

Examples

pipe(
  (x) => x * x,
  (x) => x + 1
)(3);
// ⇒ 10
Try in REPL

Questions

  • How to pipe an argument through a function?

TypeScript sourceJavaScript source

Runs the given function only when the condition is met.

Type signature

(
  predicate: (...xs: any[]) => boolean
) => (action: (...xs: any[]) => any) => (...args: any[]) => any

Examples

when((x) => x > 0)((x) =>
  console.log(x)
)(5);
when((x) => x > 0)((x) =>
  console.log(x)
)(-3);
Try in REPL

Questions

  • How to run a function only when a condition is satisfied?

TypeScript sourceJavaScript source

Runs the given function only when the condition is exactly true.

Type signature

(
  action: (...xs: any[]) => any
) => (...args: any[]) => any

Examples

whenTrue((x) => console.log(x))(
  false
);
when((x) => x > 0)((x) =>
  console.log(x)
)(true);
Try in REPL

Questions

  • How to run a function only if its argument is true?
  • How to execute function only if a variable is true?

TypeScript sourceJavaScript source

is

Checks if the given argument is an array.

Type signature

(x?: any) => boolean

Examples

array([1, 2, 3]);
// ⇒ true
Try in REPL
array({ a: 1 });
// ⇒ false
Try in REPL

Questions

  • How to check if a given value is an array?

TypeScript sourceJavaScript source

Checks if the given value is a boolean.

Type signature

(x?: any) => boolean

Examples

boolean(false); // ⇒ true
Try in REPL
boolean(1); // ⇒ false
Try in REPL

Questions

  • How to check if a given value is a boolean?

TypeScript sourceJavaScript source

Checks if the given value is a byte.

Type signature

(x?: number) => boolean

Examples

byte(128);
// ⇒ true
Try in REPL
byte(325);
// ⇒ false
Try in REPL
byte(65.5);
// ⇒ false
Try in REPL

Questions

  • How to check if a given value is a byte?
  • How to check if a given number is a byte?

TypeScript sourceJavaScript source

Checks if the given value is a Date object.

Type signature

(x?: any) => boolean

Examples

date(new Date());
// ⇒ true
Try in REPL
date(123);
// ⇒ false
Try in REPL

Questions

  • How to check if a given value is a Date object?

TypeScript sourceJavaScript source

Checks if the given value is defined.

Type signature

(x?: any) => boolean

Examples

defined(undefined);
// ⇒ false
Try in REPL
defined(null);
// ⇒ true
Try in REPL
defined(0);
// ⇒ true
Try in REPL
defined({ a: 1 });
// ⇒ true
Try in REPL

Questions

  • How to check if a given value is defined?
  • How to check if a given value is not undefined?

TypeScript sourceJavaScript source

Checks if the given value is a function.

Type signature

(x?: any) => boolean

Examples

_function((x) => x + 5);
// ⇒ true
Try in REPL

Questions

  • How to check if a given value is a function?

TypeScript sourceJavaScript source

Checks if the given value is an integer.

Type signature

(x?: number) => boolean

Examples

integer(5);
// ⇒ true
Try in REPL
integer(32.5);
// ⇒ false
Try in REPL

Questions

  • How to check if a given value is an integer?
  • How to check if a given number is an integer?

TypeScript sourceJavaScript source

Checks and asserts the given value is not null or undefined.

Type signature

<T>(val: T) => val is NonNullable<T>

Examples

nonNullable(null);
// ⇒ false
Try in REPL
nonNullable(undefined);
// ⇒ false
Try in REPL
nonNullable(false);
// ⇒ true
Try in REPL
nonNullable({ a: 1 });
// ⇒ true
Try in REPL

Questions

  • How to check if a given value is non-nullable?
  • How to check if a given value is not null?
  • How to check if a given value is not undefined?

TypeScript sourceJavaScript source

Checks if the given value is a number in a normal range [0, 1].

Type signature

(x?: number) => boolean

Examples

normal(0.75);
// ⇒ true
Try in REPL
normal(-1);
// ⇒ false
Try in REPL
normal(2.5);
// ⇒ false
Try in REPL

Questions

  • How to check if a given value is in 0 to 1 inclusive range?

TypeScript sourceJavaScript source

Checks if the given value is a number.

Type signature

(x?: any) => boolean

Examples

number(0 / 0);
// ⇒ false
Try in REPL
number(15.6);
// ⇒ true
Try in REPL

Questions

  • How to check if a given value is a valid number?
  • How to check if a given value is not NaN?
  • How to check if a given value is finite?

TypeScript sourceJavaScript source

Checks if the given value is an object.

Type signature

(x?: any) => boolean

Examples

object({ a: 1, b: 2 });
// ⇒ true
Try in REPL
object([1, 2, 3]);
// ⇒ false
Try in REPL

Questions

  • How to check if a given value is an object?

TypeScript sourceJavaScript source

Checks if the given value is a string.

Type signature

(x?: any) => boolean

Examples

string("Test");
// ⇒ true
Try in REPL
string(["T", "e", "s", "t"]);
// ⇒ false
Try in REPL

Questions

  • How to check if a given value is a string?

TypeScript sourceJavaScript source

math

Adds two values.

Type signature

(a: number, b: number) => number

Examples

add(3, 5);
// ⇒ 8
Try in REPL

Questions

  • How to add two values?

TypeScript sourceJavaScript source

Calculates the average of the given array of numbers.

Type signature

(xs?: number[]) => number

Examples

average([2, 4, 15]);
// ⇒ 7
Try in REPL

Questions

  • How to compute the average of an array?

TypeScript sourceJavaScript source

Finds the nearest power of two greater or equal to the given value.

Type signature

(x: number) => number

Examples

ceilToNearestPowerOfTwo(345);
// ⇒ 512
Try in REPL

Questions

  • How to get the nearest power of two greater or equal to the given value?

TypeScript sourceJavaScript source

Clamps the given value to the given range.

Type signature

(min: number, max: number) => (x: number) => number

Examples

clamp(0, 10)(5);
// ⇒ 5
Try in REPL
clamp(0, 10)(-5);
// ⇒ 0
Try in REPL
clamp(0, 10)(15);
// ⇒ 10
Try in REPL

Questions

  • How to clamp value to the desired range?
  • How to enforce a value to be in a given range?

TypeScript sourceJavaScript source

Clamps the given value to the [0, 1] range.

Type signature

(x: number) => number

Examples

clampNormal(0.5);
// ⇒ 0.5
Try in REPL
clampNormal(-0.5);
// ⇒ 0
Try in REPL
clampNormal(1.5);
// ⇒ 1
Try in REPL

Questions

  • How to clamp value to be in 0 to 1 inclusive range?
  • How to clamp value to be in the normal range?

TypeScript sourceJavaScript source

Clamps the given value to the [0, 100] range.

Type signature

(x: number) => number

Examples

clampPercentage(50);
// ⇒ 50
Try in REPL
clampPercentage(-50);
// ⇒ 0
Try in REPL
clampPercentage(150);
// ⇒ 100
Try in REPL

Questions

  • How to enforce a percentage be between 0% and 100%?

TypeScript sourceJavaScript source

Calculates the absolute distance between given values.

Type signature

(a: number, b: number) => number

Examples

delta(-3, 5);
// ⇒ 8
Try in REPL

Questions

  • How to calculate an absolute distance between two numbers?

TypeScript sourceJavaScript source

Checks if the given value is in the rectangular range of [0, width] and [0, height]

Type signature

(
  width: number,
  height: number
) => (x: number, y: number) => boolean

Examples

inRectangleRange(50, 100)(25, 50);
// ⇒ true
Try in REPL
inRectangleRange(50, 100)(-25, 50);
// ⇒ false
Try in REPL

Questions

  • How to check if a point is inside a rectangle defined by width and height?

TypeScript sourceJavaScript source

Linearly interpolates two given values by the normal value of their distance.

Type signature

(t: number) => (a: number, b: number) => number

Examples

lerp(0.5)(0, 10);
// ⇒ 5
Try in REPL
lerp(0)(0, 10);
// ⇒ 0
Try in REPL
lerp(1)(0, 10);
// ⇒ 10
Try in REPL

Questions

  • How to linearly interpolate between two values?
  • How to interpolate two numbers?

TypeScript sourceJavaScript source

Calculates the maximum by a given selector.

Type signature

(f: (x: number) => number) => (xs: number[]) => number

Examples

maximumBy(({ age }) => age)([
  { age: 13 },
  { age: 20 },
  { age: 7 },
  { age: 18 },
]);
// ⇒ { age: 20 }
Try in REPL

Questions

  • How to find a maximum element by a given function?

TypeScript sourceJavaScript source

Calculates the median of the values. If there is an even number of items, the average of the middle ones is returned.

Type signature

(xs?: number[]) => number | undefined

Examples

median([-5, 3, 2, 29, 43]);
// ⇒ 3
Try in REPL

Questions

  • How to compute a median of an array?

TypeScript sourceJavaScript source

Calculates the minimum and maximum value of the two given values.

Type signature

([a, b]: [number, number]) => [number, number]

Examples

minMax([5, 3]);
// ⇒ [3, 5]
Try in REPL
minMax([3, 5]);
// ⇒ [3, 5]
Try in REPL

Questions

  • How to get ordered values where the lower is the first and the higher is the second?

TypeScript sourceJavaScript source

Checks if all the given values have the same sign.

Type signature

(xs: number[]) => boolean

Examples

sameSign([-1, -2, -3]);
// ⇒ true
Try in REPL
sameSign([1, 2, -3]);
// ⇒ false
Try in REPL

Questions

  • How to check if all values have the same sign?

TypeScript sourceJavaScript source

Calculates the sign of the value and returns -1 for negative values, 1 for positive values and 0 for zeros.

Type signature

(x: number) => number

Examples

sign(3);
// ⇒ 1
Try in REPL
sign(-5);
// ⇒ 5
Try in REPL
sign(0);
// ⇒ 0
Try in REPL
sign(-0);
// ⇒ 0
Try in REPL

Questions

  • How to get a sign of a number?

TypeScript sourceJavaScript source

Calculates the standard deviation of the given array of numbers.

Type signature

(xs: number[], origin?: number) => number

Examples

standardDeviation([
  96,
  81,
  68,
  79,
  23,
  13,
  13,
  59,
  44,
  86,
]);
// ⇒ (2 * Math.sqrt(10922 / 5)) / 3
Try in REPL

Questions

  • How to compute a standard deviation of an array?

TypeScript sourceJavaScript source

Subtracts two values.

Type signature

(a: number, b: number) => number

Examples

subtract(3, 5);
// ⇒ -2
Try in REPL

Questions

  • How to subtract two numbers?

TypeScript sourceJavaScript source

object

Checks if the given object is present and it is not empty (contains at least one entry).

Type signature

(xs?: object) => boolean

Examples

any({ a: 1, b: 2, c: 3 });
// ⇒ true
Try in REPL
any({});
// ⇒ false
Try in REPL
any(null);
// ⇒ false
Try in REPL
any(undefined);
// ⇒ false
Try in REPL

Questions

  • How to check if an object is not empty?
  • How to check if an object contains some values?
  • How to check if an object is not null or undefined?

TypeScript sourceJavaScript source

Applies the given parameters to the given dictionary of functions.

Type signature

(
  fs: ((...xs: any[]) => any)[]
) => (...xs: any[]) => object

Examples

const lower = (text) =>
  text.toLowerCase();
const upper = (text) =>
  text.toUpperCase();

apply({ lower, upper })("TeSt");
// ⇒ { lower: "test", upper: "TEST" }
Try in REPL

Questions

  • How to apply a value over an object of functions?

TypeScript sourceJavaScript source

Empty object.

Type signature

{}

Examples

empty;
// ⇒ {}
Try in REPL

Questions

  • How to get an empty object?

TypeScript sourceJavaScript source

Lists key-value pairs (entries) present in the given object.

Type signature

{
  <T>(
    o:
      | {
          [s: string]: T;
        }
      | ArrayLike<T>
  ): [string, T][];
  (o: {}): [string, any][];
}

Examples

entries({ a: 1, b: 2, c: 3 });
// ⇒ [["a", 1], ["b", 2], ["c", 3]]
Try in REPL

Questions

  • How to get entries of an object?
  • How to get an array of key-value pairs of an object?

TypeScript sourceJavaScript source

Creates a 1 to 1 mapping of the given values as an object.

Type signature

(...xs: string[]) => object

Examples

enumerable("TEST", "X", "Y");
// ⇒ { TEST: 'TEST', X: 'X', Y: 'Y' }
Try in REPL

Questions

  • How to create an object of the same keys and values?

TypeScript sourceJavaScript source

Checks if two objects are deeply equal.

Type signature

(a: any, b: any) => boolean

Examples

equals({ a: 1 }, { a: 1 });
// ⇒ true
Try in REPL
equals(
  { b: [1, 2] },
  { b: [1, 2] }
);
// ⇒ true
Try in REPL

Questions

  • How to check if two objects are equal?
  • How to check deep object equality?

TypeScript sourceJavaScript source

Test if every element passes the given predicate.

Type signature

(
  f: (value: any, key: string, context: object) => boolean
) => (xs: object) => boolean

Examples

every((x) => x >= 0)({
  x: 5,
  y: 3,
  z: 0,
});
// ⇒ true
Try in REPL
every((x) => x > 0)({
  x: 5,
  y: 3,
  z: 0,
});
// ⇒ false
Try in REPL

Questions

  • How to check if every entry in an object passes a given predicate?

TypeScript sourceJavaScript source

Filters the given object with the given predicate.

Type signature

(
  f: (value: any, key: string, context: object) => boolean
) => (xs: object) => object

Examples

filter((x) => x % 2 !== 0)({
  a: 1,
  b: 2,
  c: 3,
});
// ⇒ { a: 1, c: 3 }
Try in REPL

Questions

  • How to filter an object?

TypeScript sourceJavaScript source

Searches the given object by the given predicate and returns the found value or undefined.

Type signature

(
  predicate: (value: any, key: string, context: object) => boolean
) => (xs: object) => any

Examples

find(({ x }) => x % 2 === 0)({
  a: { x: 1 },
  b: { x: 2 },
  c: { x: 3 },
});
// ⇒ { x: 2 }
Try in REPL

Questions

  • How to find the value of an object by a predicate function?

TypeScript sourceJavaScript source

Searches the given object by the given predicate and returns the found entry or undefined.

Type signature

(
  predicate: (value: any, key: string, context: object) => boolean
) => (xs: object) => any

Examples

findEntry(({ x }) => x % 2 === 0)({
  a: { x: 1 },
  b: { x: 2 },
  c: { x: 3 },
});
// ⇒ ["b", { x: 2 }]
Try in REPL

Questions

  • How to find an entry of an object by a predicate function?

TypeScript sourceJavaScript source

Searches the given object by the given predicate and returns the found key or undefined.

Type signature

(
  predicate: (value: any, key: string, context: object) => boolean
) => (xs: object) => any

Examples

findKey(({ x }) => x % 2 === 0)({
  a: { x: 1 },
  b: { x: 2 },
  c: { x: 3 },
});
// ⇒ "b"
Try in REPL

Questions

  • How to find a key of an object by a predicate function?

TypeScript sourceJavaScript source

Returns the first value in the given object. Follows the default object iteration order.

Type signature

(xs: object) => any

Examples

first({ a: 1, b: 2, c: 3 });
// ⇒ 1
Try in REPL

Questions

  • How to get the first value of an object?

TypeScript sourceJavaScript source

Flat maps the values of the given object.

Type signature

(
  f: (value: any, key: string, context: object) => any
) => (xs: object) => any[]

Examples

flatMapValues((x) => [x, x * 2])({
  a: 1,
  b: 2,
  c: 3,
});
// ⇒ [1, 2, 2, 4, 3, 6]
Try in REPL

Questions

  • How to flat map an object?

TypeScript sourceJavaScript source

Creates an object from an array of key-value pairs (entries).

Type signature

(entries: [string, any][]) => object

Examples

fromEntries([
  ["a", 1],
  ["b", 2],
  ["c", 3],
]);
// ⇒ { a: 1, b: 2, c: 3 }
Try in REPL

Questions

  • How to create an object from an array of key-value pairs?
  • How to create an object from an array of entries?

TypeScript sourceJavaScript source

Groups the given array of values by the given key selector.

Type signature

(selector: (x: any) => string) => (xs: any[]) => object

Examples

groupBy((x) =>
  x % 2 == 0 ? "even" : "odd"
)([1, 2, 3, 4, 5, 6, 7]);
// ⇒ { even: [2, 4, 6], odd: [1, 3, 5, 7] }
Try in REPL

Questions

  • How to group an array by a key function?

TypeScript sourceJavaScript source

Checks if the given key is present in the object.

Type signature

(key: string) => (xs?: any) => any

Examples

hasKey("c")({ a: 1, b: 2, c: 3 });
// ⇒ true
Try in REPL

Questions

  • How to check if an object contains a given key?

TypeScript sourceJavaScript source

Returns the number of entries within the given object.

Type signature

(xs: object) => number

Examples

length({ a: 1, b: 2, c: 3 });
// ⇒ 3
Try in REPL

Questions

  • How to check how many entries are in an object?

TypeScript sourceJavaScript source

Maps the given object with the given function.

Type signature

(
  f: (value: any, key: string, context: object) => any
) => (xs: object) => object

Examples

map((x) => x ** 2)({
  a: 1,
  b: 2,
  c: 3,
});
// ⇒ { a: 1, b: 4, c: 9 }
Try in REPL

Questions

  • How to map an object?
  • How to transform an object?

TypeScript sourceJavaScript source

Maps entries of the given object.

Type signature

(
  f: (value: any, key: string, context: object) => any
) => (xs: object) => [string, any][]

Examples

mapEntries((x) => x ** 2)({
  a: 1,
  b: 2,
  c: 3,
});
// ⇒ [["a", 1], ["b", 4], ["c", 9]]
Try in REPL

Questions

  • How to map object entries?

TypeScript sourceJavaScript source

Transforms the object keys with the given function.

Type signature

(
  f: (value: any, key: string, context: object) => any
) => (xs: object) => object

Examples

mapKeys((_, key) =>
  key.toUpperCase()
)({ a: 1, b: 2, c: 3 });
// ⇒ { A: 1, B: 2, C: 3 }
Try in REPL

Questions

  • How to map object keys?
  • How to transform object keys?

TypeScript sourceJavaScript source

Maps and returns an array of transformed object values.

Type signature

(
  f: (value: any, key: string, context: object) => any
) => (xs: object) => any[]

Examples

mapValues((x) => x ** 2)({
  a: 1,
  b: 2,
  c: 3,
});
// ⇒ [1, 4, 9]
Try in REPL

Questions

  • How to map object values?

TypeScript sourceJavaScript source

Merges two objects deeply.

Type signature

(
  a: {
    [index: string]: any;
  },
  b: object
) => object

Examples

merge({ a: 1, b: 3 }, {});
// ⇒ { a: 1, b: 3 }
Try in REPL
merge({ a: 1, b: 3 }, { b: 7 });
// ⇒ { a: 1, b: 7 }
Try in REPL
merge(
  { a: 1, b: 3 },
  { b: { d: 8 } }
);
// ⇒ { a: 1, b: { d: 8 } }
Try in REPL
merge(
  { a: 1, b: { c: 3 } },
  { b: { d: 8 } }
);
// ⇒ { a: 1, b: { c: 3, d: 8 } }
Try in REPL

Questions

  • How to merge two objects together?
  • How to deeply merge two objects?

TypeScript sourceJavaScript source

Checks if the given object is empty.

Type signature

(xs?: object) => boolean

Examples

none({});
// ⇒ true
Try in REPL
none(null);
// ⇒ true
Try in REPL
none({ a: 1 });
// ⇒ false
Try in REPL

Questions

  • How to check if an object is empty?
  • How to check if an object is null or undefined?

TypeScript sourceJavaScript source

Test if any element passes the given predicate.

Type signature

(
  f: (value: any, key: string, context: object) => boolean
) => (xs: object) => boolean

Examples

some((x) => x >= 4)({
  x: 5,
  y: 3,
  z: 0,
});
// ⇒ true
Try in REPL
some((x) => x < 0)({
  x: 5,
  y: 3,
  z: 0,
});
// ⇒ false
Try in REPL

Questions

  • How to check if any entry in an object passes a given predicate?

TypeScript sourceJavaScript source

Sorts the given object by a comparator.

Type signature

(
  f: (a: any, b: any) => number
) => (xs: object) => object

Examples

sort({
  a: 3,
  b: 2,
  c: 3,
  d: -7,
  e: 13,
  f: 0,
  g: 8,
});
// ⇒ {"d": -7,"f": 0,"b": 2,"a": 3,"c": 3,"g": 8,"e": 13}
Try in REPL

Questions

  • How to sort an object?

TypeScript sourceJavaScript source

query

Parses a query string into an object.

Type signature

(xs?: string) => object

Examples

parse("test&count=5");
// ⇒ { test: true, count: "5" }
Try in REPL

Questions

  • How to parse a query string?

TypeScript sourceJavaScript source

Parses the given query string into an object using URLSearchParams.

Type signature

(source: string) => {}

Examples

read("test&count=5");
// ⇒ { test: "", count: "5" }
Try in REPL

Questions

  • How to parse a query string using URLSearchParams?

TypeScript sourceJavaScript source

Serializes the given object into a query string.

Type signature

(xs?: { [index: string]: any }) => string

Examples

serialize({
  test: true,
  value: "a string with spaces",
  missing: false,
});
// ⇒ "test&value=a%20string%20with%20spaces"
Try in REPL

Questions

  • How to serialize an object to a query string?

TypeScript sourceJavaScript source

range

Checks if the given range is empty.

Type signature

([min, max]: [number, number]) => boolean

Examples

empty([2, 2]);
// ⇒ true
Try in REPL
empty([1, 5]);
// ⇒ false
Try in REPL

Questions

  • How to check if a given range is empty (0-length)?

TypeScript sourceJavaScript source

Checks if the given ranges are equal.

Type signature

(
  [a, b]: [number, number],
  [c, d]: [number, number]
) => boolean

Examples

equals([1, 2], [1, 2]);
// ⇒ true
Try in REPL
equals([4, 3], [1, 2]);
// ⇒ false
Try in REPL

Questions

  • How to check if two ranges are equal?

TypeScript sourceJavaScript source

Computes the signed length of the given range.

Type signature

([min, max]: [number, number]) => number

Examples

length([3, 15]);
// ⇒ 12
Try in REPL
length([1, 0]);
// ⇒ -1
Try in REPL

Questions

  • How to compute a signed length of a range?

TypeScript sourceJavaScript source

Splits the given range into subranges by excluding the given used ranged.

Type signature

(
  used: [number, number][],
  sourceRange?: number[]
) => (range: [number, number]) => [number, number][]

Examples

split([
  [2, 3],
  [5, 7],
]);
// ⇒ [[0, 2], [3, 5], [7, 10]]
Try in REPL

Questions

  • How to split a range into subranges?

TypeScript sourceJavaScript source

regex

Escapes regex string into proper regex.

Type signature

(string: string) => string

Examples

escape("te.t").test("text");
// ⇒ false
Try in REPL

Questions

  • How to properly escape a regex string?

TypeScript sourceJavaScript source

string

Checks if the given string contains whitespace.

Type signature

(x: string) => boolean

Examples

containsWhitespace("test string");
// ⇒ true
Try in REPL
containsWhitespace("test");
// ⇒ false
Try in REPL

Questions

  • How to check if a string contains whitespace?

TypeScript sourceJavaScript source

Empty string.

Type signature

""

Examples

empty();
// ⇒ ""
Try in REPL

Questions

  • How to get an empty string?

TypeScript sourceJavaScript source

Transforms the first character to lowercase.

Type signature

(text: string) => string

Examples

firstToLower("TeSt");
// ⇒ "teSt"
Try in REPL

Questions

  • How to make the first letter of a string lowercase?

TypeScript sourceJavaScript source

Transforms the first character to uppercase.

Type signature

(text: string) => string

Examples

firstToUpper("teSt");
// ⇒ "TeSt"
Try in REPL

Questions

  • How to make the first letter of a string uppercase?

TypeScript sourceJavaScript source

Checks if the given substring is present in the source string.

Type signature

(search: string) => (text: string) => boolean

Examples

includes("brown fox")(
  "The quick brown fox jumps over the lazy dog"
);
// ⇒ true
Try in REPL
includes("brown dog")(
  "The quick brown fox jumps over the lazy dog"
);
// ⇒ false
Try in REPL

Questions

  • How to check if a string contains a given substring?

TypeScript sourceJavaScript source

Non-breaking space.

Type signature

" "

Examples

nbsp;
// ⇒ " "
Try in REPL

Questions

  • How to get a non-breaking space?

TypeScript sourceJavaScript source

Checks if the given string is present and is not empty or all whitespace.

Type signature

(x?: string) => boolean

Examples

nonEmpty("test with spaces");
// ⇒ true
Try in REPL
nonEmpty("   ");
// ⇒ false
Try in REPL
nonEmpty(null);
// ⇒ false
Try in REPL

Questions

  • How to check if a string is non-empty?
  • How to check if a string is not all whitespace?
  • How to check if a string is not null or undefined?

TypeScript sourceJavaScript source

Checks if the given string starts with the given substring.

Type signature

(prefix: string) => (xs: string) => boolean

Examples

startsWith("The")(
  "The quick brown fox jumps over the lazy dog"
);
// ⇒ true
Try in REPL
startsWith("Quick")(
  "The quick brown fox jumps over the lazy dog"
);
// ⇒ false
Try in REPL

Questions

  • How to check if a string starts with a given substring?

TypeScript sourceJavaScript source

vector2

Adds two vectors.

Type signature

(
  [x1, y1]: [number, number],
  [x2, y2]: [number, number]
) => [number, number]

Examples

add([3, 5], [-1, 8]);
// ⇒ [2, 13]
Try in REPL

Questions

  • How to add two vectors?

TypeScript sourceJavaScript source

Applies transformations to the given vector.

Type signature

(space: {
  a: number;
  c: number;
  e: number;
  b: number;
  d: number;
  f: number;
}) => ([x, y]: [number, number]) => number[]

Examples

convertSpace(rotate(Math.PI))([
  2,
  3,
]);
// ⇒ [-2, -3]
Try in REPL

Questions

  • How to transform a vector with a matrix?

TypeScript sourceJavaScript source

Calculates a cross product of the given vectors. Returns a scalar.

Type signature

(
  [a, b]: [number, number],
  [c, d]: [number, number]
) => number

Examples

cross([3, 5], [-1, 8]);
// ⇒ 29
Try in REPL
cross([3, 5], [-1, -8]);
// ⇒ -19
Try in REPL

Questions

  • How to compute a cross product of two vectors?
  • How to check on which side of a line a point is?

TypeScript sourceJavaScript source

Calculates a dot product of the given vectors. Returns a scalar.

Type signature

(
  [a, b]: [number, number],
  [c, d]: [number, number]
) => number

Examples

dot([3, 5], [-1, 8]);
// ⇒ 37
Try in REPL
dot([3, 5], [-1, -8]);
// ⇒ -43
Try in REPL

Questions

  • How to compute a dot product of two vectors?

TypeScript sourceJavaScript source

Calculates the length/magnitude of the given vector.

Type signature

([x, y]: [number, number]) => number

Examples

length([3, 5]);
// ⇒ Math.sqrt(3 * 3 + 5 * 5)
Try in REPL

Questions

  • How to compute the length of a vector?
  • How to compute the magnitude of a vector?

TypeScript sourceJavaScript source

Applies matrix transformation to the given vector.

Type signature

(
  {
    a,
    b,
    c,
    d,
    e,
    f
  }: {
    a: number;
    c: number;
    e: number;
    b: number;
    d: number;
    f: number;
  },
  [x, y]: [number, number]
) => number[]

Examples

mul(scale(4, 5), [2, 3]);
// ⇒ [8, 15]
Try in REPL

Questions

  • How to apply a matrix transformation to a vector?

TypeScript sourceJavaScript source

Multiples two matrices.

Type signature

(
  m1: {
    a: number;
    c: number;
    e: number;
    b: number;
    d: number;
    f: number;
  },
  m2: {
    a: number;
    b: number;
    c: number;
    d: number;
    e: number;
    f: number;
  }
) => {
  a: number;
  c: number;
  e: number;
  b: number;
  d: number;
  f: number;
}

Examples

multiply(
  {
    a: 1,
    c: 2,
    e: 3,
    b: 4,
    d: 5,
    f: 6,
  },
  {
    a: 7,
    c: 8,
    e: 9,
    b: 10,
    d: 11,
    f: 12,
  }
);
// ⇒ { a: 27, b: 78, c: 30, d: 87, e: 36, f: 102 }
Try in REPL

Questions

  • How to multiply two matrices?

TypeScript sourceJavaScript source

Normalizes the given vector. Returns [0, 0] vector for points.

Type signature

(vector: [number, number]) => [number, number]

Examples

normalize([2, 3]);
// ⇒ [2 / length([2, 3]), 3 / length([2, 3])]
Try in REPL

Questions

  • How to normalize a vector?

TypeScript sourceJavaScript source

Reflects the given vector on the given surface.

Type signature

(
  a: [number, number],
  v: [number, number]
) => [number, number]

Examples

reflect([1, -2], [1, 0]);
// ⇒ [0.6, 0.8]
Try in REPL

Questions

  • How to reflect a vector on another vector?

TypeScript sourceJavaScript source

Creates a rotation matrix around given origin [0, 0] by default.

Type signature

(
  angle?: number,
  cx?: number,
  cy?: number
) => {
  a: number;
  c: number;
  e: number;
  b: number;
  d: number;
  f: number;
}

Examples

const angle = Math.PI;
const sine = Math.sin(angle);
const cosine = Math.cos(angle);

rotate(angle); // { a: cosine, b: sine, c: -sine, d: cosine, e: 0, f: 0 }
Try in REPL

Questions

  • How to create a rotation matrix?

TypeScript sourceJavaScript source

Creates a scale matrix.

Type signature

(
  sx?: number,
  sy?: number
) => {
  a: number;
  c: number;
  e: number;
  b: number;
  d: number;
  f: number;
}

Examples

scale(2, 3);
// ⇒ { a: 2, b: 0, c: 0, d: 3, e: 0, f: 0 }
Try in REPL

Questions

  • How to create a scale matrix?

TypeScript sourceJavaScript source

Subtracts two vectors.

Type signature

(
  [x1, y1]: [number, number],
  [x2, y2]: [number, number]
) => [number, number]

Examples

sub([3, 5], [-1, 8]);
// ⇒ [4, -3]
Try in REPL

Questions

  • How to subtract two vectors?

TypeScript sourceJavaScript source

Composes a single transformation by matrix multiplication.

Type signature

(
  ...matrices: {
    a: number;
    c: number;
    e: number;
    b: number;
    d: number;
    f: number;
  }[]
) => {
  a: number;
  c: number;
  e: number;
  b: number;
  d: number;
  f: number;
}

Examples

const originX = 5;
const originY = 6;
const angle = Math.PI;

transform(
  translate(originX, originY),
  rotate(angle),
  translate(-originX, -originY)
);
// ⇒ rotate(Math.PI, originX, originY)
Try in REPL

Questions

  • How to compose multiple matrix transformations into a single matrix?

TypeScript sourceJavaScript source

Creates a translation matrix.

Type signature

(
  tx?: number,
  ty?: number
) => {
  a: number;
  c: number;
  e: number;
  b: number;
  d: number;
  f: number;
}

Examples

translate(2, 3);
// ⇒ { a: 1, b: 0, c: 0, d: 1, e: 2, f: 3 }
Try in REPL

Questions

  • How to create a translation matrix?

TypeScript sourceJavaScript source

web

Composes class name from truthy values with the support of string and objects.

Type signature

(...xs: any[]) => any

Examples

classNames("test", {
  active: true,
  disabled: false,
  on: undefined,
});
// ⇒ "test active"
Try in REPL

Questions

  • How to create a class name from an array of strings and/or objects?

TypeScript sourceJavaScript source

events

Stops propagation and prevents the default handler of the given event.

Type signature

(event: {
  preventDefault: () => void;
  stopPropagation: () => void;
}) => boolean

Examples

const event = {
  preventDefault: () =>
    console.log("preventDefault"),
  stopPropagation: () =>
    console.log("stopPropagation"),
};

cancel(event);
Try in REPL

Questions

  • How to stop event propagation and prevent default at once?

TypeScript sourceJavaScript source

Tests if the current event seems like an intent to open a new tab. Useful for client-side navigation handling.

Type signature

({
  button,
  ctrlKey,
  metaKey,
  shiftKey
}: {
  button?: number;
  ctrlKey?: boolean;
  metaKey?: boolean;
  shiftKey?: boolean;
}) => boolean

Examples

openInNewTabIntent({
  ctrlKey: true,
});
// ⇒ true
Try in REPL

Questions

  • How to check if the user wants to open a new tab using history API?

TypeScript sourceJavaScript source

Prevents the default handler of the given event.

Type signature

(event: { preventDefault: () => void }) => boolean

Examples

const event = {
  preventDefault: () =>
    console.log("preventDefault"),
  stopPropagation: () =>
    console.log("stopPropagation"),
};

prevent(event);
Try in REPL

Questions

  • How to prevent default event handler?

TypeScript sourceJavaScript source

Stops propagation of the given event.

Type signature

(event: { stopPropagation: () => void }) => boolean

Examples

const event = {
  preventDefault: () =>
    console.log("preventDefault"),
  stopPropagation: () =>
    console.log("stopPropagation"),
};

stop(event);
Try in REPL

Questions

  • How to stop the propagation of an event?

TypeScript sourceJavaScript source