Skip to main content

Documentation

Guides

Intro

Welcome to the Remeda documentation and API reference. Below, you’ll find the complete reference for all functions exported by Remeda.

Previous Versions

Are you using version 1.x.x? Visit our migration guide to help you transition to the latest version.


array

allPass

Determines whether all predicates return true for the input data.

Data First

allPass(data, fns);
Parameters
data
The input data for predicates.
fns
The list of predicates.
Returns
boolean
const isDivisibleBy3 = (x: number) => x % 3 === 0;
const isDivisibleBy4 = (x: number) => x % 4 === 0;
const fns = [isDivisibleBy3, isDivisibleBy4];
allPass(12, fns); // => true
allPass(8, fns); // => false

Data Last

allPass(fns)(data);
Parameters
fns
The list of predicates.
Returns
Object
const isDivisibleBy3 = (x: number) => x % 3 === 0;
const isDivisibleBy4 = (x: number) => x % 4 === 0;
const fns = [isDivisibleBy3, isDivisibleBy4];
allPass(fns)(12); // => true
allPass(fns)(8); // => false

anyPass

Determines whether any predicate returns true for the input data.

Data First

anyPass(data, fns);
Parameters
data
The input data for predicates.
fns
The list of predicates.
Returns
boolean
const isDivisibleBy3 = (x: number) => x % 3 === 0;
const isDivisibleBy4 = (x: number) => x % 4 === 0;
const fns = [isDivisibleBy3, isDivisibleBy4];
anyPass(8, fns); // => true
anyPass(11, fns); // => false

Data Last

anyPass(fns)(data);
Parameters
fns
The list of predicates.
Returns
Object
const isDivisibleBy3 = (x: number) => x % 3 === 0;
const isDivisibleBy4 = (x: number) => x % 4 === 0;
const fns = [isDivisibleBy3, isDivisibleBy4];
anyPass(fns)(8); // => true
anyPass(fns)(11); // => false

chunk

Split an array into groups the length of size. If array can't be split evenly, the final chunk will be the remaining elements.

Data First

chunk(array, size);
Parameters
array
The array.
size
The length of the chunk.
Returns
Object
chunk(["a", "b", "c", "d"], 2); // => [['a', 'b'], ['c', 'd']]
chunk(["a", "b", "c", "d"], 3); // => [['a', 'b', 'c'], ['d']]

Data Last

chunk(size)(array);
Parameters
size
The length of the chunk.
Returns
Object
chunk(2)(["a", "b", "c", "d"]); // => [['a', 'b'], ['c', 'd']]
chunk(3)(["a", "b", "c", "d"]); // => [['a', 'b', 'c'], ['d']]

concat

Merge two or more arrays. This method does not change the existing arrays, but instead returns a new array, even if the other array is empty.

Data First

concat(data, other);
Parameters
data
The first items, these would be at the beginning of the new array.
other
The remaining items, these would be at the end of the new array.
Returns
Object
concat([1, 2, 3], ["a"]); // [1, 2, 3, 'a']

Data Last

concat(arr2)(arr1);
Parameters
other
The remaining items, these would be at the end of the new array.
Returns
Object
concat(["a"])([1, 2, 3]); // [1, 2, 3, 'a']

countBy

Categorize and count elements in an array using a defined callback function. The callback function is applied to each element in the array to determine its category and then counts how many elements fall into each category.

Data First

countBy(data, categorizationFn);
Parameters
data
The array.
categorizationFn
The categorization function.
Returns
Object
countBy(["a", "b", "c", "B", "A", "a"], toLowerCase()); //=> { a: 3, b: 2, c: 1 }

Data Last

countBy(categorizationFn)(data);
Parameters
categorizationFn
The categorization function.
Returns
Object
pipe(["a", "b", "c", "B", "A", "a"], countBy(toLowerCase())); //=> { a: 3, b: 2, c: 1 }

difference

Lazy

Excludes the values from other array. The output maintains the same order as the input. The inputs are treated as multi-sets/bags (multiple copies of items are treated as unique items).

Data First

difference(data, other);
Parameters
data
The input items.
other
The values to exclude.
Returns
Array
difference([1, 2, 3, 4], [2, 5, 3]); // => [1, 4]
difference([1, 1, 2, 2], [1]); // => [1, 2, 2]

Data First

difference(other)(data);
Parameters
other
The values to exclude.
Returns
Object
pipe([1, 2, 3, 4], difference([2, 5, 3])); // => [1, 4]
pipe([1, 1, 2, 2], difference([1])); // => [1, 2, 2]

differenceWith

Lazy

Excludes the values from other array. Elements are compared by custom comparator isEquals.

Data First

differenceWith(data, other, isEqual);
Parameters
data
The source array.
other
The values to exclude.
isEqual
The comparator.
Returns
Array
differenceWith(
  [{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4 }],
  [2, 5, 3],
  ({ a }, b) => a === b,
); //=> [{ a: 1 }, { a: 4 }]

Data Last

differenceWith(other, isEqual)(data);
Parameters
other
The values to exclude.
isEqual
The comparator.
Returns
Object
pipe(
  [{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4 }, { a: 5 }, { a: 6 }],
  differenceWith([2, 3], ({ a }, b) => a === b),
); //=> [{ a: 1 }, { a: 4 }, { a: 5 }, { a: 6 }]

drop

Lazy

Removes first n elements from the array.

Data First

drop(array, n);
Parameters
array
The target array.
n
The number of elements to skip.
Returns
Object
drop([1, 2, 3, 4, 5], 2); // => [3, 4, 5]

Data Last

drop(n)(array);
Parameters
n
The number of elements to skip.
Returns
Object
drop(2)([1, 2, 3, 4, 5]); // => [3, 4, 5]

dropFirstBy

Drop the first n items from data based on the provided ordering criteria. This allows you to avoid sorting the array before dropping the items. The complexity of this function is O(Nlogn) where N is the length of the array.

For the opposite operation (to keep n elements) see takeFirstBy.

Data First

dropFirstBy(data, n, ...rules);
Parameters
data
The input array.
n
The number of items to drop. If `n` is non-positive no items would be dropped and a *clone* of the input would be returned, if `n` is bigger then data.length no items would be returned.
rules
A variadic array of order rules defining the sorting criteria. Each order rule is a projection function that extracts a comparable value from the data. Sorting is based on these extracted values using the native `<` and `>` operators. Earlier rules take precedence over later ones. Use the syntax `[projection, "desc"]` for descending order.
Returns
Array
dropFirstBy(["aa", "aaaa", "a", "aaa"], 2, (x) => x.length); // => ['aaaa', 'aaa']

Data Last

dropFirstBy(n, ...rules)(data);
Parameters
n
The number of items to drop. If `n` is non-positive no items would be dropped and a *clone* of the input would be returned, if `n` is bigger then data.length no items would be returned.
rules
A variadic array of order rules defining the sorting criteria. Each order rule is a projection function that extracts a comparable value from the data. Sorting is based on these extracted values using the native `<` and `>` operators. Earlier rules take precedence over later ones. Use the syntax `[projection, "desc"]` for descending order.
Returns
Object
pipe(
  ["aa", "aaaa", "a", "aaa"],
  dropFirstBy(2, (x) => x.length),
); // => ['aaaa', 'aaa']

dropLast

Removes last n elements from the array.

Data First

dropLast(array, n);
Parameters
array
The target array.
n
The number of elements to skip.
Returns
Array
dropLast([1, 2, 3, 4, 5], 2); // => [1, 2, 3]

Data Last

dropLast(n)(array);
Parameters
n
The number of elements to skip.
Returns
Object
dropLast(2)([1, 2, 3, 4, 5]); // => [1, 2, 3]

dropLastWhile

Removes elements from the end of the array until the predicate returns false.

The predicate is applied to each element in the array starting from the end and moving towards the beginning, until the predicate returns false. The returned array includes elements from the beginning of the array, up to and including the element that produced false for the predicate.

Data First

dropLastWhile(data, predicate);
Parameters
data
The array.
predicate
The predicate.
Returns
Array
dropLastWhile([1, 2, 10, 3, 4], (x) => x < 10); // => [1, 2, 10]

Data Last

dropLastWhile(predicate)(data);
Parameters
predicate
The predicate.
Returns
Object
pipe(
  [1, 2, 10, 3, 4],
  dropLastWhile((x) => x < 10),
); // => [1, 2, 10]

dropWhile

Removes elements from the beginning of the array until the predicate returns false.

The predicate is applied to each element in the array, until the predicate returns false. The returned array includes the rest of the elements, starting with the element that produced false for the predicate.

Data First

dropWhile(data, predicate);
Parameters
data
The array.
predicate
The predicate.
Returns
Array
dropWhile([1, 2, 10, 3, 4], (x) => x < 10); // => [10, 3, 4]

Data Last

dropWhile(predicate)(data);
Parameters
predicate
The predicate.
Returns
Object
pipe(
  [1, 2, 10, 3, 4],
  dropWhile((x) => x < 10),
); // => [10, 3, 4]

filter

Lazy

Creates a shallow copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function. Equivalent to Array.prototype.filter.

Data First

filter(data, predicate);
Parameters
data
The array to filter.
predicate
A function to execute for each element in the array. It should return `true` to keep the element in the resulting array, and `false` otherwise. A type-predicate can also be used to narrow the result.
Returns
Object
filter([1, 2, 3], (x) => x % 2 === 1); // => [1, 3]

Data Last

filter(predicate)(data);
Parameters
predicate
A function to execute for each element in the array. It should return `true` to keep the element in the resulting array, and `false` otherwise.
Returns
Object
pipe(
  [1, 2, 3],
  filter((x) => x % 2 === 1),
); // => [1, 3]

find

Lazy

Returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.

Similar functions:

  • findLast - If you need the last element that satisfies the provided testing function.
  • findIndex - If you need the index of the found element in the array.
  • indexOf - If you need to find the index of a value.
  • includes - If you need to find if a value exists in an array.
  • some - If you need to find if any element satisfies the provided testing function.
  • filter - If you need to find all elements that satisfy the provided testing function.

Data First

find(data, predicate);
Parameters
data
The items to search in.
predicate
A function to execute for each element in the array. It should return `true` to indicate a matching element has been found, and `false` otherwise. A type-predicate can also be used to narrow the result.
Returns
Object
find([1, 3, 4, 6], (n) => n % 2 === 0); // => 4

Data Last

find(predicate)(data);
Parameters
predicate
A function to execute for each element in the array. It should return `true` to indicate a matching element has been found, and `false` otherwise. A type-predicate can also be used to narrow the result.
Returns
Object
pipe(
  [1, 3, 4, 6],
  find((n) => n % 2 === 0),
); // => 4

findIndex

Returns the index of the first element in an array that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned.

See also the find method, which returns the first element that satisfies the testing function (rather than its index).

Data First

findIndex(data, predicate);
Parameters
data
The items to search in.
predicate
A function to execute for each element in the array. It should return a `true` to indicate a matching element has been found, and a `false` otherwise.
Returns
number
findIndex([1, 3, 4, 6], (n) => n % 2 === 0); // => 2

Data Last

findIndex(predicate)(data);
Parameters
predicate
A function to execute for each element in the array. It should return a `true` to indicate a matching element has been found, and a `false` otherwise.
Returns
Object
pipe(
  [1, 3, 4, 6],
  findIndex((n) => n % 2 === 0),
); // => 2

findLast

Iterates the array in reverse order and returns the value of the first element that satisfies the provided testing function. If no elements satisfy the testing function, undefined is returned.

Similar functions:

  • find - If you need the first element that satisfies the provided testing function.
  • findLastIndex - If you need the index of the found element in the array.
  • lastIndexOf - If you need to find the index of a value.
  • includes - If you need to find if a value exists in an array.
  • some - If you need to find if any element satisfies the provided testing function.
  • filter - If you need to find all elements that satisfy the provided testing function.

Data First

findLast(data, predicate);
Parameters
data
The items to search in.
predicate
A function to execute for each element in the array. It should return `true` to indicate a matching element has been found, and `false` otherwise. A type-predicate can also be used to narrow the result.
Returns
Object
findLast([1, 3, 4, 6], (n) => n % 2 === 1); // => 3

Data Last

findLast(predicate)(data);
Parameters
predicate
A function to execute for each element in the array. It should return `true` to indicate a matching element has been found, and `false` otherwise. A type-predicate can also be used to narrow the result.
Returns
Object
pipe(
  [1, 3, 4, 6],
  findLast((n) => n % 2 === 1),
); // => 3

findLastIndex

Iterates the array in reverse order and returns the index of the first element that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned.

See also findLast which returns the value of last element that satisfies the testing function (rather than its index).

Data First

findLastIndex(data, predicate);
Parameters
data
The items to search in.
predicate
A function to execute for each element in the array. It should return `true` to indicate a matching element has been found, and `false` otherwise.
Returns
number
findLastIndex([1, 3, 4, 6], (n) => n % 2 === 1); // => 1

Data Last

findLastIndex(fn)(items);
Parameters
predicate
A function to execute for each element in the array. It should return `true` to indicate a matching element has been found, and `false` otherwise.
Returns
Object
pipe(
  [1, 3, 4, 6],
  findLastIndex((n) => n % 2 === 1),
); // => 1

first

Lazy

Gets the first element of array.

Data First

first(array);
Parameters
data
The array.
Returns
Object
first([1, 2, 3]); // => 1
first([]); // => undefined

Data Last

first()(array);
Parameters
Returns
Object
pipe(
  [1, 2, 4, 8, 16],
  filter((x) => x > 3),
  first(),
  (x) => x + 1,
); // => 5

firstBy

Find the first element in the array that adheres to the order rules provided. This is a superset of what a typical maxBy or minBy function would do as it allows defining "tie-breaker" rules when values are equal, and allows comparing items using any logic. This function is equivalent to calling first(sortBy(...)) but runs at O(n) instead of O(nlogn).

Use nthBy if you need an element other that the first, or takeFirstBy if you more than just the first element.

Data First

firstBy(data, ...rules);
Parameters
data
An array of items.
rules
A variadic array of order rules defining the sorting criteria. Each order rule is a projection function that extracts a comparable value from the data. Sorting is based on these extracted values using the native `<` and `>` operators. Earlier rules take precedence over later ones. Use the syntax `[projection, "desc"]` for descending order.
Returns
Object
const max = firstBy([1, 2, 3], [identity(), "desc"]); // => 3;
const min = firstBy([1, 2, 3], identity()); // => 1;

const data = [{ a: "a" }, { a: "aa" }, { a: "aaa" }] as const;
const maxBy = firstBy(data, [(item) => item.a.length, "desc"]); // => { a: "aaa" };
const minBy = firstBy(data, (item) => item.a.length); // => { a: "a" };

const data = [
  { type: "cat", size: 1 },
  { type: "cat", size: 2 },
  { type: "dog", size: 3 },
] as const;
const multi = firstBy(data, prop("type"), [prop("size"), "desc"]); // => {type: "cat", size: 2}

Data Last

firstBy(...rules)(data);
Parameters
rules
A variadic array of order rules defining the sorting criteria. Each order rule is a projection function that extracts a comparable value from the data. Sorting is based on these extracted values using the native `<` and `>` operators. Earlier rules take precedence over later ones. Use the syntax `[projection, "desc"]` for descending order.
Returns
Object
const max = pipe([1, 2, 3], firstBy([identity(), "desc"])); // => 3;
const min = pipe([1, 2, 3], firstBy(identity())); // => 1;

const data = [{ a: "a" }, { a: "aa" }, { a: "aaa" }] as const;
const maxBy = pipe(data, firstBy([(item) => item.a.length, "desc"])); // => { a: "aaa" };
const minBy = pipe(
  data,
  firstBy((item) => item.a.length),
); // => { a: "a" };

const data = [
  { type: "cat", size: 1 },
  { type: "cat", size: 2 },
  { type: "dog", size: 3 },
] as const;
const multi = pipe(data, firstBy(prop("type"), [prop("size"), "desc"])); // => {type: "cat", size: 2}

flat

Lazy

Creates a new array with all sub-array elements concatenated into it recursively up to the specified depth. Equivalent to the built-in Array.prototype.flat method.

Data First

flat(data);
Parameters
data
The items to flatten.
depth
The depth level specifying how deep a nested array structure should be flattened. Defaults to 1. Non literal values (those typed as `number`cannot be used. `Infinity`, `Number.POSITIVE_INFINITY` and `Number.MAX_VALUE` are all typed as `number` and can't be used either. For "unlimited" depth use a literal value that would exceed your expected practical maximum nesting level.
Returns
Object
flat([[1, 2], [3, 4], [5], [[6]]]); // => [1, 2, 3, 4, 5, [6]]
flat([[[1]], [[2]]], 2); // => [1, 2]

Data Last

flat()(data);
Parameters
depth
The depth level specifying how deep a nested array structure should be flattened. Defaults to 1.
Returns
Object
pipe([[1, 2], [3, 4], [5], [[6]]], flat()); // => [1, 2, 3, 4, 5, [6]]
pipe([[[1]], [[2]]], flat(2)); // => [1, 2]

flatMap

Lazy

Returns a new array formed by applying a given callback function to each element of the array, and then flattening the result by one level. It is identical to a map followed by a flat of depth 1 (flat(map(data, ...args))), but slightly more efficient than calling those two methods separately. Equivalent to Array.prototype.flatMap.

Data First

flatMap(data, callbackfn);
Parameters
data
The items to map and flatten.
callbackfn
A function to execute for each element in the array. It should return an array containing new elements of the new array, or a single non-array value to be added to the new array.
Returns
Array
flatMap([1, 2, 3], (x) => [x, x * 10]); // => [1, 10, 2, 20, 3, 30]

Data Last

flatMap(callbackfn)(data);
Parameters
callbackfn
A function to execute for each element in the array. It should return an array containing new elements of the new array, or a single non-array value to be added to the new array.
Returns
Object
pipe(
  [1, 2, 3],
  flatMap((x) => [x, x * 10]),
); // => [1, 10, 2, 20, 3, 30]

forEach

Lazy

Executes a provided function once for each array element. Equivalent to Array.prototype.forEach.

The dataLast version returns the original array (instead of not returning anything (void)) to allow using it in a pipe. When not used in a pipe the returned array is equal to the input array (by reference), and not a shallow copy of it!

Data First

forEach(data, callbackfn);
Parameters
data
The values that would be iterated on.
callbackfn
A function to execute for each element in the array.
Returns
void
forEach([1, 2, 3], (x) => {
  console.log(x);
});

Data Last

forEach(callbackfn)(data);
Parameters
callbackfn
A function to execute for each element in the array.
Returns
Object
pipe(
  [1, 2, 3],
  forEach((x) => {
    console.log(x);
  }),
); // => [1, 2, 3]

groupBy

Groups the elements of a given iterable according to the string values returned by a provided callback function. The returned object has separate properties for each group, containing arrays with the elements in the group. Unlike the built in Object.groupBy this function also allows the callback to return undefined in order to exclude the item from being added to any group.

If you are grouping objects by a property of theirs (e.g. groupBy(data, ({ myProp }) => myProp) or groupBy(data, prop('myProp'))) consider using groupByProp (e.g. groupByProp(data, 'myProp')) instead, as it would provide better typing.

Data First

groupBy(data, callbackfn);
Parameters
data
The items to group.
callbackfn
A function to execute for each element in the iterable. It should return a value indicating the group of the current element, or `undefined` when the item should be excluded from any group.
Returns
Object
groupBy([{ a: "cat" }, { a: "dog" }] as const, prop("a")); // => {cat: [{a: 'cat'}], dog: [{a: 'dog'}]}
groupBy([0, 1], (x) => (x % 2 === 0 ? "even" : undefined)); // => {even: [0]}

Data Last

groupBy(callbackfn)(data);
Parameters
callbackfn
A function to execute for each element in the iterable. It should return a value indicating the group of the current element, or `undefined` when the item should be excluded from any group.
Returns
Object
pipe([{ a: "cat" }, { a: "dog" }] as const, groupBy(prop("a"))); // => {cat: [{a: 'cat'}], dog: [{a: 'dog'}]}
pipe(
  [0, 1],
  groupBy((x) => (x % 2 === 0 ? "even" : undefined)),
); // => {even: [0]}

groupByProp

Groups the elements of an array of objects based on the values of a specified property of those objects. The result would contain a property for each unique value of the specific property, with it's value being the input array filtered to only items that have that property set to that value. For any object where the property is missing, or if it's value is undefined the item would be filtered out.

The grouping property is enforced at the type level to exist in at least one item and to never have a value that cannot be used as an object key (e.g. it must be PropertyKey | undefined).

The resulting arrays are filtered with the prop and it's value as a type-guard, effectively narrowing the items in each output arrays. This means that when the grouping property is the discriminator of a discriminated union type each output array would contain just the subtype for that value.

If you need more control over the grouping you should use groupBy instead.

Data First

groupByProp(data, prop);
Parameters
data
The items to group.
prop
The property name to group by.
Returns
Object
const result = groupByProp(
  //  ^? { cat: [{ a: 'cat' }], dog: [{ a: 'dog' }] }
  [{ a: "cat" }, { a: "dog" }] as const,
  "a",
);

Data Last

groupByProp(prop)(data);
Parameters
prop
The property name to group by.
Returns
Object
const result = pipe(
  //  ^? { cat: [{ a: 'cat' }], dog: [{ a: 'dog' }] }
  [{ a: "cat" }, { a: "dog" }] as const,
  groupByProp("a"),
);

hasAtLeast

Checks if the given array has at least the defined number of elements. When the minimum used is a literal (e.g. 3) the output is refined accordingly so that those indices are defined when accessing the array even when using typescript's 'noUncheckedIndexAccess'.

Data First

hasAtLeast(data, minimum);
Parameters
data
The input array.
minimum
The minimum number of elements the array must have.
Returns
boolean
hasAtLeast([], 4); // => false

const data: number[] = [1, 2, 3, 4];
hasAtLeast(data, 1); // => true
data[0]; // 1, with type `number`

Data Last

hasAtLeast(minimum)(data);
Parameters
minimum
The minimum number of elements the array must have.
Returns
Object
pipe([], hasAtLeast(4)); // => false

const data = [[1, 2], [3], [4, 5]];
pipe(
  data,
  filter(hasAtLeast(2)),
  map(([, second]) => second),
); // => [2,5], with type `number[]`

indexBy

Converts a list of objects into an object indexing the objects by the given key.

There are several other functions that could be used to build an object from an array:

  • fromKeys - Builds an object from an array of keys and a mapper for values.
  • pullObject - Builds an object from an array of items with mappers for both keys and values.
  • fromEntries - Builds an object from an array of key-value pairs. Refer to the docs for more details.

Data First

indexBy(array, fn);
Parameters
data
The array.
mapper
The indexing function.
Returns
Object
indexBy(["one", "two", "three"], (x) => x.length); // => {3: 'two', 5: 'three'}

Data Last

indexBy(fn)(array);
Parameters
mapper
The indexing function.
Returns
Object
pipe(
  ["one", "two", "three"],
  indexBy((x) => x.length),
); // => {3: 'two', 5: 'three'}

intersection

Lazy

Returns a list of elements that exist in both array. The output maintains the same order as the input. The inputs are treated as multi-sets/bags (multiple copies of items are treated as unique items).

Data First

intersection(data, other);
Parameters
data
The input items.
other
The items to compare against.
Returns
Array
intersection([1, 2, 3], [2, 3, 5]); // => [2, 3]
intersection([1, 1, 2, 2], [1]); // => [1]

Data Last

intersection(other)(data);
Parameters
other
The items to compare against.
Returns
Object
pipe([1, 2, 3], intersection([2, 3, 5])); // => [2, 3]
pipe([1, 1, 2, 2], intersection([1])); // => [1]

intersectionWith

Lazy

Returns a list of intersecting values based on a custom comparator function that compares elements of both arrays.

Data First

intersectionWith(array, other, comparator);
Parameters
array
The source array.
other
The second array.
comparator
The custom comparator.
Returns
Array
intersectionWith(
  [
    { id: 1, name: "Ryan" },
    { id: 3, name: "Emma" },
  ],
  [3, 5],
  (a, b) => a.id === b,
); // => [{ id: 3, name: 'Emma' }]

Data Last

intersectionWith(other, comparator)(array);
Parameters
other
The second array.
comparator
The custom comparator.
Returns
Object
intersectionWith(
  [3, 5],
  (a, b) => a.id === b,
)([
  { id: 1, name: "Ryan" },
  { id: 3, name: "Emma" },
]); // => [{ id: 3, name: 'Emma' }]

join

Joins the elements of the array by: casting them to a string and concatenating them one to the other, with the provided glue string in between every two elements.

When called on a tuple and with stricter item types (union of literal values, the result is strictly typed to the tuples shape and it's item types).

Data First

join(data, glue);
Parameters
data
The array to join.
glue
The string to put in between every two elements.
Returns
Object
join([1, 2, 3], ","); // => "1,2,3" (typed `string`)
join(["a", "b", "c"], ""); // => "abc" (typed `string`)
join(["hello", "world"] as const, " "); // => "hello world" (typed `hello world`)

Data Last

join(glue)(data);
Parameters
glue
The string to put in between every two elements.
Returns
Object
pipe([1, 2, 3], join(",")); // => "1,2,3" (typed `string`)
pipe(["a", "b", "c"], join("")); // => "abc" (typed `string`)
pipe(["hello", "world"] as const, join(" ")); // => "hello world" (typed `hello world`)

last

Gets the last element of array.

Data First

last(array);
Parameters
data
The array.
Returns
Object
last([1, 2, 3]); // => 3
last([]); // => undefined

Data Last

last()(array);
Parameters
Returns
Object
pipe(
  [1, 2, 4, 8, 16],
  filter((x) => x > 3),
  last(),
  (x) => x + 1,
); // => 17

length

Counts values of the collection or iterable.

Data First

length(array);
Parameters
items
The input data.
Returns
number
length([1, 2, 3]); // => 3

Data Last

length()(array);
Parameters
Returns
Object
pipe([1, 2, 3], length()); // => 3

map

Lazy

Creates a new array populated with the results of calling a provided function on every element in the calling array. Equivalent to Array.prototype.map.

Data First

map(data, callbackfn);
Parameters
data
The array to map.
callbackfn
A function to execute for each element in the array. Its return value is added as a single element in the new array.
Returns
Object
map([1, 2, 3], multiply(2)); // => [2, 4, 6]
map([0, 0], add(1)); // => [1, 1]
map([0, 0], (value, index) => value + index); // => [0, 1]

Data Last

map(callbackfn)(data);
Parameters
callbackfn
A function to execute for each element in the array. Its return value is added as a single element in the new array.
Returns
Object
pipe([1, 2, 3], map(multiply(2))); // => [2, 4, 6]
pipe([0, 0], map(add(1))); // => [1, 1]
pipe(
  [0, 0],
  map((value, index) => value + index),
); // => [0, 1]

mapToObj

Map each element of an array into an object using a defined mapper that converts each item into an object entry (a tuple of [<key>, <value>]).

There are several other functions that could be used to build an object from an array:

  • fromKeys - Builds an object from an array of keys and a mapper for values.
  • indexBy - Builds an object from an array of values and a mapper for keys.
  • pullObject - Builds an object from an array of items with a mapper for values and another mapper for keys.
  • fromEntries - Builds an object from an array of key-value pairs.

Warning: We strongly advise against using this function unless it is used with a huge input array and your app has stringent memory/gc constraints. We recommend that in most cases you should use pullObject, or the composition fromEntries(map(array, fn)). This function will be deprecated and removed in future versions of the library!

Data First

mapToObj(array, fn);
Parameters
array
The array to map.
fn
The mapping function, which should return a tuple of [key, value], similar to Object.fromEntries.
Returns
Object
mapToObj([1, 2, 3], (x) => [String(x), x * 2]); // => {1: 2, 2: 4, 3: 6}

Data Last

mapToObj(fn)(array);
Parameters
fn
The mapping function, which should return a tuple of [key, value], similar to Object.fromEntries.
Returns
Object
pipe(
  [1, 2, 3],
  mapToObj((x) => [String(x), x * 2]),
); // => {1: 2, 2: 4, 3: 6}

mapWithFeedback

Lazy

Applies a function on each element of the array, using the result of the previous application, and returns an array of the successively computed values.

Data First

mapWithFeedback(data, callbackfn, initialValue);
Parameters
data
The array to map over.
callbackfn
The callback function that receives the previous value, the current element.
initialValue
The initial value to start the computation with.
Returns
Object
mapWithFeedback([1, 2, 3, 4, 5], (prev, x) => prev + x, 100); // => [101, 103, 106, 110, 115]

Data Last

mapWithFeedback(callbackfn, initialValue)(data);
Parameters
callbackfn
The callback function that receives the previous value, the current element.
initialValue
The initial value to start the computation with.
Returns
Object
pipe(
  [1, 2, 3, 4, 5],
  mapWithFeedback((prev, x) => prev + x, 100),
); // => [101, 103, 106, 110, 115]

meanBy

Returns the mean of the elements of an array using the provided predicate.

Data First

meanBy(array, fn);
Parameters
items
The array.
fn
Predicate function.
Returns
number
meanBy([{ a: 5 }, { a: 1 }, { a: 3 }], (x) => x.a); // 3

Data Last

meanBy(fn)(array);
Parameters
fn
Predicate function.
Returns
Object
pipe(
  [{ a: 5 }, { a: 1 }, { a: 3 }],
  meanBy((x) => x.a),
); // 3

mergeAll

Merges a list of objects into a single object.

Data First

mergeAll(objects);
Parameters
objects
The array of objects.
Returns
Object
mergeAll([{ a: 1, b: 1 }, { b: 2, c: 3 }, { d: 10 }]); // => { a: 1, b: 2, c: 3, d: 10 }
mergeAll([]); // => {}

nthBy

Retrieves the element that would be at the given index if the array were sorted according to specified rules. This function uses the QuickSelect algorithm running at an average complexity of O(n). Semantically it is equivalent to sortBy(data, ...rules).at(index) which would run at O(nlogn).

See also firstBy which provides an even more efficient algorithm and a stricter return type, but only for index === 0. See takeFirstBy to get all the elements up to and including index.

Data First

nthBy(data, index, ...rules);
Parameters
data
The input array.
index
The zero-based index for selecting the element in the sorted order. Negative indices count backwards from the end.
rules
A variadic array of order rules defining the sorting criteria. Each order rule is a projection function that extracts a comparable value from the data. Sorting is based on these extracted values using the native `<` and `>` operators. Earlier rules take precedence over later ones. Use the syntax `[projection, "desc"]` for descending order.
Returns
Object
nthBy([2, 1, 4, 5, 3], 2, identity()); // => 3

Data Last

nthBy(index, ...rules)(data);
Parameters
index
The zero-based index for selecting the element in the sorted order. Negative indices count backwards from the end.
rules
A variadic array of order rules defining the sorting criteria. Each order rule is a projection function that extracts a comparable value from the data. Sorting is based on these extracted values using the native `<` and `>` operators. Earlier rules take precedence over later ones. Use the syntax `[projection, "desc"]` for descending order.
Returns
Object
pipe([2, 1, 4, 5, 3], nthBy(2, identity())); // => 3

only

Returns the first and only element of array, or undefined otherwise.

Data First

only(array);
Parameters
array
The target array.
Returns
Object
only([]); // => undefined
only([1]); // => 1
only([1, 2]); // => undefined

Data Last

only()(array);
Parameters
Returns
Object
pipe([], only()); // => undefined
pipe([1], only()); // => 1
pipe([1, 2], only()); // => undefined

partition

Splits a collection into two groups, the first of which contains elements the predicate type guard passes, and the second one containing the rest.

Data First

partition(data, predicate);
Parameters
data
The items to split.
predicate
A function to execute for each element in the array. It should return `true` to add the element to the first partition, and and `false` to add the element to the other partition. A type-predicate can also be used to narrow the result.
Returns
Object
partition(["one", "two", "forty two"], (x) => x.length === 3); // => [['one', 'two'], ['forty two']]

Data Last

partition(predicate)(data);
Parameters
predicate
A function to execute for each element in the array. It should return `true` to add the element to the first partition, and and `false` to add the element to the other partition. A type-predicate can also be used to narrow the result.
Returns
Object
pipe(
  ["one", "two", "forty two"],
  partition((x) => x.length === 3),
); // => [['one', 'two'], ['forty two']]

range

Returns a sequence of numbers from start (inclusive) to end (exclusive), adding step (default is 1) to each number in the sequence.

Data First

range(start, end);
Parameters
start
The first number in the sequence.
endOrOptions
The end number **or** an object which is used to **also** define a step size.
Returns
Array
range(1, 5); //=> [1, 2, 3, 4]
range(1, { end: 5, step: 2 }); //=> [1, 3]

Data Last

range(end)(start);
Parameters
endOrOptions
The end number **or** an object which is used to **also** define a step size.
Returns
Object
pipe(1, range(5)); //=> [1, 2, 3, 4]
pipe(1, range({ end: 5, step: 2 })); //=> [1, 3]

rankBy

Calculates the rank of an item in an array based on rules. The rank is the position where the item would appear in the sorted array. This function provides an efficient way to determine the rank in O(n) time, compared to O(nlogn) for the equivalent sortedIndex(sortBy(data, ...rules), item).

Data First

rankBy(data, item, ...rules);
Parameters
data
The input array.
item
The item whose rank is to be determined.
rules
A variadic array of order rules defining the sorting criteria. Each order rule is a projection function that extracts a comparable value from the data. Sorting is based on these extracted values using the native `<` and `>` operators. Earlier rules take precedence over later ones. Use the syntax `[projection, "desc"]` for descending order.
Returns
number
const DATA = [{ a: 5 }, { a: 1 }, { a: 3 }] as const;
rankBy(DATA, 0, prop("a")); // => 0
rankBy(DATA, 1, prop("a")); // => 1
rankBy(DATA, 2, prop("a")); // => 1
rankBy(DATA, 3, prop("a")); // => 2

Data Last

rankBy(item, ...rules)(data);
Parameters
item
The item whose rank is to be determined.
rules
A variadic array of order rules defining the sorting criteria. Each order rule is a projection function that extracts a comparable value from the data. Sorting is based on these extracted values using the native `<` and `>` operators. Earlier rules take precedence over later ones. Use the syntax `[projection, "desc"]` for descending order.
Returns
Object
const DATA = [{ a: 5 }, { a: 1 }, { a: 3 }] as const;
pipe(DATA, rankBy(0, prop("a"))); // => 0
pipe(DATA, rankBy(1, prop("a"))); // => 1
pipe(DATA, rankBy(2, prop("a"))); // => 1
pipe(DATA, rankBy(3, prop("a"))); // => 2

reduce

Executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value. Equivalent to Array.prototype.reduce.

Data First

reduce(data, callbackfn, initialValue);
Parameters
data
The items to reduce.
callbackfn
A function to execute for each element in the array. Its return value becomes the value of the accumulator parameter on the next invocation of callbackFn. For the last invocation, the return value becomes the return value of reduce().
initialValue
A value to which accumulator is initialized the first time the callback is called. CallbackFn starts executing with the first value in the array as currentValue.
Returns
Object
reduce([1, 2, 3, 4, 5], (acc, x) => acc + x, 100); // => 115

Data Last

reduce(fn, initialValue)(array);
Parameters
callbackfn
A function to execute for each element in the array. Its return value becomes the value of the accumulator parameter on the next invocation of callbackFn. For the last invocation, the return value becomes the return value of reduce().
initialValue
A value to which accumulator is initialized the first time the callback is called. CallbackFn starts executing with the first value in the array as currentValue.
Returns
Object
pipe(
  [1, 2, 3, 4, 5],
  reduce((acc, x) => acc + x, 100),
); // => 115

reverse

Reverses array.

Data First

reverse(arr);
Parameters
array
The array.
Returns
Object
reverse([1, 2, 3]); // [3, 2, 1]

Data Last

reverse()(array);
Parameters
Returns
Object
reverse()([1, 2, 3]); // [3, 2, 1]

sample

Returns a random subset of size sampleSize from array.

Maintains and infers most of the typing information that could be passed along to the output. This means that when using tuples, the output will be a tuple too, and when using literals, those literals would be preserved.

The items in the result are kept in the same order as they are in the input. If you need to get a shuffled response you can pipe the shuffle function after this one.

Data First

sample(array, sampleSize);
Parameters
data
The array.
sampleSize
The number of elements to take.
Returns
Object
sample(["hello", "world"], 1); // => ["hello"] // typed string[]
sample(["hello", "world"] as const, 1); // => ["world"] // typed ["hello" | "world"]

Data Last

sample(sampleSize)(array);
Parameters
sampleSize
The number of elements to take.
Returns
Object
sample(1)(["hello", "world"]); // => ["hello"] // typed string[]
sample(1)(["hello", "world"] as const); // => ["world"] // typed ["hello" | "world"]

shuffle

Shuffles the input array, returning a new array with the same elements in a random order.

Data First

shuffle(array);
Parameters
items
The array to shuffle.
Returns
Object
shuffle([4, 2, 7, 5]); // => [7, 5, 4, 2]

Data Last

shuffle()(array);
Parameters
Returns
Object
pipe([4, 2, 7, 5], shuffle()); // => [7, 5, 4, 2]

sort

Sorts an array. The comparator function should accept two values at a time and return a negative number if the first value is smaller, a positive number if it's larger, and zero if they are equal. Sorting is based on a native sort function.

Data First

sort(items, cmp);
Parameters
items
The array to sort.
cmp
The comparator function.
Returns
Object
sort([4, 2, 7, 5], (a, b) => a - b); // => [2, 4, 5, 7]

Data Last

sort(cmp)(items);
Parameters
cmp
The comparator function.
Returns
Object
pipe(
  [4, 2, 7, 5],
  sort((a, b) => a - b),
); // => [2, 4, 5, 7]

sortBy

Sorts data using the provided ordering rules. The sort is done via the native Array.prototype.sort but is performed on a shallow copy of the array to avoid mutating the original data.

There are several other functions that take order rules and bypass the need to sort the array first (in O(nlogn) time):

  • firstBy === first(sortBy(data, ...rules)), O(n).
  • takeFirstBy === take(sortBy(data, ...rules), k), O(nlogk).
  • dropFirstBy === drop(sortBy(data, ...rules), k), O(nlogk).
  • nthBy === sortBy(data, ...rules).at(k), O(n).
  • rankBy === sortedIndex(sortBy(data, ...rules), item), O(n). Refer to the docs for more details.

Data First

sortBy(data, ...rules);
Parameters
array
The input array.
sortRules
A variadic array of order rules defining the sorting criteria. Each order rule is a projection function that extracts a comparable value from the data. Sorting is based on these extracted values using the native `<` and `>` operators. Earlier rules take precedence over later ones. Use the syntax `[projection, "desc"]` for descending order.
Returns
Object
sortBy([{ a: 1 }, { a: 3 }, { a: 7 }, { a: 2 }], prop("a")); // => [{ a: 1 }, { a: 2 }, { a: 3 }, { a: 7 }]
sortBy(
  [
    { color: "red", weight: 2 },
    { color: "blue", weight: 3 },
    { color: "green", weight: 1 },
    { color: "purple", weight: 1 },
  ],
  [prop("weight"), "asc"],
  prop("color"),
); // => [
//   {color: 'green', weight: 1},
//   {color: 'purple', weight: 1},
//   {color: 'red', weight: 2},
//   {color: 'blue', weight: 3},
// ]

Data Last

sortBy(...rules)(data);
Parameters
sortRules
A variadic array of order rules defining the sorting criteria. Each order rule is a projection function that extracts a comparable value from the data. Sorting is based on these extracted values using the native `<` and `>` operators. Earlier rules take precedence over later ones. Use the syntax `[projection, "desc"]` for descending order.
Returns
Object
pipe([{ a: 1 }, { a: 3 }, { a: 7 }, { a: 2 }], sortBy(prop("a"))); // => [{ a: 1 }, { a: 2 }, { a: 3 }, { a: 7 }]

sortedIndex

Find the insertion position (index) of an item in an array with items sorted in ascending order; so that splice(sortedIndex, 0, item) would result in maintaining the array's sort-ness. The array can contain duplicates. If the item already exists in the array the index would be of the first occurrence of the item.

Runs in O(logN) time.

Data First

sortedIndex(data, item);
Parameters
data
The (ascending) sorted array.
item
The item to insert.
Returns
number
sortedIndex(["a", "a", "b", "c", "c"], "c"); // => 3

Data Last

sortedIndex(item)(data);
Parameters
item
The item to insert.
Returns
Object
pipe(["a", "a", "b", "c", "c"], sortedIndex("c")); // => 3

sortedIndexBy

Find the insertion position (index) of an item in an array with items sorted in ascending order using a value function; so that splice(sortedIndex, 0, item) would result in maintaining the arrays sort- ness. The array can contain duplicates. If the item already exists in the array the index would be of the first occurrence of the item.

Runs in O(logN) time.

See also:

  • findIndex - scans a possibly unsorted array in-order (linear search).
  • sortedIndex - like this function, but doesn't take a callbackfn.
  • sortedLastIndexBy - like this function, but finds the last suitable index.
  • sortedLastIndex - like sortedIndex, but finds the last suitable index.
  • rankBy - scans a possibly unsorted array in-order, returning the index based on a sorting criteria.

Data First

sortedIndexBy(data, item, valueFunction);
Parameters
data
The (ascending) sorted array.
item
The item to insert.
valueFunction
All comparisons would be performed on the result of calling this function on each compared item. Preferably this function should return a `number` or `string`. This function should be the same as the one provided to sortBy to sort the array. The function is called exactly once on each items that is compared against in the array, and once at the beginning on `item`. When called on `item` the `index` argument is `undefined`.
Returns
number
sortedIndexBy([{ age: 20 }, { age: 22 }], { age: 21 }, prop("age")); // => 1

sortedIndexWith

Performs a binary search for the index of the item at which the predicate stops returning true. This function assumes that the array is "sorted" in regards to the predicate, meaning that running the predicate as a mapper on it would result in an array [...true[], ...false[]]. This stricter requirement from the predicate provides us 2 benefits over findIndex which does a similar thing:

  1. It would run at O(logN) time instead of O(N) time.
  2. It always returns a value (it would return data.length if the predicate returns true for all items).

This function is the basis for all other sortedIndex functions which search for a specific item in a sorted array, and it could be used to perform similar efficient searches.

See also:

  • findIndex - scans a possibly unsorted array in-order (linear search).
  • rankBy - scans a possibly unsorted array in-order, returning the index based on a sorting criteria.

Data First

sortedIndexWith(data, predicate);
Parameters
data
Array, "sorted" by `predicate`.
predicate
A predicate which also defines the array's order.
Returns
number
sortedIndexWith(["a", "ab", "abc"], (item) => item.length < 2); // => 1

Data Last

sortedIndexWith(predicate)(data);
Parameters
predicate
A predicate which also defines the array's order.
Returns
Object
pipe(
  ["a", "ab", "abc"],
  sortedIndexWith((item) => item.length < 2),
); // => 1

sortedLastIndex

Find the insertion position (index) of an item in an array with items sorted in ascending order; so that splice(sortedIndex, 0, item) would result in maintaining the array's sort-ness. The array can contain duplicates. If the item already exists in the array the index would be of the last occurrence of the item.

Runs in O(logN) time.

Data First

sortedLastIndex(data, item);
Parameters
data
The (ascending) sorted array.
item
The item to insert.
Returns
number
sortedLastIndex(["a", "a", "b", "c", "c"], "c"); // => 5

Data Last

sortedLastIndex(item)(data);
Parameters
item
The item to insert.
Returns
Object
pipe(["a", "a", "b", "c", "c"], sortedLastIndex("c")); // => 5

sortedLastIndexBy

Find the insertion position (index) of an item in an array with items sorted in ascending order using a value function; so that splice(sortedIndex, 0, item) would result in maintaining the arrays sort- ness. The array can contain duplicates. If the item already exists in the array the index would be of the last occurrence of the item.

Runs in O(logN) time.

See also:

  • findIndex - scans a possibly unsorted array in-order (linear search).
  • sortedLastIndex - a simplified version of this function, without a callbackfn.
  • sortedIndexBy - like this function, but returns the first suitable index.
  • sortedIndex - like sortedLastIndex but without a callbackfn.
  • rankBy - scans a possibly unsorted array in-order, returning the index based on a sorting criteria.

Data First

sortedLastIndexBy(data, item, valueFunction);
Parameters
data
The (ascending) sorted array.
item
The item to insert.
valueFunction
All comparisons would be performed on the result of calling this function on each compared item. Preferably this function should return a `number` or `string`. This function should be the same as the one provided to sortBy to sort the array. The function is called exactly once on each items that is compared against in the array, and once at the beginning on `item`. When called on `item` the `index` argument is `undefined`.
Returns
number
sortedLastIndexBy([{ age: 20 }, { age: 22 }], { age: 21 }, prop("age")); // => 1

Data Last

sortedLastIndexBy(item, valueFunction)(data);
Parameters
item
The item to insert.
valueFunction
All comparisons would be performed on the result of calling this function on each compared item. Preferably this function should return a `number` or `string`. This function should be the same as the one provided to sortBy to sort the array. The function is called exactly once on each items that is compared against in the array, and once at the beginning on `item`. When called on `item` the `index` argument is `undefined`.
Returns
Object
pipe([{ age: 20 }, { age: 22 }], sortedLastIndexBy({ age: 21 }, prop("age"))); // => 1

splice

Removes elements from an array and, inserts new elements in their place.

Data First

splice(items, start, deleteCount, replacement);
Parameters
items
The array to splice.
start
The index from which to start removing elements.
deleteCount
The number of elements to remove.
replacement
The elements to insert into the array in place of the deleted elements.
Returns
Array
splice([1, 2, 3, 4, 5, 6, 7, 8], 2, 3, []); //=> [1,2,6,7,8]
splice([1, 2, 3, 4, 5, 6, 7, 8], 2, 3, [9, 10]); //=> [1,2,9,10,6,7,8]

Data Last

splice(start, deleteCount, replacement)(items);
Parameters
start
The index from which to start removing elements.
deleteCount
The number of elements to remove.
replacement
The elements to insert into the array in place of the deleted elements.
Returns
Object
pipe([1, 2, 3, 4, 5, 6, 7, 8], splice(2, 3, [])); // => [1,2,6,7,8]
pipe([1, 2, 3, 4, 5, 6, 7, 8], splice(2, 3, [9, 10])); // => [1,2,9,10,6,7,8]

splitAt

Splits a given array at a given index.

Data First

splitAt(array, index);
Parameters
array
The array to split.
index
The index to split at.
Returns
Object
splitAt([1, 2, 3], 1); // => [[1], [2, 3]]
splitAt([1, 2, 3, 4, 5], -1); // => [[1, 2, 3, 4], [5]]

Data Last

splitAt(index)(array);
Parameters
index
The index to split at.
Returns
Object
splitAt(1)([1, 2, 3]); // => [[1], [2, 3]]
splitAt(-1)([1, 2, 3, 4, 5]); // => [[1, 2, 3, 4], [5]]

splitWhen

Splits a given array at the first index where the given predicate returns true.

Data First

splitWhen(array, fn);
Parameters
data
The array to split.
predicate
The predicate.
Returns
Object
splitWhen([1, 2, 3], (x) => x === 2); // => [[1], [2, 3]]

Data Last

splitWhen(fn)(array);
Parameters
predicate
The predicate.
Returns
Object
splitWhen((x) => x === 2)([1, 2, 3]); // => [[1], [2, 3]]

sumBy

Returns the sum of the elements of an array using the provided mapper.

Works for both number and bigint mappers, but not mappers that return both types.

IMPORTANT: The result for empty arrays would be 0 (number) regardless of the type of the mapper; to avoid adding this to the return type for cases where the array is known to be non-empty you can use hasAtLeast or isEmpty to guard against this case.

Data First

sumBy(array, fn);
Parameters
data
The array.
callbackfn
Predicate function.
Returns
Object
sumBy([{ a: 5 }, { a: 1 }, { a: 3 }], (x) => x.a); // 9
sumBy([{ a: 5n }, { a: 1n }, { a: 3n }], (x) => x.a); // 9n

Data Last

sumBy(fn)(array);
Parameters
callbackfn
Predicate function.
Returns
Object
pipe(
  [{ a: 5 }, { a: 1 }, { a: 3 }],
  sumBy((x) => x.a),
); // 9

pipe(
  [{ a: 5n }, { a: 1n }, { a: 3n }],
  sumBy((x) => x.a),
); // 9n

swapIndices

Swaps the positions of two elements in an array or string at the provided indices.

Negative indices are supported and would be treated as an offset from the end of the array. The resulting type thought would be less strict than when using positive indices.

If either index is out of bounds the result would be a shallow copy of the input, as-is.

Data First

swapIndices(data, index1, index2);
Parameters
data
The item to be manipulated. This can be an array, or a string.
index1
The first index.
index2
The second index.
Returns
Object
swapIndices(["a", "b", "c"], 0, 1); // => ['b', 'a', 'c']
swapIndices(["a", "b", "c"], 1, -1); // => ['a', 'c', 'b']
swapIndices("abc", 0, 1); // => 'bac'

Data Last

swapIndices(index1, index2)(data);
Parameters
index1
The first index.
index2
The second index.
Returns
Object
swapIndices(0, 1)(["a", "b", "c"]); // => ['b', 'a', 'c']
swapIndices(0, -1)("abc"); // => 'cba'

take

Lazy

Returns the first n elements of array.

Data First

take(array, n);
Parameters
array
The array.
n
The number of elements to take.
Returns
Array
take([1, 2, 3, 4, 3, 2, 1], 3); // => [1, 2, 3]

Data Last

take(n)(array);
Parameters
n
The number of elements to take.
Returns
Object
pipe([1, 2, 3, 4, 3, 2, 1], take(3)); // => [1, 2, 3]

takeFirstBy

Take the first n items from data based on the provided ordering criteria. This allows you to avoid sorting the array before taking the items. The complexity of this function is O(Nlogn) where N is the length of the array.

For the opposite operation (to drop n elements) see dropFirstBy.

Data First

takeFirstBy(data, n, ...rules);
Parameters
data
The input array.
n
The number of items to take. If `n` is non-positive no items would be returned, if `n` is bigger then data.length a *clone* of `data` would be returned.
rules
A variadic array of order rules defining the sorting criteria. Each order rule is a projection function that extracts a comparable value from the data. Sorting is based on these extracted values using the native `<` and `>` operators. Earlier rules take precedence over later ones. Use the syntax `[projection, "desc"]` for descending order.
Returns
Array
takeFirstBy(["aa", "aaaa", "a", "aaa"], 2, (x) => x.length); // => ['aa', 'a']

Data Last

takeFirstBy(n, ...rules)(data);
Parameters
n
The number of items to take. If `n` is non-positive no items would be returned, if `n` is bigger then data.length a *clone* of `data` would be returned.
rules
A variadic array of order rules defining the sorting criteria. Each order rule is a projection function that extracts a comparable value from the data. Sorting is based on these extracted values using the native `<` and `>` operators. Earlier rules take precedence over later ones. Use the syntax `[projection, "desc"]` for descending order.
Returns
Object
pipe(
  ["aa", "aaaa", "a", "aaa"],
  takeFirstBy(2, (x) => x.length),
); // => ['aa', 'a']

takeLast

Takes the last n elements from the array.

Data First

takeLast(array, n);
Parameters
array
The target array.
n
The number of elements to take.
Returns
Array
takeLast([1, 2, 3, 4, 5], 2); // => [4, 5]

Data Last

takeLast(n)(array);
Parameters
n
The number of elements to take.
Returns
Object
takeLast(2)([1, 2, 3, 4, 5]); // => [4, 5]

takeLastWhile

Returns elements from the end of the array until the predicate returns false. The returned elements will be in the same order as in the original array.

Data First

takeLastWhile(data, predicate);
Parameters
data
The array.
predicate
The predicate.
Returns
Array
takeLastWhile([1, 2, 10, 3, 4, 5], (x) => x < 10); // => [3, 4, 5]

Data Last

takeLastWhile(predicate)(data);
Parameters
predicate
The predicate.
Returns
Object
pipe(
  [1, 2, 10, 3, 4, 5],
  takeLastWhile((x) => x < 10),
); // => [3, 4, 5]

takeWhile

Returns elements from the array until predicate returns false.

Data First

takeWhile(data, predicate);
Parameters
data
The array.
predicate
The predicate.
Returns
Array
takeWhile([1, 2, 3, 4, 3, 2, 1], (x) => x !== 4); // => [1, 2, 3]

Data Last

takeWhile(predicate)(data);
Parameters
predicate
The predicate.
Returns
Object
pipe(
  [1, 2, 3, 4, 3, 2, 1],
  takeWhile((x) => x !== 4),
); // => [1, 2, 3]

times

Calls an input function n times, returning an array containing the results of those function calls.

fn is passed one argument: The current value of n, which begins at 0 and is gradually incremented to n - 1.

Data First

times(count, fn);
Parameters
count
A value between `0` and `n - 1`. Increments after each function call.
fn
The function to invoke. Passed one argument, the current value of `n`.
Returns
Object
times(5, identity()); //=> [0, 1, 2, 3, 4]

Data Last

times(fn)(count);
Parameters
fn
The function to invoke. Passed one argument, the current value of `n`.
Returns
Object
times(identity())(5); //=> [0, 1, 2, 3, 4]

unique

Lazy

Returns a new array containing only one copy of each element in the original list. Elements are compared by reference using Set.

Data First

unique(array);
Parameters
data
The array to filter.
Returns
Object
unique([1, 2, 2, 5, 1, 6, 7]); // => [1, 2, 5, 6, 7]

Data Last

unique()(array);
Parameters
Returns
Object
pipe(
  [1, 2, 2, 5, 1, 6, 7], // only 4 iterations
  unique(),
  take(3),
); // => [1, 2, 5]

uniqueBy

Lazy

Returns a new array containing only one copy of each element in the original list transformed by a function. Elements are compared by reference using Set.

Data First

uniqueBy(data, keyFunction);
Parameters
data
The array to filter.
keyFunction
Extracts a value that would be used to compare elements.
Returns
Object
uniqueBy(
  [{ n: 1 }, { n: 2 }, { n: 2 }, { n: 5 }, { n: 1 }, { n: 6 }, { n: 7 }],
  (obj) => obj.n,
); // => [{n: 1}, {n: 2}, {n: 5}, {n: 6}, {n: 7}]

Data Last

uniqueBy(keyFunction)(data);
Parameters
keyFunction
Extracts a value that would be used to compare elements.
Returns
Object
pipe(
  [{ n: 1 }, { n: 2 }, { n: 2 }, { n: 5 }, { n: 1 }, { n: 6 }, { n: 7 }], // only 4 iterations
  uniqueBy((obj) => obj.n),
  take(3),
); // => [{n: 1}, {n: 2}, {n: 5}]

uniqueWith

Lazy

Returns a new array containing only one copy of each element in the original list. Elements are compared by custom comparator isEquals.

Data First

uniqueWith(array, isEquals);
Parameters
data
The array to filter.
isEquals
The comparator.
Returns
Object
uniqueWith(
  [{ a: 1 }, { a: 2 }, { a: 2 }, { a: 5 }, { a: 1 }, { a: 6 }, { a: 7 }],
  equals,
); // => [{a: 1}, {a: 2}, {a: 5}, {a: 6}, {a: 7}]

Data Last

uniqueWith(isEquals)(array);
Parameters
isEquals
The comparator.
Returns
Object
uniqueWith(equals)([
  { a: 1 },
  { a: 2 },
  { a: 2 },
  { a: 5 },
  { a: 1 },
  { a: 6 },
  { a: 7 },
]); // => [{a: 1}, {a: 2}, {a: 5}, {a: 6}, {a: 7}]
pipe(
  [{ a: 1 }, { a: 2 }, { a: 2 }, { a: 5 }, { a: 1 }, { a: 6 }, { a: 7 }], // only 4 iterations
  uniqueWith(equals),
  take(3),
); // => [{a: 1}, {a: 2}, {a: 5}]

zip

Lazy

Creates a new list from two supplied lists by pairing up equally-positioned items. The length of the returned list will match the shortest of the two inputs.

Data First

zip(first, second);
Parameters
first
The first input list.
second
The second input list.
Returns
Object
zip([1, 2], ["a", "b"]); // => [[1, 'a'], [2, 'b']]

Data Last

zip(second)(first);
Parameters
second
The second input list.
Returns
Object
zip(["a", "b"])([1, 2]); // => [[1, 'a'], [2, 'b']]

zipWith

Creates a new list from two supplied lists by calling the supplied function with the same-positioned element from each list.

zipWith(fn)(first, second);
Parameters
fn
The function applied to each position of the list.
Returns
Object
zipWith((a: string, b: string) => a + b)(["1", "2", "3"], ["a", "b", "c"]); // => ['1a', '2b', '3c']

Data First

zipWith(first, second, fn);
Parameters
first
The first input list.
second
The second input list.
fn
The function applied to each position of the list.
Returns
Array
zipWith(["1", "2", "3"], ["a", "b", "c"], (a, b) => a + b); // => ['1a', '2b', '3c']

Data Last

zipWith(second, fn)(first);
Parameters
second
The second input list.
fn
The function applied to each position of the list.
Returns
Object
pipe(
  ["1", "2", "3"],
  zipWith(["a", "b", "c"], (a, b) => a + b),
); // => ['1a', '2b', '3c']

function

conditional

Executes a transformer function based on the first matching predicate, functioning like a series of if...else if... statements. It sequentially evaluates each case and, upon finding a truthy predicate, runs the corresponding transformer, and returns, ignoring any further cases, even if they would match.

NOTE: Some type-predicates may fail to narrow the param type of their transformer; in such cases wrap your type-predicate in an anonymous arrow function: e.g., instead of conditional(..., [myTypePredicate, myTransformer], ...), use conditional(..., [($) => myTypePredicate($), myTransformer], ...).

To add a a default, catch-all, case you can provide a single callback function (instead of a 2-tuple) as the last case. This is equivalent to adding a case with a trivial always-true predicate as it's condition (see example).

For simpler cases you should also consider using when instead.

Due to TypeScript's inability to infer the result of negating a type- predicate we can't refine the types used in subsequent cases based on previous conditions. Using a switch (true) statement or ternary operators is recommended for more precise type control when such type narrowing is needed.

!IMPORTANT! - Unlike similar implementations in Lodash and Ramda, the Remeda implementation doesn't implicitly return undefined as a fallback when when none of the cases match; and instead throws an exception in those cases! You have to explicitly provide a default case, and can use constant(undefined) as your last case to replicate that behavior.

Data First

conditional(data, ...cases);
Parameters
data
The input data to be evaluated against the provided cases.
case0
fallback
Returns
Object
const nameOrId = 3 as string | number | boolean;

conditional(
  nameOrId,
  [isString, (name) => `Hello ${name}`],
  [isNumber, (id) => `Hello ID: ${id}`],
); //=> 'Hello ID: 3' (typed as `string`), can throw!.

conditional(
  nameOrId,
  [isString, (name) => `Hello ${name}`],
  [isNumber, (id) => `Hello ID: ${id}`],
  constant(undefined),
); //=> 'Hello ID: 3' (typed as `string | undefined`), won't throw.

conditional(
  nameOrId,
  [isString, (name) => `Hello ${name}`],
  [isNumber, (id) => `Hello ID: ${id}`],
  (something) => `Hello something (${JSON.stringify(something)})`,
); //=> 'Hello ID: 3' (typed as `string`), won't throw.

Data Last

conditional(...cases)(data);
Parameters
case0
fallback
Returns
Object
const nameOrId = 3 as string | number | boolean;

pipe(
  nameOrId,
  conditional(
    [isString, (name) => `Hello ${name}`],
    [isNumber, (id) => `Hello ID: ${id}`],
  ),
); //=> 'Hello ID: 3' (typed as `string`), can throw!.

pipe(
  nameOrId,
  conditional(
    [isString, (name) => `Hello ${name}`],
    [isNumber, (id) => `Hello ID: ${id}`],
    constant(undefined),
  ),
); //=> 'Hello ID: 3' (typed as `string | undefined`), won't throw.

pipe(
  nameOrId,
  conditional(
    [isString, (name) => `Hello ${name}`],
    [isNumber, (id) => `Hello ID: ${id}`],
    (something) => `Hello something (${JSON.stringify(something)})`,
  ),
); //=> 'Hello ID: 3' (typed as `string`), won't throw.

constant

A function that takes any arguments and returns the provided value on every invocation. This is useful to provide trivial implementations for APIs or in combination with a ternary or other conditional execution to allow to short- circuit more complex implementations for a specific case.

Notice that this is a dataLast impl where the function needs to be invoked to get the "do nothing" function.

See also: doNothing - A function that doesn't return anything. identity - A function that returns the first argument it receives.

Data Last

constant(value);
Parameters
value
The constant value that would be returned on every invocation. The value is not copied/cloned on every invocation so care should be taken with mutable objects (like arrays, objects, Maps, etc...).
Returns
Object
map([1, 2, 3], constant("a")); // => ['a', 'a', 'a']
map([1, 2, 3], isDemoMode ? add(1) : constant(0)); // => [2, 3, 4] or [0, 0, 0]

debounce

Wraps func with a debouncer object that "debounces" (delays) invocations of the function during a defined cool-down period (waitMs). It can be configured to invoke the function either at the start of the cool-down period, the end of it, or at both ends (timing). It can also be configured to allow invocations during the cool-down period (maxWaitMs). It stores the latest call's arguments so they could be used at the end of the cool-down period when invoking func (if configured to invoke the function at the end of the cool-down period). It stores the value returned by func whenever its invoked. This value is returned on every call, and is accessible via the cachedValue property of the debouncer. Its important to note that the value might be different from the value that would be returned from running func with the current arguments as it is a cached value from a previous invocation. Important: The cool-down period defines the minimum between two invocations, and not the maximum. The period will be extended each time a call is made until a full cool-down period has elapsed without any additional calls.

! DEPRECATED: This implementation of debounce is known to have issues and might not behave as expected. It should be replaced with the funnel utility instead. The test file funnel.remeda-debounce.test.ts offers a reference implementation that replicates debounce via funnel!

Data First

debounce(func, options);
Parameters
func
The function to debounce, the returned `call` function will have the exact same signature.
options
An object allowing further customization of the debouncer: - `timing?: 'leading' | 'trailing' |'both'`. The default is `'trailing'`. `leading` would result in the function being invoked at the start of the cool-down period; `trailing` would result in the function being invoked at the end of the cool-down period (using the args from the last call to the debouncer). When `both` is selected the `trailing` invocation would only take place if there were more than one call to the debouncer during the cool-down period. **DEFAULT: 'trailing'** - `waitMs?: number`. The length of the cool-down period in milliseconds. The debouncer would wait until this amount of time has passed without **any** additional calls to the debouncer before triggering the end-of-cool-down- period event. When this happens, the function would be invoked (if `timing` isn't `'leading'`) and the debouncer state would be reset. **DEFAULT: 0** - `maxWaitMs?: number`. The length of time since a debounced call (a call that the debouncer prevented from being invoked) was made until it would be invoked. Because the debouncer can be continually triggered and thus never reach the end of the cool-down period, this allows the function to still be invoked occasionally. IMPORTANT: This param is ignored when `timing` is `'leading'`.
Returns
Object
const debouncer = debounce(identity(), { timing: "trailing", waitMs: 1000 });
const result1 = debouncer.call(1); // => undefined
const result2 = debouncer.call(2); // => undefined
// after 1 second
const result3 = debouncer.call(3); // => 2
// after 1 second
debouncer.cachedValue; // => 3

doNothing

A function that takes any arguments and does nothing with them. This is useful as a placeholder for any function or API that requires a void function (a function that doesn't return a value). This could also be used in combination with a ternary or other conditional execution to allow disabling a function call for a specific case.

Notice that this is a dataLast impl where the function needs to be invoked to get the "do nothing" function.

See also:

  • constant - A function that ignores it's arguments and returns the same value on every invocation.
  • identity - A function that returns the first argument it receives.

Data Last

doNothing();
Parameters
Returns
Object
myApi({ onSuccess: handleSuccess, onError: doNothing() });
myApi({ onSuccess: isDemoMode ? doNothing() : handleSuccess });

funnel

Creates a funnel that controls the timing and execution of callback. Its main purpose is to manage multiple consecutive (usually fast-paced) calls, reshaping them according to a defined batching strategy and timing policy. This is useful when handling uncontrolled call rates, such as DOM events or network traffic. It can implement strategies like debouncing, throttling, batching, and more.

An optional reducer function can be provided to allow passing data to the callback via calls to call (otherwise the signature of call takes no arguments).

Typing is inferred from callbacks param, and from the rest params that the optional reducer function accepts. Use explicit types for these to ensure that everything else is well-typed.

Notice that this function constructs a funnel object, and does not execute anything when called. The returned object should be used to execute the funnel via the its call method.

  • Debouncing: use minQuietPeriodMs and any triggerAt.
  • Throttling: use minGapMs and triggerAt: "start" or "both".
  • Batching: See the reference implementation in funnel.reference-batch.test.ts.
funnel(callback, options);
Parameters
callback
The main function that would be invoked periodically based on `options`. The function would take the latest result of the `reducer`; if no calls where made since the last time it was invoked it will not be invoked. (If a return value is needed, it should be passed via a reference or via closure to the outer scope of the funnel).
options
An object that defines when `execute` should be invoked, relative to the calls of `call`. An empty/missing options object is equivalent to setting `minQuietPeriodMs` to `0`.
Returns
Object
const debouncer = funnel(
  () => {
    console.log("Callback executed!");
  },
  { minQuietPeriodMs: 100 },
);
debouncer.call();
debouncer.call();

const throttle = funnel(
  () => {
    console.log("Callback executed!");
  },
  { minGapMs: 100, triggerAt: "start" },
);
throttle.call();
throttle.call();

identity

A function that returns the first argument passed to it.

Notice that this is a dataLast impl where the function needs to be invoked to get the "do nothing" function.

See also:

  • doNothing - A function that doesn't return anything.
  • constant - A function that ignores the input arguments and returns the same value on every invocation.
identity();
Parameters
Returns
Object
map([1, 2, 3], identity()); // => [1,2,3]

once

Creates a function that is restricted to invoking func once. Repeat calls to the function return the value of the first invocation.

once(fn);
Parameters
fn
The function to wrap.
Returns
Object
const initialize = once(createApplication);
initialize();
initialize();
// => `createApplication` is invoked once

partialBind

Creates a function that calls func with partial put before the arguments it receives.

Can be thought of as "freezing" some portion of a function's arguments, resulting in a new function with a simplified signature.

Data First

partialBind(func, ...partial);
Parameters
func
The function to wrap.
partial
The arguments to put before.
Returns
Object
const fn = (x: number, y: number, z: number) => x * 100 + y * 10 + z;
const partialFn = partialBind(fn, 1, 2);
partialFn(3); //=> 123

const logWithPrefix = partialBind(console.log, "[prefix]");
logWithPrefix("hello"); //=> "[prefix] hello"

partialLastBind

Creates a function that calls func with partial put after the arguments it receives. Note that this doesn't support functions with both optional and rest parameters.

Can be thought of as "freezing" some portion of a function's arguments, resulting in a new function with a simplified signature.

Useful for converting a data-first function to a data-last one.

Data First

partialLastBind(func, ...partial);
Parameters
func
The function to wrap.
partial
The arguments to put after.
Returns
Object
const fn = (x: number, y: number, z: number) => x * 100 + y * 10 + z;
const partialFn = partialLastBind(fn, 2, 3);
partialFn(1); //=> 123

const parseBinary = partialLastBind(parseInt, "2");
parseBinary("101"); //=> 5

pipe(
  { a: 1 },
  // instead of (arg) => JSON.stringify(arg, null, 2)
  partialLastBind(JSON.stringify, null, 2),
); //=> '{\n  "a": 1\n}'

pipe

Performs left-to-right function composition, passing data through functions in sequence. Each function receives the output of the previous function, creating a readable top-to-bottom data flow that matches how the transformation is executed. This enables converting deeply nested function calls into clear, sequential steps without temporary variables.

When consecutive functions with a lazy tag (e.g., map, filter, take, drop, forEach, etc...) are used together, they process data item-by-item rather than creating intermediate arrays. This enables early termination when only partial results are needed, improving performance for large datasets and expensive operations.

Functions are only evaluated lazily when their data-last form is used directly in the pipe. To disable lazy evaluation, use data-first calls via arrow functions: ($) => map($, callback) instead of map(callback).

Any function can be used in pipes, not just Remeda utilities. For creating custom functions with currying and lazy evaluation support, see the purry utility.

A "headless" variant piped is available for creating reusable pipe functions without initial data.

IMPORTANT: During lazy evaluation, callbacks using the third parameter (the input array) receive only items processed up to that point, not the complete array.

Data First

pipe(data, ...functions);
Parameters
data
The input data.
Returns
Object
pipe([1, 2, 3], map(multiply(3))); //=> [3, 6, 9]

// = Early termination with lazy evaluation =
pipe(
  hugeArray,
  map(expensiveComputation),
  filter(complexPredicate),
  // Only processes items until 2 results are found, then stops.
  // Most of hugeArray never gets processed.
  take(2),
);

// = Custom logic within a pipe =
pipe(
  input,
  toLowerCase(),
  normalize,
  ($) => validate($, CONFIG),
  split(","),
  unique(),
);

// = Migrating nested transformations to pipes =
// Nested
const result = prop(
  mapValues(groupByProp(users, "department"), length()),
  "engineering",
);

// Piped
const result = pipe(
  users,
  groupByProp("department"),
  mapValues(length()),
  prop("engineering"),
);

// = Using the 3rd param of a callback =
// The following would print out `data` in its entirety for each value
// of `data`.
forEach([1, 2, 3, 4], (_item, _index, data) => {
  console.log(data);
}); //=> "[1, 2, 3, 4]" logged 4 times

// But with `pipe` data would only contain the items up to the current
// index
pipe(
  [1, 2, 3, 4],
  forEach((_item, _index, data) => {
    console.log(data);
  }),
); //=> "[1]", "[1, 2]", "[1, 2, 3]", "[1, 2, 3, 4]"

piped

Data-last version of pipe. See pipe documentation for full details.

Use piped when you need to pass a transformation as a callback to functions like map and filter, where the data type can be inferred from the call site.

IMPORTANT: piped does not work as a "function factory" in order to create standalone utility functions; because TypeScript cannot infer the input data type (without requiring to explicitly define all type params for all functions in the pipe). We recommend defining the function explicitly, and then use pipe in its implementation.

Data Last

piped(...functions)(data);
Parameters
Returns
Object
map([{ a: 1 }, { a: 2 }, { a: 3 }], piped(prop("a"), add(1))); //=> [2, 3, 4]

purry

Creates a function with dataFirst and dataLast signatures.

purry is a dynamic function and it's not type safe. It should be wrapped by a function that have proper typings. Refer to the example below for correct usage.

!IMPORTANT: functions that simply call purry and return the result (like almost all functions in this library) should return unknown themselves if an explicit return type is required. This is because we currently don't provide a generic return type that is built from the input function, and crafting one manually isn't worthwhile as we rely on function declaration overloading to combine the types for dataFirst and dataLast invocations!

purry(fn, args);
Parameters
fn
The function to purry.
args
The arguments.
lazy
A lazy version of the function to purry.
Returns
unknown
function _findIndex(array, fn) {
  for (let i = 0; i < array.length; i++) {
    if (fn(array[i])) {
      return i;
    }
  }
  return -1;
}

// data-first
function findIndex<T>(array: T[], fn: (item: T) => boolean): number;

// data-last
function findIndex<T>(fn: (item: T) => boolean): (array: T[]) => number;

function findIndex(...args: unknown[]) {
  return purry(_findIndex, args);
}

when

Conditionally run a function based on a predicate, returning its result (similar to the ?: (ternary) operator.) If the optional onFalse function is not provided, the data will be passed through in those cases.

Supports type predicates to refine the types for both branches and the return value.

Additional arguments are passed to all functions. In data-first calls, they are taken as variadic arguments; but in data-last calls, they are when the curried function itself is called.

For more complex cases check out conditional.

Data First

when(data, predicate, onTrue, ...extraArgs);
Parameters
data
The data to be passed to all functions, as the first param.
predicate
Decides if the `onTrue` mapper should run or not. If it's a type predicate it also narrows types for the mappers and the return value.
onTrue
The function that would run when the predicate returns `true`.
extraArgs
Additional arguments. These would be passed as is to the `predicate`, `onTrue`, and `onFalse` functions.
Returns
Object
when(data, isNullish, constant(42));
when(data, (x) => x > 3, { onTrue: add(1), onFalse: multiply(2) });
when(data, isString, (x, radix) => parseInt(x, radix), 10);

Data Last

when(predicate, onTrue)(data, ...extraArgs);
Parameters
predicate
Decides if the `onTrue` mapper should run or not. If it's a type predicate it also narrows types for the mappers and the return value.
onTrue
Function to run when the predicate returns `true`.
Returns
Object
pipe(data, when(isNullish, constant(42)));
pipe(
  data,
  when((x) => x > 3, { onTrue: add(1), onFalse: multiply(2) }),
);
map(
  data,
  when(isNullish, (x, index) => x + index),
);

guard

hasProp

Checks if data has a prop key.

Uses Object.hasOwn which only checks own properties, skipping properties inherited through the prototype chain.

  • Use prop to read the value.

Data First

hasProp(data, key);
Parameters
data
The object to test.
key
The key to look up.
Returns
boolean
hasProp({ a: 1 }, "a"); //=> true

Data Last

hasProp(key)(data);
Parameters
key
The key to look up.
Returns
Object
pipe({ a: 1 }, hasProp("a")); //=> true
filter([] as { a?: number }[], hasProp("a")); //=> typed as { a: number }[]

hasSubObject

Checks if subObject is a sub-object of object, which means for every property and value in subObject, there's the same property in object with an equal value. Equality is checked with isDeepEqual.

Data First

hasSubObject(data, subObject);
Parameters
data
The object to test.
subObject
The sub-object to test against.
Returns
boolean
hasSubObject({ a: 1, b: 2, c: 3 }, { a: 1, c: 3 }); //=> true
hasSubObject({ a: 1, b: 2, c: 3 }, { b: 4 }); //=> false
hasSubObject({ a: 1, b: 2, c: 3 }, {}); //=> true

Data Last

hasSubObject(subObject)(data);
Parameters
subObject
The sub-object to test against.
Returns
Object
hasSubObject({ a: 1, c: 3 })({ a: 1, b: 2, c: 3 }); //=> true
hasSubObject({ b: 4 })({ a: 1, b: 2, c: 3 }); //=> false
hasSubObject({})({ a: 1, b: 2, c: 3 }); //=> true

isArray

A function that checks if the passed parameter is an Array and narrows its type accordingly.

isArray(data);
Parameters
data
The variable to check.
Returns
boolean
isArray([5]); //=> true
isArray([]); //=> true
isArray("somethingElse"); //=> false

isBigInt

A function that checks if the passed parameter is a bigint and narrows its type accordingly.

isBigInt(data);
Parameters
data
The variable to check.
Returns
boolean
isBigInt(1n); // => true
isBigInt(1); // => false
isBigInt("notANumber"); // => false

isBoolean

A function that checks if the passed parameter is a boolean and narrows its type accordingly.

isBoolean(data);
Parameters
data
The variable to check.
Returns
boolean
isBoolean(true); //=> true
isBoolean(false); //=> true
isBoolean("somethingElse"); //=> false

isDate

A function that checks if the passed parameter is a Date and narrows its type accordingly.

isDate(data);
Parameters
data
The variable to check.
Returns
boolean
isDate(new Date()); //=> true
isDate("somethingElse"); //=> false

isDeepEqual

Performs a deep structural comparison between two values to determine if they are equivalent. For primitive values this is equivalent to ===, for arrays the check would be performed on every item recursively, in order, and for objects all props will be compared recursively.

The built-in Date and RegExp are special-cased and will be compared by their values. Objects with a null prototype are comparable to plain objects by their enumerable properties.

!IMPORTANT: TypedArrays and symbol properties of objects are not supported right now and might result in unexpected behavior. Please open an issue in the Remeda github project if you need support for these types.

The result would be narrowed to the second value so that the function can be used as a type guard.

See:

  • isStrictEqual if you don't need a deep comparison and just want to check for simple (===, Object.is) equality.
  • isShallowEqual if you need to compare arrays and objects "by-value" but don't want to recurse into their values.

Data First

isDeepEqual(data, other);
Parameters
data
The first value to compare.
other
The second value to compare.
Returns
boolean
isDeepEqual(1, 1); //=> true
isDeepEqual(1, "1"); //=> false
isDeepEqual([1, 2, 3], [1, 2, 3]); //=> true

Data Last

isDeepEqual(other)(data);
Parameters
other
The second value to compare.
Returns
Object
pipe(1, isDeepEqual(1)); //=> true
pipe(1, isDeepEqual("1")); //=> false
pipe([1, 2, 3], isDeepEqual([1, 2, 3])); //=> true

isDefined

A function that checks if the passed parameter is defined (!== undefined) and narrows its type accordingly.

isDefined(data);
Parameters
data
The variable to check.
Returns
boolean
isDefined("string"); //=> true
isDefined(null); //=> true
isDefined(undefined); //=> false

isEmpty

A function that checks if the passed parameter is empty.

This function has limited utility at the type level because negating it does not yield a useful type in most cases because of TypeScript limitations. Additionally, utilities which accept a narrower input type provide better type-safety on their inputs. In most cases, you should use one of the following functions instead:

  • isEmptyish - supports a wider range of cases, accepts any input including nullish values, and does a better job at narrowing the result.
  • hasAtLeast - when the input is just an array/tuple.
  • isStrictEqual - when you just need to check for a specific literal value.
  • isNullish - when you just care about null and undefined.
  • isTruthy - when you need to also filter number and boolean.
isEmpty(data);
Parameters
data
The variable to check.
Returns
boolean
isEmpty(""); //=> true
isEmpty([]); //=> true
isEmpty({}); //=> true

isEmpty("test"); //=> false
isEmpty([1, 2, 3]); //=> false
isEmpty({ a: "hello" }); //=> false

isEmpty(undefined); // Deprecated: use `isEmptyish`
Parameters
data
Returns
boolean

isEmptyish

A function that checks if the input is empty. Empty is defined as anything exposing a numerical length, or size property that is equal to 0. This definition covers strings, arrays, Maps, Sets, plain objects, and custom classes. Additionally, null and undefined are also considered empty.

number, bigint, boolean, symbol, and function will always return false. RegExp, Date, and weak collections will always return true. Classes and Errors are treated as plain objects: if they expose any public property they would be considered non-empty, unless they expose a numerical length or size property, which defines their emptiness regardless of other properties.

This function has limited utility at the type level because negating it does not yield a useful type in most cases because of TypeScript limitations. Additionally, utilities which accept a narrower input type provide better type-safety on their inputs. In most cases, you should use one of the following functions instead:

  • isEmpty - provides better type-safety on inputs by accepting a narrower set of cases.
  • hasAtLeast - when the input is just an array/tuple.
  • isStrictEqual - when you just need to check for a specific literal value.
  • isNullish - when you just care about null and undefined.
  • isTruthy - when you need to also filter number and boolean.
isEmptyish(data);
Parameters
data
The variable to check.
Returns
boolean
isEmptyish(undefined); //=> true
isEmptyish(null); //=> true
isEmptyish(""); //=> true
isEmptyish([]); //=> true
isEmptyish({}); //=> true
isEmptyish(new Map()); //=> true
isEmptyish(new Set()); //=> true
isEmptyish({ a: "hello", size: 0 }); //=> true
isEmptyish(/abc/); //=> true
isEmptyish(new Date()); //=> true
isEmptyish(new WeakMap()); //=> true

isEmptyish("test"); //=> false
isEmptyish([1, 2, 3]); //=> false
isEmptyish({ a: "hello" }); //=> false
isEmptyish({ length: 1 }); //=> false
isEmptyish(0); //=> false
isEmptyish(true); //=> false
isEmptyish(() => {}); //=> false

isError

A function that checks if the passed parameter is an Error and narrows its type accordingly.

isError(data);
Parameters
data
The variable to check.
Returns
boolean
isError(new Error("message")); //=> true
isError("somethingElse"); //=> false

isFunction

A function that checks if the passed parameter is a Function and narrows its type accordingly.

isFunction(data);
Parameters
data
The variable to check.
Returns
boolean
isFunction(() => {}); //=> true
isFunction("somethingElse"); //=> false

isIncludedIn

Checks if the item is included in the container. This is a wrapper around Array.prototype.includes and Set.prototype.has and thus relies on the same equality checks that those functions do (which is reference equality, e.g. ===). In some cases the input's type is also narrowed to the container's item types.

Notice that unlike most functions, this function takes a generic item as it's data and an array as it's parameter.

Data First

isIncludedIn(data, container);
Parameters
data
The item that is checked.
container
The items that are checked against.
Returns
boolean
isIncludedIn(2, [1, 2, 3]); // => true
isIncludedIn(4, [1, 2, 3]); // => false

const data = "cat" as "cat" | "dog" | "mouse";
isIncludedIn(data, ["cat", "dog"] as const); // true (typed "cat" | "dog");

Data Last

isIncludedIn(container)(data);
Parameters
container
The items that are checked against.
Returns
Object
pipe(2, isIncludedIn([1, 2, 3])); // => true
pipe(4, isIncludedIn([1, 2, 3])); // => false

const data = "cat" as "cat" | "dog" | "mouse";
pipe(data, isIncludedIn(["cat", "dog"] as const)); // => true (typed "cat" | "dog");

isNonNull

A function that checks if the passed parameter is not null and narrows its type accordingly. Notice that undefined is not null!

isNonNull(data);
Parameters
data
The variable to check.
Returns
boolean
isNonNull("string"); //=> true
isNonNull(null); //=> false
isNonNull(undefined); //=> true

isNonNullish

A function that checks if the passed parameter is defined AND isn't null and narrows its type accordingly.

isNonNullish(data);
Parameters
data
The variable to check.
Returns
boolean
isNonNullish("string"); //=> true
isNonNullish(null); //=> false
isNonNullish(undefined); //=> false

isNot

A function that takes a guard function as predicate and returns a guard that negates it.

Data Last

isNot(isTruthy)(data);
Parameters
predicate
The guard function to negate.
Returns
Object
isNot(isTruthy)(false); //=> true
isNot(isTruthy)(true); //=> false

isNullish

A function that checks if the passed parameter is either null or undefined and narrows its type accordingly.

isNullish(data);
Parameters
data
The variable to check.
Returns
boolean
isNullish(undefined); //=> true
isNullish(null); //=> true
isNullish("somethingElse"); //=> false

isNumber

A function that checks if the passed parameter is a number and narrows its type accordingly.

isNumber(data);
Parameters
data
The variable to check.
Returns
boolean
isNumber(1); // => true
isNumber(1n); // => false
isNumber("notANumber"); // => false

isObjectType

Checks if the given parameter is of type "object" via typeof, excluding null.

It's important to note that in JavaScript, many entities are considered objects, like Arrays, Classes, RegExps, Maps, Sets, Dates, URLs, Promise, Errors, and more. Although technically an object too, null is not considered an object by this function, so that its easier to narrow nullables.

For a more specific check that is limited to plain objects (simple struct/shape/record-like objects), consider using isPlainObject instead. For a simpler check that only removes null from the type prefer isNonNull or isDefined.

Data First

isObjectType(data);
Parameters
data
The variable to be checked for being an object type.
Returns
boolean
// true
isObjectType({}); //=> true
isObjectType([]); //=> true
isObjectType(Promise.resolve("something")); //=> true
isObjectType(new Date()); //=> true
isObjectType(new Error("error")); //=> true

// false
isObjectType("somethingElse"); //=> false
isObjectType(null); //=> false

isPlainObject

Checks if data is a "plain" object. A plain object is defined as an object with string keys and values of any type, including primitives, other objects, functions, classes, etc (aka struct/shape/record/simple). Technically, a plain object is one whose prototype is either Object.prototype or null, ensuring it does not inherit properties or methods from other object types.

This function is narrower in scope than isObjectType, which accepts any entity considered an "object" by JavaScript's typeof.

Note that Maps, Arrays, and Sets are not considered plain objects and would return false.

isPlainObject(data);
Parameters
data
The variable to check.
Returns
boolean
// true
isPlainObject({}); //=> true
isPlainObject({ a: 123 }); //=> true

// false
isPlainObject([]); //=> false
isPlainObject(Promise.resolve("something")); //=> false
isPlainObject(new Date()); //=> false
isPlainObject(new Error("error")); //=> false
isPlainObject("somethingElse"); //=> false
isPlainObject(null); //=> false

isPromise

A function that checks if the passed parameter is a Promise and narrows its type accordingly.

isPromise(data);
Parameters
data
The variable to check.
Returns
boolean
isPromise(Promise.resolve(5)); //=> true
isPromise(Promise.reject(5)); //=> true
isPromise("somethingElse"); //=> false

isShallowEqual

Performs a shallow structural comparison between two values to determine if they are equivalent. For primitive values this is equivalent to ===, for arrays a strict equality check would be performed on every item, in order, and for objects props will be matched and checked for strict equality; Unlike isDeepEqual where the function also recurses into each item and value.

!IMPORTANT: symbol properties of objects are not supported right now and might result in unexpected behavior. Please open an issue in the Remeda github project if you need support for these types.

!IMPORTANT: Promise, Date, and RegExp, are shallowly equal, even when they are semantically different (e.g. resolved promises); but isDeepEqual does compare the latter 2 semantically by-value.

The result would be narrowed to the second value so that the function can be used as a type guard.

See:

  • isStrictEqual if you don't need a deep comparison and just want to check for simple (===, Object.is) equality.
  • isDeepEqual for a recursively deep check of arrays and objects.

Data First

isShallowEqual(data, other);
Parameters
data
The first value to compare.
other
The second value to compare.
Returns
boolean
isShallowEqual(1, 1); //=> true
isShallowEqual(1, "1"); //=> false
isShallowEqual([1, 2, 3], [1, 2, 3]); //=> true
isShallowEqual([[1], [2], [3]], [[1], [2], [3]]); //=> false

Data First

isShallowEqual(other)(data);
Parameters
other
The second value to compare.
Returns
Object
pipe(1, isShallowEqual(1)); //=> true
pipe(1, isShallowEqual("1")); //=> false
pipe([1, 2, 3], isShallowEqual([1, 2, 3])); //=> true
pipe([[1], [2], [3]], isShallowEqual([[1], [2], [3]])); //=> false

isStrictEqual

Determines whether two values are functionally identical in all contexts. For primitive values (string, number), this is done by-value, and for objects it is done by-reference (i.e., they point to the same object in memory).

Under the hood we use both the === operator and Object.is. This means that isStrictEqual(NaN, NaN) === true (whereas NaN !== NaN), and isStrictEqual(-0, 0) === true (whereas Object.is(-0, 0) === false).

The result would be narrowed to the second value so that the function can be used as a type guard.

See:

  • isDeepEqual for a semantic comparison that allows comparing arrays and objects "by-value", and recurses for every item.
  • isShallowEqual if you need to compare arrays and objects "by-value" but don't want to recurse into their values.

Data First

isStrictEqual(data, other);
Parameters
data
The first value to compare.
other
The second value to compare.
Returns
boolean
isStrictEqual(1, 1); //=> true
isStrictEqual(1, "1"); //=> false
isStrictEqual([1, 2, 3], [1, 2, 3]); //=> false

Data Last

isStrictEqual(other)(data);
Parameters
other
The second value to compare.
Returns
Object
pipe(1, isStrictEqual(1)); //=> true
pipe(1, isStrictEqual("1")); //=> false
pipe([1, 2, 3], isStrictEqual([1, 2, 3])); //=> false

isString

A function that checks if the passed parameter is a string and narrows its type accordingly.

isString(data);
Parameters
data
The variable to check.
Returns
boolean
isString("string"); //=> true
isString(1); //=> false

isSymbol

A function that checks if the passed parameter is a symbol and narrows its type accordingly.

isSymbol(data);
Parameters
data
The variable to check.
Returns
boolean
isSymbol(Symbol("foo")); //=> true
isSymbol(1); //=> false

isTruthy

A function that checks if the passed parameter is truthy and narrows its type accordingly.

isTruthy(data);
Parameters
data
The variable to check.
Returns
boolean
isTruthy("somethingElse"); //=> true
isTruthy(null); //=> false
isTruthy(undefined); //=> false
isTruthy(false); //=> false
isTruthy(0); //=> false
isTruthy(""); //=> false

number

add

Adds two numbers.

Data First

add(value, addend);
Parameters
value
The number.
addend
The number to add to the value.
Returns
bigint
add(10, 5); // => 15
add(10, -5); // => 5

Data Last

add(addend)(value);
Parameters
addend
The number to add to the value.
Returns
Object
add(5)(10); // => 15
add(-5)(10); // => 5
map([1, 2, 3, 4], add(1)); // => [2, 3, 4, 5]

ceil

Rounds up a given number to a specific precision. If you'd like to round up to an integer (i.e. use this function with constant precision === 0), use Math.ceil instead, as it won't incur the additional library overhead.

Data First

ceil(value, precision);
Parameters
value
The number to round up.
precision
The precision to round up to. Must be an integer between -15 and 15.
Returns
number
ceil(123.9876, 3); // => 123.988
ceil(483.22243, 1); // => 483.3
ceil(8541, -1); // => 8550
ceil(456789, -3); // => 457000

Data Last

ceil(precision)(value);
Parameters
precision
The precision to round up to. Must be an integer between -15 and 15.
Returns
Object
ceil(3)(123.9876); // => 123.988
ceil(1)(483.22243); // => 483.3
ceil(-1)(8541); // => 8550
ceil(-3)(456789); // => 457000

clamp

Clamp the given value within the inclusive min and max bounds.

Data First

clamp(value, { min, max });
Parameters
value
The number.
limits
The bounds limits.
Returns
number
clamp(10, { min: 20 }); // => 20
clamp(10, { max: 5 }); // => 5
clamp(10, { max: 20, min: 5 }); // => 10

Data Last

clamp({ min, max })(value);
Parameters
limits
The bounds limits.
Returns
Object
clamp({ min: 20 })(10); // => 20
clamp({ max: 5 })(10); // => 5
clamp({ max: 20, min: 5 })(10); // => 10

divide

Divides two numbers.

Data First

divide(value, divisor);
Parameters
value
The number.
divisor
The number to divide the value by.
Returns
bigint
divide(12, 3); // => 4
reduce([1, 2, 3, 4], divide, 24); // => 1

Data Last

divide(divisor)(value);
Parameters
divisor
The number to divide the value by.
Returns
Object
divide(3)(12); // => 4
map([2, 4, 6, 8], divide(2)); // => [1, 2, 3, 4]

floor

Rounds down a given number to a specific precision. If you'd like to round down to an integer (i.e. use this function with constant precision === 0), use Math.floor instead, as it won't incur the additional library overhead.

Data First

floor(value, precision);
Parameters
value
The number to round down.
precision
The precision to round down to. Must be an integer between -15 and 15.
Returns
number
floor(123.9876, 3); // => 123.987
floor(483.22243, 1); // => 483.2
floor(8541, -1); // => 8540
floor(456789, -3); // => 456000

Data Last

floor(precision)(value);
Parameters
precision
The precision to round down to. Must be an integer between -15 and 15.
Returns
Object
floor(3)(123.9876); // => 123.987
floor(1)(483.22243); // => 483.2
floor(-1)(8541); // => 8540
floor(-3)(456789); // => 456000

mean

Returns the mean of the elements of an array.

Only number arrays are supported, as bigint is unable to represent fractional values.

IMPORTANT: The result for empty arrays would be undefined, regardless of the type of the array. This approach improves type-checking and ensures that cases where NaN might occur are handled properly. To avoid adding this to the return type for cases where the array is known to be non-empty you can use hasAtLeast or isEmpty to guard against this case.

Data First

mean(data);
Parameters
data
The array of numbers.
Returns
Object
mean([1, 2, 3]); // => 2
mean([]); // => undefined

Data Last

mean()(data);
Parameters
Returns
Object
pipe([1, 2, 3], mean()); // => 2
pipe([], mean()); // => undefined

median

Returns the median of the elements of an array.

Only number arrays are supported, as bigint is unable to represent fractional values.

IMPORTANT: The result for empty arrays would be undefined, regardless of the type of the array. This approach improves type-checking and ensures that cases where NaN might occur are handled properly. To avoid adding this to the return type for cases where the array is known to be non-empty you can use hasAtLeast or isEmpty to guard against this case.

Data First

median(data);
Parameters
data
The array of numbers.
Returns
Object
pipe([6, 10, 11], median()); // => 10
median([]); // => undefined

Data Last

median()(data);
Parameters
Returns
Object
pipe([6, 10, 11], median()); // => 10
pipe([], median()); // => undefined

multiply

Multiplies two numbers.

Data First

multiply(value, multiplicand);
Parameters
value
The number.
multiplicand
The number to multiply the value by.
Returns
bigint
multiply(3, 4); // => 12
reduce([1, 2, 3, 4], multiply, 1); // => 24

Data Last

multiply(multiplicand)(value);
Parameters
multiplicand
The number to multiply the value by.
Returns
Object
multiply(4)(3); // => 12
map([1, 2, 3, 4], multiply(2)); // => [2, 4, 6, 8]

product

Compute the product of the numbers in the array, or return 1 for an empty array.

Works for both number and bigint arrays, but not arrays that contain both types.

IMPORTANT: The result for empty arrays would be 1 (number) regardless of the type of the array; to avoid adding this to the return type for cases where the array is known to be non-empty you can use hasAtLeast or isEmpty to guard against this case.

Data First

product(data);
Parameters
data
The array of numbers.
Returns
Object
product([1, 2, 3]); // => 6
product([1n, 2n, 3n]); // => 6n
product([]); // => 1

Data Last

product()(data);
Parameters
Returns
Object
pipe([1, 2, 3], product()); // => 6
pipe([1n, 2n, 3n], product()); // => 6n
pipe([], product()); // => 1

randomBigInt

Generate a random bigint between from and to (inclusive).

! Important: In most environments this function uses crypto.getRandomValues() under-the-hood which is cryptographically strong. When the WebCrypto API isn't available (Node 18) we fallback to an implementation that uses Math.random() which is NOT cryptographically secure.

Data First

randomBigInt(from, to);
Parameters
from
The minimum value.
to
The maximum value.
Returns
bigint
randomBigInt(1n, 10n); // => 5n

randomInteger

Generate a random integer between from and to (inclusive).

!Important: This function uses Math.random() under-the-hood, which has two major limitations:

  1. It generates 2^52 possible values, so the bigger the range, the less uniform the distribution of values would be, and at ranges larger than that some values would never come up.
  2. It is not cryptographically secure and should not be used for security scenarios.

Data First

randomInteger(from, to);
Parameters
from
The minimum value.
to
The maximum value.
Returns
Object
randomInteger(1, 10); // => 5
randomInteger(1.5, 2.6); // => 2

round

Rounds a given number to a specific precision. If you'd like to round to an integer (i.e. use this function with constant precision === 0), use Math.round instead, as it won't incur the additional library overhead.

Data First

round(value, precision);
Parameters
value
The number to round.
precision
The precision to round to. Must be an integer between -15 and 15.
Returns
number
round(123.9876, 3); // => 123.988
round(483.22243, 1); // => 483.2
round(8541, -1); // => 8540
round(456789, -3); // => 457000

Data Last

round(precision)(value);
Parameters
precision
The precision to round to. Must be an integer between -15 and 15.
Returns
Object
round(3)(123.9876); // => 123.988
round(1)(483.22243); // => 483.2
round(-1)(8541); // => 8540
round(-3)(456789); // => 457000

subtract

Subtracts two numbers.

Data First

subtract(value, subtrahend);
Parameters
value
The number.
subtrahend
The number to subtract from the value.
Returns
bigint
subtract(10, 5); // => 5
subtract(10, -5); // => 15
reduce([1, 2, 3, 4], subtract, 20); // => 10

Data Last

subtract(subtrahend)(value);
Parameters
subtrahend
The number to subtract from the value.
Returns
Object
subtract(5)(10); // => 5
subtract(-5)(10); // => 15
map([1, 2, 3, 4], subtract(1)); // => [0, 1, 2, 3]

sum

Sums the numbers in the array, or return 0 for an empty array.

Works for both number and bigint arrays, but not arrays that contain both types.

IMPORTANT: The result for empty arrays would be 0 (number) regardless of the type of the array; to avoid adding this to the return type for cases where the array is known to be non-empty you can use hasAtLeast or isEmpty to guard against this case.

Data First

sum(data);
Parameters
data
The array of numbers.
Returns
Object
sum([1, 2, 3]); // => 6
sum([1n, 2n, 3n]); // => 6n
sum([]); // => 0

Data Last

sum()(data);
Parameters
Returns
Object
pipe([1, 2, 3], sum()); // => 6
pipe([1n, 2n, 3n], sum()); // => 6n
pipe([], sum()); // => 0

object

addProp

Add a new property to an object.

The function doesn't do any checks on the input object. If the property already exists it will be overwritten, and the type of the new value is not checked against the previous type.

Use set to override values explicitly with better protections.

Data First

addProp(obj, prop, value);
Parameters
obj
The target object.
prop
The property name.
value
The property value.
Returns
Object
addProp({ firstName: "john" }, "lastName", "doe"); // => {firstName: 'john', lastName: 'doe'}

Data Last

addProp(prop, value)(obj);
Parameters
prop
The property name.
value
The property value.
Returns
Object
addProp("lastName", "doe")({ firstName: "john" }); // => {firstName: 'john', lastName: 'doe'}

clone

Creates a deep copy of the value. Supported types: plain objects, Array, number, string, boolean, Date, and RegExp. Functions are assigned by reference rather than copied. Class instances or any other built-in type that isn't mentioned above are not supported (but might work).

Data First

clone(data);
Parameters
data
The object to clone.
Returns
Object
clone({ foo: "bar" }); // {foo: 'bar'}

Data Last

clone()(data);
Parameters
Returns
Object
pipe({ foo: "bar" }, clone()); // {foo: 'bar'}

entries

Returns an array of key/values of the enumerable properties of an object.

Data First

entries(object);
Parameters
data
Object to return keys and values of.
Returns
Array
entries({ a: 1, b: 2, c: 3 }); // => [['a', 1], ['b', 2], ['c', 3]]

Data Last

entries()(object);
Parameters
Returns
Object
pipe({ a: 1, b: 2, c: 3 }, entries()); // => [['a', 1], ['b', 2], ['c', 3]]

evolve

Creates a new object by applying functions that is included in evolver object parameter to the data object parameter according to their corresponding path.

Functions included in evolver object will not be invoked if its corresponding key does not exist in the data object. Also, values included in data object will be kept as is if its corresponding key does not exist in the evolver object.

Data First

evolve(data, evolver);
Parameters
object
Object whose value is applied to the corresponding function that is defined in `evolver` at the same path.
evolver
Object that include functions that is applied to the corresponding value of `data` object at the same path.
Returns
Object
const evolver = {
  count: add(1),
  time: { elapsed: add(1), remaining: add(-1) },
};
const data = {
  id: 10,
  count: 10,
  time: { elapsed: 100, remaining: 1400 },
};
evolve(data, evolver);
// => {
//   id: 10,
//   count: 11,
//   time: { elapsed: 101, remaining: 1399 },
// }

Data Last

evolve(evolver)(data);
Parameters
evolver
Object that include functions that is applied to the corresponding value of `data` object at the same path.
Returns
Object
const evolver = {
  count: add(1),
  time: { elapsed: add(1), remaining: add(-1) },
};
const data = {
  id: 10,
  count: 10,
  time: { elapsed: 100, remaining: 1400 },
};
pipe(data, evolve(evolver));
// => {
//   id: 10,
//   count: 11,
//   time: { elapsed: 101, remaining: 1399 },
// }

forEachObj

Iterate an object using a defined callback function.

The dataLast version returns the original object (instead of not returning anything (void)) to allow using it in a pipe. The returned object is the same reference as the input object, and not a shallow copy of it!

Data First

forEachObj(object, fn);
Parameters
data
The object who'se entries would be iterated on.
callbackfn
A function to execute for each element in the array.
Returns
void
forEachObj({ a: 1 }, (val, key, obj) => {
  console.log(`${key}: ${val}`);
}); // "a: 1"

Data Last

forEachObj(fn)(object);
Parameters
callbackfn
A function to execute for each element in the array.
Returns
Object
pipe(
  { a: 1 },
  forEachObj((val, key) => console.log(`${key}: ${val}`)),
); // "a: 1"

fromEntries

Creates a new object from an array of tuples by pairing up first and second elements as {[key]: value}. If a tuple is not supplied for any element in the array, the element will be ignored If duplicate keys exist, the tuple with the greatest index in the input array will be preferred.

The strict option supports more sophisticated use-cases like those that would result when calling the strict toPairs function.

There are several other functions that could be used to build an object from an array:

  • fromKeys - Builds an object from an array of keys and a mapper for values.
  • indexBy - Builds an object from an array of values and a mapper for keys.
  • pullObject - Builds an object from an array of items with mappers for both keys and values. Refer to the docs for more details.

Data First

fromEntries(tuples);
Parameters
entries
An array of key-value pairs.
Returns
Object
fromEntries([
  ["a", "b"],
  ["c", "d"],
]); // => {a: 'b', c: 'd'}

Data Last

fromEntries()(tuples);
Parameters
Returns
Object
pipe(
  [
    ["a", "b"],
    ["c", "d"],
  ] as const,
  fromEntries(),
); // => {a: 'b', c: 'd'}

fromKeys

Creates an object that maps each key in data to the result of mapper for that key. Duplicate keys are overwritten, guaranteeing that mapper is run for each item in data.

There are several other functions that could be used to build an object from an array:

  • indexBy - Builds an object from an array of values and a mapper for keys.
  • pullObject - Builds an object from an array of items with mappers for both keys and values.
  • fromEntries - Builds an object from an array of key-value pairs. Refer to the docs for more details.

Data First

fromKeys(data, mapper);
Parameters
data
An array of keys of the output object. All items in the array would be keys in the output array.
mapper
Takes a key and returns the value that would be associated with that key.
Returns
Object
fromKeys(["cat", "dog"], length()); // { cat: 3, dog: 3 } (typed as Partial<Record<"cat" | "dog", number>>)
fromKeys([1, 2], add(1)); // { 1: 2, 2: 3 } (typed as Partial<Record<1 | 2, number>>)

Data Last

fromKeys(mapper)(data);
Parameters
mapper
Takes a key and returns the value that would be associated with that key.
Returns
Object
pipe(["cat", "dog"], fromKeys(length())); // { cat: 3, dog: 3 } (typed as Partial<Record<"cat" | "dog", number>>)
pipe([1, 2], fromKeys(add(1))); // { 1: 2, 2: 3 } (typed as Partial<Record<1 | 2, number>>)

invert

Returns an object whose keys and values are swapped. If the object contains duplicate values, subsequent values will overwrite previous values.

Data First

invert(object);
Parameters
object
The object.
Returns
Object
invert({ a: "d", b: "e", c: "f" }); // => { d: "a", e: "b", f: "c" }

Data Last

invert()(object);
Parameters
Returns
Object
pipe({ a: "d", b: "e", c: "f" }, invert()); // => { d: "a", e: "b", f: "c" }

keys

Returns a new array containing the keys of the array or object.

Data First

keys(source);
Parameters
data
Either an array or an object.
Returns
Object
keys(["x", "y", "z"]); // => ['0', '1', '2']
keys({ a: "x", b: "y", 5: "z" }); // => ['a', 'b', '5']

Data Last

keys()(source);
Parameters
Returns
Object
pipe(["x", "y", "z"], keys()); // => ['0', '1', '2']
pipe({ a: "x", b: "y", 5: "z" } as const, keys()); // => ['a', 'b', '5']

mapKeys

Maps keys of object and keeps the same values.

Data First

mapKeys(object, fn);
Parameters
data
The object to map.
keyMapper
The mapping function.
Returns
Object
mapKeys({ a: 1, b: 2 }, (key, value) => key + value); // => { a1: 1, b2: 2 }

Data Last

mapKeys(fn)(object);
Parameters
keyMapper
The mapping function.
Returns
Object
pipe(
  { a: 1, b: 2 },
  mapKeys((key, value) => key + value),
); // => { a1: 1, b2: 2 }

mapValues

Maps values of object and keeps the same keys. Symbol keys are not passed to the mapper and will be removed from the output object.

To also copy the symbol keys to the output use merge: merge(data, mapValues(data, mapper))).

Data First

mapValues(data, mapper);
Parameters
data
The object to map.
valueMapper
The mapping function.
Returns
Object
mapValues({ a: 1, b: 2 }, (value, key) => value + key); // => {a: '1a', b: '2b'}

Data Last

mapValues(mapper)(data);
Parameters
valueMapper
The mapping function.
Returns
Object
pipe(
  { a: 1, b: 2 },
  mapValues((value, key) => value + key),
); // => {a: '1a', b: '2b'}

merge

Merges two objects into one by combining their properties, effectively creating a new object that incorporates elements from both. The merge operation prioritizes the second object's properties, allowing them to overwrite those from the first object with the same names.

Equivalent to { ...data, ...source }.

Data First

merge(data, source);
Parameters
data
The destination object, serving as the basis for the merge. Properties from this object are included in the new object, but will be overwritten by properties from the source object with matching keys.
source
The source object, whose properties will be included in the new object. If properties in this object share keys with properties in the destination object, the values from the source object will be used in the new object.
Returns
Object
merge({ x: 1, y: 2 }, { y: 10, z: 2 }); // => { x: 1, y: 10, z: 2 }

Data Last

merge(source)(data);
Parameters
source
The source object, whose properties will be included in the new object. If properties in this object share keys with properties in the destination object, the values from the source object will be used in the new object.
Returns
Object
pipe({ x: 1, y: 2 }, merge({ y: 10, z: 2 })); // => { x: 1, y: 10, z: 2 }

mergeDeep

Merges the source object into the destination object. The merge is similar to performing { ...destination, ... source } (where disjoint values from each object would be copied as-is, and for any overlapping props the value from source would be used); But for each prop (p), if both destination and source have a plain-object as a value, the value would be taken as the result of recursively deepMerging them (result.p === deepMerge(destination.p, source.p)).

Data First

mergeDeep(destination, source);
Parameters
destination
The object to merge into. In general, this object would have it's values overridden.
source
The object to merge from. In general, shared keys would be taken from this object.
Returns
Object
mergeDeep({ foo: "bar", x: 1 }, { foo: "baz", y: 2 }); // => { foo: 'baz', x: 1, y: 2 }

Data Last

mergeDeep(source)(destination);
Parameters
source
The object to merge from. In general, shared keys would be taken from this object.
Returns
Object
pipe({ foo: "bar", x: 1 }, mergeDeep({ foo: "baz", y: 2 })); // => { foo: 'baz', x: 1, y: 2 }

objOf

Creates an object containing a single key:value pair.

objOf(value, key);
Parameters
value
The object value.
key
The property name.
Returns
Object
objOf(10, "a"); // => { a: 10 }
objOf(key)(value);
Parameters
key
The property name.
Returns
Object
pipe(10, objOf("a")); // => { a: 10 }

omit

Returns a partial copy of an object omitting the keys specified.

Data First

omit(obj, keys);
Parameters
data
The object.
keys
The property names.
Returns
Object
omit({ a: 1, b: 2, c: 3, d: 4 }, ["a", "d"]); // => { b: 2, c: 3 }

Data Last

omit(keys)(obj);
Parameters
keys
The property names.
Returns
Object
pipe({ a: 1, b: 2, c: 3, d: 4 }, omit(["a", "d"])); // => { b: 2, c: 3 }

omitBy

Creates a shallow copy of the data, and then removes any keys that the predicate rejects. Symbol keys are not passed to the predicate and would be passed through to the output as-is.

See pickBy for a complementary function which starts with an empty object and adds the entries that the predicate accepts. Because it is additive, symbol keys will not be passed through to the output object.

Data First

omitBy(data, predicate);
Parameters
data
The target object.
predicate
A function that takes the value, key, and the data itself and returns `true` if the entry shouldn't be part of the output object, or `false` to keep it. If the function is a type-guard on the value the output type would be narrowed accordingly.
Returns
Object
omitBy({ a: 1, b: 2, A: 3, B: 4 }, (val, key) => key.toUpperCase() === key); // => {a: 1, b: 2}

Data Last

omitBy(fn)(object);
Parameters
predicate
The predicate.
Returns
Object
omitBy((val, key) => key.toUpperCase() === key)({ a: 1, b: 2, A: 3, B: 4 }); // => {a: 1, b: 2}

pathOr

Gets the value at path of object. If the resolved value is null or undefined, the defaultValue is returned in its place.

DEPRECATED: Use defaultTo(prop(object, ...path), defaultValue) instead!

Data First

pathOr(object, array, defaultValue);
Parameters
object
The target object.
path
The path of the property to get.
defaultValue
The default value.
Returns
Object
pathOr({ x: 10 }, ["y"], 2); // 2
pathOr({ y: 10 }, ["y"], 2); // 10
Parameters
object
path
defaultValue
Returns
Object

Data Last

pathOr(array, defaultValue)(object);
Parameters
path
The path of the property to get.
defaultValue
The default value.
Returns
Object
pipe({ x: 10 }, pathOr(["y"], 2)); // 2
pipe({ y: 10 }, pathOr(["y"], 2)); // 10

pick

Creates an object composed of the picked data properties.

Data First

pick(object, [prop1, prop2]);
Parameters
data
The target object.
keys
The property names.
Returns
Object
pick({ a: 1, b: 2, c: 3, d: 4 }, ["a", "d"]); // => { a: 1, d: 4 }

Data Last

pick([prop1, prop2])(object);
Parameters
keys
The property names.
Returns
Object
pipe({ a: 1, b: 2, c: 3, d: 4 }, pick(["a", "d"])); // => { a: 1, d: 4 }

pickBy

Iterates over the entries of data and reconstructs the object using only entries that predicate accepts. Symbol keys are not passed to the predicate and would be filtered out from the output object.

See omitBy for a complementary function which starts with a shallow copy of the input object and removes the entries that the predicate rejects. Because it is subtractive symbol keys would be copied over to the output object. See also entries, filter, and fromEntries which could be used to build your own version of pickBy if you need more control (though the resulting type might be less precise).

Data First

pickBy(data, predicate);
Parameters
data
The target object.
predicate
A function that takes the value, key, and the data itself and returns true if the entry should be part of the output object, or `false` to remove it. If the function is a type-guard on the value the output type would be narrowed accordingly.
Returns
Object
pickBy({ a: 1, b: 2, A: 3, B: 4 }, (val, key) => key.toUpperCase() === key); // => {A: 3, B: 4}

Data Last

pickBy(predicate)(data);
Parameters
predicate
A function that takes the value, key, and the data itself and returns true if the entry should be part of the output object, or `false` to remove it. If the function is a type-guard on the value the output type would be narrowed accordingly.
Returns
Object
pipe(
  { a: 1, b: 2, A: 3, B: 4 },
  pickBy((val, key) => key.toUpperCase() === key),
); // => {A: 3, B: 4}

prop

Gets the value of the given property from an object. Nested properties can be accessed by providing a variadic array of keys that define the path from the root to the desired property. Arrays can be accessed by using numeric keys. Unions and optional properties are handled gracefully by returning undefined early for any non-existing property on the path. Paths are validated against the object type to provide stronger type safety, better compile-time errors, and to enable autocompletion in IDEs.

To check whether a key exists on the object, use hasProp.

Data First

prop(data, ...keys);
Parameters
data
The object or array to access.
key
The key(s) for the property to extract.
Returns
Object
prop({ foo: { bar: "baz" } }, "foo"); //=> { bar: 'baz' }
prop({ foo: { bar: "baz" } }, "foo", "bar"); //=> 'baz'
prop(["cat", "dog"], 1); //=> 'dog'

Data Last

prop(...keys)(data);
Parameters
key
The key(s) for the property to extract.
Returns
Object
pipe({ foo: { bar: "baz" } }, prop("foo")); //=> { bar: 'baz' }
pipe({ foo: { bar: "baz" } }, prop("foo", "bar")); //=> 'baz'
pipe(["cat", "dog"], prop(1)); //=> 'dog'

pullObject

Creates an object that maps the result of valueExtractor with a key resulting from running keyExtractor on each item in data. Duplicate keys are overwritten, guaranteeing that the extractor functions are run on each item in data.

There are several other functions that could be used to build an object from an array:

  • fromKeys - Builds an object from an array of keys and a mapper for values.
  • indexBy - Builds an object from an array of values and a mapper for keys.
  • fromEntries - Builds an object from an array of key-value pairs. Refer to the docs for more details.

Data First

pullObject(data, keyExtractor, valueExtractor);
Parameters
data
The items used to pull/extract the keys and values from.
keyExtractor
Computes the key for item.
valueExtractor
Computes the value for the item.
Returns
Object
pullObject(
  [
    { name: "john", email: "john@remedajs.com" },
    { name: "jane", email: "jane@remedajs.com" },
  ],
  prop("name"),
  prop("email"),
); // => { john: "john@remedajs.com", jane: "jane@remedajs.com" }

Data Last

pullObject(keyExtractor, valueExtractor)(data);
Parameters
keyExtractor
Computes the key for item.
valueExtractor
Computes the value for the item.
Returns
Object
pipe(
  [
    { name: "john", email: "john@remedajs.com" },
    { name: "jane", email: "jane@remedajs.com" },
  ],
  pullObject(prop("name"), prop("email")),
); // => { john: "john@remedajs.com", jane: "jane@remedajs.com" }

set

Sets the value at prop of object.

To add a new property to an object, or to override its type, use addProp instead, and to set a property within a nested object use setPath.

Data First

set(obj, prop, value);
Parameters
obj
The target method.
prop
The property name.
value
The value to set.
Returns
Object
set({ a: 1 }, "a", 2); // => { a: 2 }

Data Last

set(prop, value)(obj);
Parameters
prop
The property name.
value
The value to set.
Returns
Object
pipe({ a: 1 }, set("a", 2)); // => { a: 2 }

setPath

Sets the value at path of object.

For simple cases where the path is only one level deep, prefer set instead.

Data First

setPath(obj, path, value);
Parameters
data
The target method.
path
The array of properties.
value
The value to set.
Returns
Object
setPath({ a: { b: 1 } }, ["a", "b"], 2); // => { a: { b: 2 } }

Data Last

setPath(path, value)(obj);
Parameters
path
The array of properties.
value
The value to set.
Returns
Object
pipe({ a: { b: 1 } }, setPath(["a", "b"], 2)); // { a: { b: 2 } }

swapProps

Swaps the values of two properties in an object based on the provided keys.

Data First

swapProps(data, key1, key2);
Parameters
data
The object to be manipulated.
key1
The first property key.
key2
The second property key.
Returns
Object
swapProps({ a: 1, b: 2, c: 3 }, "a", "b"); // => {a: 2, b: 1, c: 3}

Data Last

swapProps(key1, key2)(data);
Parameters
key1
The first property key.
key2
The second property key.
Returns
Object
swapProps("a", "b")({ a: 1, b: 2, c: 3 }); // => {a: 2, b: 1, c: 3}

values

Returns a new array containing the values of the array or object.

Data First

values(source);
Parameters
data
Either an array or an object.
Returns
Object
values(["x", "y", "z"]); // => ['x', 'y', 'z']
values({ a: "x", b: "y", c: "z" }); // => ['x', 'y', 'z']

Data Last

values()(source);
Parameters
Returns
Object
pipe(["x", "y", "z"], values()); // => ['x', 'y', 'z']
pipe({ a: "x", b: "y", c: "z" }, values()); // => ['x', 'y', 'z']
pipe({ a: "x", b: "y", c: "z" }, values(), first()); // => 'x'

other

defaultTo

A stricter wrapper around the Nullish coalescing operator ?? that ensures that the fallback matches the type of the data. Only works when data can be null or undefined.

Notice that Number.NaN is not nullish and would not result in returning the fallback!

Data First

defaultTo(data, fallback);
Parameters
data
A nullish value.
fallback
A value of the same type as `data` that would be returned when `data` is nullish.
Returns
Object
defaultTo("hello" as string | undefined, "world"); //=> "hello"
defaultTo(undefined as string | undefined, "world"); //=> "world"

Data Last

defaultTo(fallback)(data);
Parameters
fallback
A value of the same type as `data` that would be returned when `data` is nullish.
Returns
Object
pipe("hello" as string | undefined, defaultTo("world")); //=> "hello"
pipe(undefined as string | undefined, defaultTo("world")); //=> "world"

tap

Calls the given function with the given value, then returns the given value. The return value of the provided function is ignored.

This allows "tapping into" a function sequence in a pipe, to perform side effects on intermediate results.

Data First

tap(value, fn);
Parameters
value
The value to pass into the function.
fn
The function to call.
Returns
Object
tap("foo", console.log); // => "foo"

Data Last

tap(fn)(value);
Parameters
fn
The function to call.
Returns
Object
pipe(
  [-5, -1, 2, 3],
  filter((n) => n > 0),
  tap(console.log), // prints [2, 3]
  map((n) => n * 2),
); // => [4, 6]

string

capitalize

Makes the first character of a string uppercase while leaving the rest unchanged.

It uses the built-in String.prototype.toUpperCase for the runtime and the built-in Capitalize utility type for typing and thus shares their locale inaccuracies.

For display purposes, prefer using the CSS pseudo-element ::first-letter to target just the first letter of the word, and text-transform: uppercase to capitalize it. This transformation is locale-aware.

For other case manipulations see: toUpperCase, toLowerCase, uncapitalize, toCamelCase, toKebabCase, toSnakeCase, and toTitleCase.

Data First

capitalize(data);
Parameters
data
A string.
Returns
Object
capitalize("hello world"); // "Hello world"

Data Last

capitalize()(data);
Parameters
Returns
Object
pipe("hello world", capitalize()); // "Hello world"

endsWith

Determines whether a string ends with the provided suffix, and refines the output type if possible.

This function is a wrapper around the built-in String.prototype.endsWith method, but doesn't expose the endPosition parameter. To check only up to a specific position, use endsWith(sliceString(data, 0, endPosition), suffix).

Data First

endsWith(data, suffix);
Parameters
data
The input string.
suffix
The string to check for at the end.
Returns
boolean
endsWith("hello world", "hello"); // false
endsWith("hello world", "world"); // true

Data Last

endsWith(suffix)(data);
Parameters
suffix
The string to check for at the end.
Returns
Object
pipe("hello world", endsWith("hello")); // false
pipe("hello world", endsWith("world")); // true

randomString

Data First

randomString(length);
Parameters
length
The length of the random string.
Returns
string
randomString(5); // => aB92J

Data Last

randomString()(length);
Parameters
Returns
Object
pipe(5, randomString()); // => aB92J

sliceString

Extracts a section of a string between two indices.

This function is a wrapper around the built-in String.prototype.slice method.

Data First

sliceString(data, indexStart, indexEnd);
Parameters
data
The string to extract from.
indexStart
The index of the first character to include in the returned substring.
indexEnd
The index of the first character to exclude from the returned substring.
Returns
string
sliceString("abcdefghijkl", 1); // => `bcdefghijkl`
sliceString("abcdefghijkl", 4, 7); // => `efg`

Data Last

sliceString(indexStart, indexEnd)(string);
Parameters
indexStart
The index of the first character to include in the returned substring.
indexEnd
The index of the first character to exclude from the returned substring, or `undefined` for the rest of the string.
Returns
Object
sliceString(1)("abcdefghijkl"); // => `bcdefghijkl`
sliceString(4, 7)("abcdefghijkl"); // => `efg`

split

Splits a string into an array of substrings using a separator pattern.

This function is a wrapper around the built-in String.prototype.split method.

Data First

split(data, separator, limit);
Parameters
data
The string to split.
separator
The pattern describing where each split should occur. Can be a string, or a regular expression.
limit
A non-negative integer specifying a limit on the number of substrings to be included in the array. If provided, splits the string at each occurrence of the specified separator, but stops when limit entries have been placed in the array. Any leftover text is not included in the array at all. The array may contain fewer entries than limit if the end of the string is reached before the limit is reached. If limit is 0, [] is returned.
Returns
Array
split("a,b,c", ","); //=> ["a", "b", "c"]
split("a,b,c", ",", 2); //=> ["a", "b"]
split("a1b2c3d", /\d/u); //=> ["a", "b", "c", "d"]

Data Last

split(separator, limit)(data);
Parameters
separator
The pattern describing where each split should occur. Can be a string, or a regular expression.
limit
A non-negative integer specifying a limit on the number of substrings to be included in the array. If provided, splits the string at each occurrence of the specified separator, but stops when limit entries have been placed in the array. Any leftover text is not included in the array at all. The array may contain fewer entries than limit if the end of the string is reached before the limit is reached. If limit is 0, [] is returned.
Returns
Object
pipe("a,b,c", split(",")); //=> ["a", "b", "c"]
pipe("a,b,c", split(",", 2)); //=> ["a", "b"]
pipe("a1b2c3d", split(/\d/u)); //=> ["a", "b", "c", "d"]

startsWith

Determines whether a string begins with the provided prefix, and refines the output type if possible.

This function is a wrapper around the built-in String.prototype.startsWith method, but doesn't expose the startPosition parameter. To check from a specific position, use startsWith(sliceString(data, startPosition), prefix).

Data First

startsWith(data, prefix);
Parameters
data
The input string.
prefix
The string to check for at the beginning.
Returns
boolean
startsWith("hello world", "hello"); // true
startsWith("hello world", "world"); // false

Data Last

startsWith(prefix)(data);
Parameters
prefix
The string to check for at the beginning.
Returns
Object
pipe("hello world", startsWith("hello")); // true
pipe("hello world", startsWith("world")); // false

toCamelCase

Converts text to camelCase by splitting it into words, lowercasing the first word, capitalizing the rest, then joining them back together.

Because it uses the built-in case conversion methods, the function shares their locale inaccuracies too, making it best suited for simple strings like identifiers and internal keys. For linguistic text processing, use Intl.Segmenter with granularity: "word", toLocaleLowerCase, and toLocaleUpperCase which are purpose-built to handle nuances in languages and locales.

For other case manipulations see: toLowerCase, toUpperCase, capitalize, uncapitalize, toKebabCase, toSnakeCase, and toTitleCase.

For PascalCase use capitalize(toCamelCase(data)).

Data First

toCamelCase(data);
Parameters
data
A string.
options
An _optional_ object with the _optional_ property `preserveConsecutiveUppercase` that can be used to change the way consecutive uppercase characters are handled. Defaults to `true`.
Returns
Object
toCamelCase("hello world"); // "helloWorld"
toCamelCase("__HELLO_WORLD__"); // "helloWorld"
toCamelCase("HasHTML"); // "hasHTML"
toCamelCase("HasHTML", { preserveConsecutiveUppercase: false }); // "hasHtml"

Data Last

toCamelCase()(data);
Parameters
options
An _optional_ object with the _optional_ property `preserveConsecutiveUppercase` that can be used to change the way consecutive uppercase characters are handled. Defaults to `true`.
Returns
Object
pipe("hello world", toCamelCase()); // "helloWorld"
pipe("__HELLO_WORLD__", toCamelCase()); // "helloWorld"
pipe("HasHTML", toCamelCase()); // "hasHTML"
pipe("HasHTML", toCamelCase({ preserveConsecutiveUppercase: false })); // "hasHtml"

toKebabCase

Converts text to kebab-case by splitting it into words and joining them back together with hyphens (-), then lowercasing the result.

Because it uses toLowerCase, the function shares its locale inaccuracies too, making it best suited for simple strings like identifiers and internal keys. For linguistic text processing, use Intl.Segmenter with granularity: "word", and toLocaleLowerCase, which are purpose-built to handle nuances in languages and locales.

For other case manipulations see: toLowerCase, toUpperCase, capitalize, uncapitalize, toCamelCase, toSnakeCase, and toTitleCase.

For COBOL-CASE use toUpperCase(toKebabCase(data)).

Data First

toKebabCase(data);
Parameters
data
A string.
Returns
Object
toKebabCase("hello world"); // "hello-world"
toKebabCase("__HELLO_WORLD__"); // "hello-world"

Data Last

toKebabCase()(data);
Parameters
Returns
Object
pipe("hello world", toKebabCase()); // "hello-world"
pipe("__HELLO_WORLD__", toKebabCase()); // "hello-world"

toLowerCase

Replaces all uppercase characters with their lowercase equivalents.

This function is a wrapper around the built-in String.prototype.toLowerCase method and shares its locale inaccuracies.

For a more linguistically accurate transformation use toLocaleLowerCase, and for display purposes use CSS text-transform: lowercase; which is locale-aware.

For other case manipulations see: toUpperCase, capitalize, uncapitalize, toCamelCase, toKebabCase, toSnakeCase, and toTitleCase.

Data First

toLowerCase(data);
Parameters
data
A string.
Returns
Object
toLowerCase("Hello World"); // "hello world"

Data Last

toLowerCase()(data);
Parameters
Returns
Object
pipe("Hello World", toLowerCase()); // "hello world"

toSnakeCase

Converts text to snake_case by splitting it into words and joining them back together with underscores (_), then lowercasing the result.

Because it uses toLowerCase, the function shares its locale inaccuracies too, making it best suited for simple strings like identifiers and internal keys. For linguistic text processing, use Intl.Segmenter with granularity: "word", and toLocaleLowerCase, which are purpose-built to handle nuances in languages and locales.

For other case manipulations see: toLowerCase, toUpperCase, capitalize, uncapitalize, toCamelCase, toKebabCase, and toTitleCase.

For CONSTANT_CASE use toUpperCase(toSnakeCase(data)).

Data First

toSnakeCase(data);
Parameters
data
A string.
Returns
Object
toSnakeCase("hello world"); // "hello_world"
toSnakeCase("__HELLO_WORLD__"); // "hello_world"

Data Last

toSnakeCase()(data);
Parameters
Returns
Object
pipe("hello world", toSnakeCase()); // "hello_world"
pipe("__HELLO_WORLD__", toSnakeCase()); // "hello_world"

toTitleCase

Converts text to Title Case by splitting it into words, capitalizing the first letter of each word, then joining them back together with spaces.

Because it uses the built-in case conversion methods, the function shares their locale inaccuracies, making it best suited for simple strings like identifiers and internal keys. For linguistic text processing, use Intl.Segmenter with granularity: "word", toLocaleLowerCase, and toLocaleUpperCase which are purpose-built to handle nuances in languages and locales.

For other case manipulations see: toLowerCase, toUpperCase, capitalize, uncapitalize, toCamelCase, toKebabCase, and toSnakeCase.

Data First

toTitleCase(data);
Parameters
data
A string.
options
An _optional_ object with the _optional_ property `preserveConsecutiveUppercase` that can be used to change the way consecutive uppercase characters are handled. Defaults to `true`.
Returns
Object
toTitleCase("hello world"); // "Hello World"
toTitleCase("--foo-bar--"); // "Foo Bar"
toTitleCase("fooBar"); // "Foo Bar"
toTitleCase("__FOO_BAR__"); // "Foo Bar"
toTitleCase("XMLHttpRequest"); // "XML Http Request"
toTitleCase("XMLHttpRequest", { preserveConsecutiveUppercase: false }); // "Xml Http Request"

Data Last

toTitleCase()(data);
Parameters
options
An _optional_ object with the _optional_ property `preserveConsecutiveUppercase` that can be used to change the way consecutive uppercase characters are handled. Defaults to `true`.
Returns
Object
pipe("hello world", toTitleCase()); // "Hello World"
pipe("--foo-bar--", toTitleCase()); // "Foo Bar"
pipe("fooBar", toTitleCase()); // "Foo Bar"
pipe("__FOO_BAR__", toTitleCase()); // "Foo Bar"
pipe("XMLHttpRequest", toTitleCase()); // "XML Http Request"
pipe("XMLHttpRequest", toTitleCase({ preserveConsecutiveUppercase: false })); // "Xml Http Request"

toUpperCase

Replaces all lowercase characters with their uppercase equivalents.

This function is a wrapper around the built-in String.prototype.toUpperCase method and shares its locale inaccuracies.

For a more linguistically accurate transformation use toLocaleUpperCase, and for display purposes use CSS text-transform: uppercase; which is locale-aware.

For other case manipulations see: toLowerCase, capitalize, uncapitalize, toCamelCase, toKebabCase, toSnakeCase, and toTitleCase.

Data First

toUpperCase(data);
Parameters
data
A string.
Returns
Object
toUpperCase("Hello World"); // "HELLO WORLD"

Data Last

toUpperCase()(data);
Parameters
Returns
Object
pipe("Hello World", toUpperCase()); // "HELLO WORLD"

truncate

Truncates strings to a maximum length, adding an ellipsis when truncated.

Shorter strings are returned unchanged. If the omission marker is longer than the maximum length, it will be truncated as well.

The separator argument provides more control by optimistically searching for a matching cutoff point, which could be used to avoid truncating in the middle of a word or other semantic boundary.

If you just need to limit the total length of the string, without adding an omission or optimizing the cutoff point via separator, prefer sliceString instead, which runs more efficiently.

The function counts Unicode characters, not visual graphemes, and may split emojis, denormalized diacritics, or combining characters, in the middle. For display purposes, prefer CSS text-overflow: ellipsis which is locale-aware and purpose-built for this task.

Data First

truncate(data, n, { omission, separator });
Parameters
data
The input string.
n
The maximum length of the output string. The output will **never** exceed this length.
options
An optional options object.
Returns
Object
truncate("Hello, world!", 8); //=> "Hello..."
truncate("cat, dog, mouse", 12, { omission: "__", separator: "," }); //=> "cat, dog__"

Data Last

truncate(n, { omission, separator })(data);
Parameters
n
The maximum length of the output string. The output will **never** exceed this length.
options
An optional options object.
Returns
Object
pipe("Hello, world!" as const, truncate(8)); //=> "Hello..."
pipe(
  "cat, dog, mouse" as const,
  truncate(12, { omission: "__", separator: "," }),
); //=> "cat, dog__"

uncapitalize

Makes the first character of a string lowercase while leaving the rest unchanged.

It uses the built-in String.prototype.toLowerCase for the runtime and the built-in Uncapitalize utility type for typing and thus shares their locale inaccuracies.

For display purposes, prefer using the CSS pseudo-element ::first-letter to target just the first letter of the word, and text-transform: lowercase to lowercase it. This transformation is locale-aware.

For other case manipulations see: toUpperCase, toLowerCase, capitalize, toCamelCase, toKebabCase, toSnakeCase, and toTitleCase.

Data First

uncapitalize(data);
Parameters
data
A string.
Returns
Object
uncapitalize("HELLO WORLD"); // "hELLO WORLD"

Data Last

uncapitalize()(data);
Parameters
Returns
Object
pipe("HELLO WORLD", uncapitalize()); // "hELLO WORLD"

utility

stringToPath

A utility to allow JSONPath-like strings to be used in other utilities which take an array of path segments as input (e.g. prop, setPath, etc...). The main purpose of this utility is to act as a bridge between the runtime implementation that converts the path to an array, and the type-system that parses the path string type into an array type. This type allows us to return fine-grained types and to enforce correctness at the type-level.

We discourage using this utility for new code. This utility is for legacy code that already contains path strings (which are accepted by Lodash). We strongly recommend using path arrays instead as they provide better developer experience via significantly faster type-checking, fine-grained error messages, and automatic typeahead suggestions for each segment of the path.

There are a bunch of limitations to this utility derived from the limitations of the type itself, these are usually edge-cases around deeply nested paths, escaping, whitespaces, and empty segments. This is true even in cases where the runtime implementation can better handle them, this is intentional. See the tests for this utility for more details and the expected outputs.

Data First

stringToPath(path);
Parameters
path
A string path.
Returns
Object
stringToPath("a.b[0].c"); // => ['a', 'b', 0, 'c']