Skip to main content

latest
Works with
It is unknown whether this package works with Node.js, Deno, Browsers, Cloudflare Workers, Bun
It is unknown whether this package works with Node.js
It is unknown whether this package works with Deno
It is unknown whether this package works with Browsers
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Bun
JSR Score70%
Downloads1/wk
Published2 years ago (0.215.0)
default

Pure functions for common tasks around collection types like arrays and objects.

Functions

f
aggregateGroups<T, A>(
record: Readonly<Record<string, ReadonlyArray<T>>>,
aggregator: (
current: T,
key: string,
first: boolean,
accumulator?: A
) => A
): Record<string, A>

Applies the given aggregator to each group in the given grouping, returning the results together with the respective group keys

f
associateBy<T>(
array: Iterable<T>,
selector: (el: T) => string
): Record<string, T>

Transforms the given array into a Record, extracting the key of each element using the given selector. If the selector produces the same key for multiple elements, the latest one will be used (overriding the ones before it).

f
associateWith<T>(
array: Iterable<string>,
selector: (key: string) => T
): Record<string, T>

Builds a new Record using the given array as keys and choosing a value for each key using the given selector. If any of two pairs would have the same value the latest on will be used (overriding the ones before it).

f
chunk<T>(
array: readonly T[],
size: number
): T[][]

Splits the given array into chunks of the given size and returns them.

f
deepMerge<
T extends Record<PropertyKey, unknown>,
U extends Record<PropertyKey, unknown>,
Options extends DeepMergeOptions = { arrays: "merge"; sets: "merge"; maps: "merge"; }
>
(
record: Readonly<T>,
other: Readonly<U>,
options?: Readonly<Options>
): DeepMerge<T, U, Options>
2 overloads

Merges the two given Records, recursively merging any nested Records with the second collection overriding the first in case of conflict

f
distinct<T>(array: Iterable<T>): T[]

Returns all distinct elements in the given array, preserving order by first occurrence.

f
distinctBy<T, D>(
array: Iterable<T>,
selector: (el: T) => D
): T[]

Returns all elements in the given array that produce a distinct value using the given selector, preserving order by first occurrence.

f
dropLastWhile<T>(
array: readonly T[],
predicate: (el: T) => boolean
): T[]

Returns a new array that drops all elements in the given collection until the last element that does not match the given predicate.

f
dropWhile<T>(
array: readonly T[],
predicate: (el: T) => boolean
): T[]

Returns a new array that drops all elements in the given collection until the first element that does not match the given predicate.

f
filterEntries<T>(
record: Readonly<Record<string, T>>,
predicate: (entry: [string, T]) => boolean
): Record<string, T>

Returns a new record with all entries of the given record except the ones that do not match the given predicate.

f
filterKeys<T>(
record: Readonly<Record<string, T>>,
predicate: (key: string) => boolean
): Record<string, T>

Returns a new record with all entries of the given record except the ones that have a key that does not match the given predicate.

f
filterValues<T>(
record: Readonly<Record<string, T>>,
predicate: (value: T) => boolean
): Record<string, T>

Returns a new record with all entries of the given record except the ones that have a value that does not match the given predicate.

f
findSingle<T>(
array: Iterable<T>,
predicate: (el: T) => boolean
): T | undefined

Returns an element if and only if that element is the only one matching the given condition. Returns undefined otherwise.

f
firstNotNullishOf<T, O>(
array: Iterable<T>,
selector: (item: T) => O | undefined | null
): NonNullable<O> | undefined

Applies the given selector to elements in the given array until a value is produced that is neither null nor undefined and returns that value. Returns undefined if no such value is produced.

f
includesValue<T>(
record: Readonly<Record<string, T>>,
value: T
): boolean

If the given value is part of the given object it returns true, otherwise it returns false. Doesn't work with non-primitive values: includesValue({x: {}}, {}) returns false.

f
intersect<T>(...arrays: (readonly T[])[]): T[]

Returns all distinct elements that appear at least once in each of the given arrays.

f
joinToString<T>(
array: Iterable<T>,
selector: (el: T) => string,
unnamed 2?: Readonly<JoinToStringOptions>
): string

Transforms the elements in the given array to strings using the given selector. Joins the produced strings into one using the given separator and applying the given prefix and suffix to the whole string afterwards. If the array could be huge, you can specify a non-negative value of limit, in which case only the first limit elements will be appended, followed by the truncated string. Returns the resulting string.

f
mapEntries<T, O>(
record: Readonly<Record<string, T>>,
transformer: (entry: [string, T]) => [string, O]
): Record<string, O>

Applies the given transformer to all entries in the given record and returns a new record containing the results.

f
mapKeys<T>(
record: Readonly<Record<string, T>>,
transformer: (key: string) => string
): Record<string, T>

Applies the given transformer to all keys in the given record's entries and returns a new record containing the transformed entries.

f
mapNotNullish<T, O>(
array: Iterable<T>,
transformer: (el: T) => O
): NonNullable<O>[]

Returns a new array, containing all elements in the given array transformed using the given transformer, except the ones that were transformed to null or undefined.

f
mapValues<T, O, K extends string>(
record: Record<K, T>,
transformer: (
value: T,
key: K
) => O
): any
2 overloads

Applies the given transformer to all values in the given record and returns a new record containing the resulting keys associated to the last value that produced them.

f
maxBy<T>(
array: Iterable<T>,
selector:
((el: T) => number)
| ((el: T) => string)
| ((el: T) => bigint)
| ((el: T) => Date)
): T | undefined
4 overloads

Returns the first element that is the largest value of the given function or undefined if there are no elements.

f
maxOf<T, S extends ((el: T) => number) | ((el: T) => bigint)>(
array: Iterable<T>,
selector: S
): ReturnType<S> | undefined
2 overloads

Applies the given selector to all elements of the provided collection and returns the max value of all elements. If an empty array is provided the function will return undefined

f
maxWith<T>(
array: Iterable<T>,
comparator: (
a: T,
b: T
) => number
): T | undefined

Returns the first element having the largest value according to the provided comparator or undefined if there are no elements.

f
minBy<T>(
array: Iterable<T>,
selector:
((el: T) => number)
| ((el: T) => string)
| ((el: T) => bigint)
| ((el: T) => Date)
): T | undefined
4 overloads

Returns the first element that is the smallest value of the given function or undefined if there are no elements

f
minOf<T, S extends ((el: T) => number) | ((el: T) => bigint)>(
array: Iterable<T>,
selector: S
): ReturnType<S> | undefined
2 overloads

Applies the given selector to all elements of the given collection and returns the min value of all elements. If an empty array is provided the function will return undefined.

f
minWith<T>(
array: Iterable<T>,
comparator: (
a: T,
b: T
) => number
): T | undefined

Returns the first element having the smallest value according to the provided comparator or undefined if there are no elements

f
partition(
array: Iterable<unknown>,
predicate: (el: unknown) => boolean
): [unknown[], unknown[]]
2 overloads

Returns a tuple of two arrays with the first one containing all elements in the given array that match the given predicate and the second one containing all that do not.

f
partitionEntries<T>(
record: Readonly<Record<string, T>>,
predicate: (entry: [string, T]) => boolean
): [Record<string, T>, Record<string, T>]

Returns a tuple of two records with the first one containing all entries of the given record that match the given predicate and the second one containing all that do not.

f
permutations<T>(inputArray: Iterable<T>): T[][]

Builds all possible orders of all elements in the given array Ignores equality of elements, meaning this will always return the same number of permutations for a given length of input.

f
reduceGroups<T, A>(
record: Readonly<Record<string, ReadonlyArray<T>>>,
reducer: (
accumulator: A,
current: T
) => A
,
initialValue: A
): Record<string, A>

Applies the given reducer to each group in the given grouping, returning the results together with the respective group keys.

f
runningReduce<T, O>(
array: readonly T[],
reducer: (
accumulator: O,
current: T,
currentIndex: number
) => O
,
initialValue: O
): O[]

Calls the given reducer on each element of the given collection, passing its result as the accumulator to the next respective call, starting with the given initialValue. Returns all intermediate accumulator results.

f
sample<T>(array: readonly T[]): T | undefined

Returns a random element from the given array.

f
slidingWindows<T>(
array: readonly T[],
size: number,
unnamed 2?: { step?: number; partial?: boolean; }
): T[][]

Generates sliding views of the given array of the given size and returns a new array containing all of them.

f
sortBy<T>(
array: readonly T[],
selector:
((el: T) => number)
| ((el: T) => string)
| ((el: T) => bigint)
| ((el: T) => Date),
options?: SortByOptions
): T[]
4 overloads

Returns all elements in the given collection, sorted by their result using the given selector. The selector function is called only once for each element. Ascending or descending order can be specified.

f
sumOf<T>(
array: Iterable<T>,
selector: (el: T) => number
): number

Applies the given selector to all elements in the given collection and calculates the sum of the results.

f
takeLastWhile<T>(
array: readonly T[],
predicate: (el: T) => boolean
): T[]

Returns all elements in the given array after the last element that does not match the given predicate.

f
takeWhile<T>(
array: readonly T[],
predicate: (el: T) => boolean
): T[]

Returns all elements in the given collection until the first element that does not match the given predicate.

f
union<T>(...arrays: Iterable<T>[]): T[]

Returns all distinct elements that appear in any of the given arrays.

f
unzip<T, U>(pairs: readonly [T, U][]): [T[], U[]]

Builds two separate arrays from the given array of 2-tuples, with the first returned array holding all first tuple elements and the second one holding all the second elements.

f
withoutAll<T>(
array: readonly T[],
values: readonly T[]
): T[]

Returns an array excluding all given values.

f
zip<T extends unknown[]>(...arrays: [K in keyof T]: ReadonlyArray<T[K]>): T[]

Builds N-tuples of elements from the given N arrays with matching indices, stopping when the smallest array's end is reached.

Type Aliases

T
ArrayValueType<T> = T extends Array<infer V> ? V : never

Get array values type

T
DeepMerge<T, U, Options = Record<string, MergingStrategy>> = [T, U] extends [Record<PropertyKey, unknown>, Record<PropertyKey, unknown>] ? Merge<T, U, Options> : T | U

Merge deeply two objects

T
DeepMergeOptions = { arrays?: MergingStrategy; maps?: MergingStrategy; sets?: MergingStrategy; }

Deep merge options

  • arrays: MergingStrategy
    No documentation available
  • maps: MergingStrategy
    No documentation available
  • sets: MergingStrategy
    No documentation available
T
ExpandRecursively<T> = T extends Record<PropertyKey, unknown> ? T extends infer O ? [K in keyof O]: ExpandRecursively<O[K]> : never : T

Force intellisense to expand the typing to hide merging typings

T
JoinToStringOptions = { separator?: string; prefix?: string; suffix?: string; limit?: number; truncated?: string; }

Options for joinToString.

  • limit: number
    No documentation available
  • prefix: string
    No documentation available
  • separator: string
    No documentation available
  • suffix: string
    No documentation available
  • truncated: string
    No documentation available
T
MapKeyType<T> = T extends Map<infer K, unknown> ? K : never

Get map values types

T
MapValueType<T> = T extends Map<unknown, infer V> ? V : never

Get map values types

T
Merge<
T,
U,
Options,
X =
MergeRightOmitComplexes<T, U>
& MergeAllRecords<T, U, Options>
& (Options extends { sets: "replace"; } ? PartialByType<U, Set<unknown>> : MergeAllSets<T, U>)
& (Options extends { arrays: "replace"; } ? PartialByType<U, Array<unknown>> : MergeAllArrays<T, U>)
& (Options extends { maps: "replace"; } ? PartialByType<U, Map<unknown, unknown>> : MergeAllMaps<T, U>)
>
= ExpandRecursively<X>

Merge two objects

T
MergeAllArrays<
T,
U,
X = PartialByType<T, Array<unknown>>,
Y = PartialByType<U, Array<unknown>>,
Z = [K in keyof X & keyof Y]: Array<ArrayValueType<X[K]> | ArrayValueType<Y[K]>>
>
= Z

Merge all sets types definitions from keys present in both objects

T
MergeAllMaps<
T,
U,
X = PartialByType<T, Map<unknown, unknown>>,
Y = PartialByType<U, Map<unknown, unknown>>,
Z = [K in keyof X & keyof Y]: Map<
MapKeyType<X[K]> | MapKeyType<Y[K]>,
MapValueType<X[K]> | MapValueType<Y[K]>
>
>
= Z

Merge all sets types definitions from keys present in both objects

T
MergeAllRecords<
T,
U,
Options,
X = PartialByType<T, Record<PropertyKey, unknown>>,
Y = PartialByType<U, Record<PropertyKey, unknown>>,
Z = [K in keyof X & keyof Y]: DeepMerge<X[K], Y[K], Options>
>
= Z

Merge all records types definitions from keys present in both objects

T
MergeAllSets<
T,
U,
X = PartialByType<T, Set<unknown>>,
Y = PartialByType<U, Set<unknown>>,
Z = [K in keyof X & keyof Y]: Set<SetValueType<X[K]> | SetValueType<Y[K]>>
>
= Z

Merge all sets types definitions from keys present in both objects

T
MergeRightOmitComplexes<T, U, X = ObjectXorKeys<T, U> & OmitComplexes<[K in keyof U]: U[K]>> = X

Merge two objects, with left precedence

T
MergingStrategy = "replace" | "merge"

Merging strategy

T
ObjectXorKeys<
T,
U,
X = Omit<T, keyof U> & Omit<U, keyof T>,
Y = [K in keyof X]: X[K]
>
= Y

Object with keys in either T or U but not in both

T
OmitComplexes<T> = Omit<
T,
keyof PartialByType<
T,
Map<unknown, unknown>
| Set<unknown>
| Array<unknown>
| Record<PropertyKey, unknown>
>
>

Exclude map, sets and array from type

T
Order = "asc" | "desc"

Order option for SortByOptions.

T
PartialByType<T, U> = [K in keyof T in keyof T[K] extends U ? K : never]: T[K]

Filter of keys matching a given type

T
SetValueType<T> = T extends Set<infer V> ? V : never

Get set values type

T
SortByOptions = { order: Order; }

Options for sortBy.

  • order: Order
    No documentation available
aggregate_groups

Functions

f
aggregateGroups<T, A>(
record: Readonly<Record<string, ReadonlyArray<T>>>,
aggregator: (
current: T,
key: string,
first: boolean,
accumulator?: A
) => A
): Record<string, A>

Applies the given aggregator to each group in the given grouping, returning the results together with the respective group keys

associate_by

Functions

f
associateBy<T>(
array: Iterable<T>,
selector: (el: T) => string
): Record<string, T>

Transforms the given array into a Record, extracting the key of each element using the given selector. If the selector produces the same key for multiple elements, the latest one will be used (overriding the ones before it).

associate_with

Functions

f
associateWith<T>(
array: Iterable<string>,
selector: (key: string) => T
): Record<string, T>

Builds a new Record using the given array as keys and choosing a value for each key using the given selector. If any of two pairs would have the same value the latest on will be used (overriding the ones before it).

chunk

Functions

f
chunk<T>(
array: readonly T[],
size: number
): T[][]

Splits the given array into chunks of the given size and returns them.

deep_merge

Functions

f
deepMerge<
T extends Record<PropertyKey, unknown>,
U extends Record<PropertyKey, unknown>,
Options extends DeepMergeOptions = { arrays: "merge"; sets: "merge"; maps: "merge"; }
>
(
record: Readonly<T>,
other: Readonly<U>,
options?: Readonly<Options>
): DeepMerge<T, U, Options>
2 overloads

Merges the two given Records, recursively merging any nested Records with the second collection overriding the first in case of conflict

Type Aliases

T
ArrayValueType<T> = T extends Array<infer V> ? V : never

Get array values type

T
DeepMerge<T, U, Options = Record<string, MergingStrategy>> = [T, U] extends [Record<PropertyKey, unknown>, Record<PropertyKey, unknown>] ? Merge<T, U, Options> : T | U

Merge deeply two objects

T
DeepMergeOptions = { arrays?: MergingStrategy; maps?: MergingStrategy; sets?: MergingStrategy; }

Deep merge options

  • arrays: MergingStrategy
    No documentation available
  • maps: MergingStrategy
    No documentation available
  • sets: MergingStrategy
    No documentation available
T
ExpandRecursively<T> = T extends Record<PropertyKey, unknown> ? T extends infer O ? [K in keyof O]: ExpandRecursively<O[K]> : never : T

Force intellisense to expand the typing to hide merging typings

T
MapKeyType<T> = T extends Map<infer K, unknown> ? K : never

Get map values types

T
MapValueType<T> = T extends Map<unknown, infer V> ? V : never

Get map values types

T
Merge<
T,
U,
Options,
X =
MergeRightOmitComplexes<T, U>
& MergeAllRecords<T, U, Options>
& (Options extends { sets: "replace"; } ? PartialByType<U, Set<unknown>> : MergeAllSets<T, U>)
& (Options extends { arrays: "replace"; } ? PartialByType<U, Array<unknown>> : MergeAllArrays<T, U>)
& (Options extends { maps: "replace"; } ? PartialByType<U, Map<unknown, unknown>> : MergeAllMaps<T, U>)
>
= ExpandRecursively<X>

Merge two objects

T
MergeAllArrays<
T,
U,
X = PartialByType<T, Array<unknown>>,
Y = PartialByType<U, Array<unknown>>,
Z = [K in keyof X & keyof Y]: Array<ArrayValueType<X[K]> | ArrayValueType<Y[K]>>
>
= Z

Merge all sets types definitions from keys present in both objects

T
MergeAllMaps<
T,
U,
X = PartialByType<T, Map<unknown, unknown>>,
Y = PartialByType<U, Map<unknown, unknown>>,
Z = [K in keyof X & keyof Y]: Map<
MapKeyType<X[K]> | MapKeyType<Y[K]>,
MapValueType<X[K]> | MapValueType<Y[K]>
>
>
= Z

Merge all sets types definitions from keys present in both objects

T
MergeAllRecords<
T,
U,
Options,
X = PartialByType<T, Record<PropertyKey, unknown>>,
Y = PartialByType<U, Record<PropertyKey, unknown>>,
Z = [K in keyof X & keyof Y]: DeepMerge<X[K], Y[K], Options>
>
= Z

Merge all records types definitions from keys present in both objects

T
MergeAllSets<
T,
U,
X = PartialByType<T, Set<unknown>>,
Y = PartialByType<U, Set<unknown>>,
Z = [K in keyof X & keyof Y]: Set<SetValueType<X[K]> | SetValueType<Y[K]>>
>
= Z

Merge all sets types definitions from keys present in both objects

T
MergeRightOmitComplexes<T, U, X = ObjectXorKeys<T, U> & OmitComplexes<[K in keyof U]: U[K]>> = X

Merge two objects, with left precedence

T
MergingStrategy = "replace" | "merge"

Merging strategy

T
ObjectXorKeys<
T,
U,
X = Omit<T, keyof U> & Omit<U, keyof T>,
Y = [K in keyof X]: X[K]
>
= Y

Object with keys in either T or U but not in both

T
OmitComplexes<T> = Omit<
T,
keyof PartialByType<
T,
Map<unknown, unknown>
| Set<unknown>
| Array<unknown>
| Record<PropertyKey, unknown>
>
>

Exclude map, sets and array from type

T
PartialByType<T, U> = [K in keyof T in keyof T[K] extends U ? K : never]: T[K]

Filter of keys matching a given type

T
SetValueType<T> = T extends Set<infer V> ? V : never

Get set values type

distinct

Functions

f
distinct<T>(array: Iterable<T>): T[]

Returns all distinct elements in the given array, preserving order by first occurrence.

distinct_by

Functions

f
distinctBy<T, D>(
array: Iterable<T>,
selector: (el: T) => D
): T[]

Returns all elements in the given array that produce a distinct value using the given selector, preserving order by first occurrence.

drop_last_while

Functions

f
dropLastWhile<T>(
array: readonly T[],
predicate: (el: T) => boolean
): T[]

Returns a new array that drops all elements in the given collection until the last element that does not match the given predicate.

drop_while

Functions

f
dropWhile<T>(
array: readonly T[],
predicate: (el: T) => boolean
): T[]

Returns a new array that drops all elements in the given collection until the first element that does not match the given predicate.

filter_entries

Functions

f
filterEntries<T>(
record: Readonly<Record<string, T>>,
predicate: (entry: [string, T]) => boolean
): Record<string, T>

Returns a new record with all entries of the given record except the ones that do not match the given predicate.

filter_keys

Functions

f
filterKeys<T>(
record: Readonly<Record<string, T>>,
predicate: (key: string) => boolean
): Record<string, T>

Returns a new record with all entries of the given record except the ones that have a key that does not match the given predicate.

filter_values

Functions

f
filterValues<T>(
record: Readonly<Record<string, T>>,
predicate: (value: T) => boolean
): Record<string, T>

Returns a new record with all entries of the given record except the ones that have a value that does not match the given predicate.

find_single

Functions

f
findSingle<T>(
array: Iterable<T>,
predicate: (el: T) => boolean
): T | undefined

Returns an element if and only if that element is the only one matching the given condition. Returns undefined otherwise.

first_not_nullish_of

Functions

f
firstNotNullishOf<T, O>(
array: Iterable<T>,
selector: (item: T) => O | undefined | null
): NonNullable<O> | undefined

Applies the given selector to elements in the given array until a value is produced that is neither null nor undefined and returns that value. Returns undefined if no such value is produced.

includes_value

Functions

f
includesValue<T>(
record: Readonly<Record<string, T>>,
value: T
): boolean

If the given value is part of the given object it returns true, otherwise it returns false. Doesn't work with non-primitive values: includesValue({x: {}}, {}) returns false.

intersect

Functions

f
intersect<T>(...arrays: (readonly T[])[]): T[]

Returns all distinct elements that appear at least once in each of the given arrays.

join_to_string

Functions

f
joinToString<T>(
array: Iterable<T>,
selector: (el: T) => string,
unnamed 2?: Readonly<JoinToStringOptions>
): string

Transforms the elements in the given array to strings using the given selector. Joins the produced strings into one using the given separator and applying the given prefix and suffix to the whole string afterwards. If the array could be huge, you can specify a non-negative value of limit, in which case only the first limit elements will be appended, followed by the truncated string. Returns the resulting string.

Type Aliases

T
JoinToStringOptions = { separator?: string; prefix?: string; suffix?: string; limit?: number; truncated?: string; }

Options for joinToString.

  • limit: number
    No documentation available
  • prefix: string
    No documentation available
  • separator: string
    No documentation available
  • suffix: string
    No documentation available
  • truncated: string
    No documentation available
map_entries

Functions

f
mapEntries<T, O>(
record: Readonly<Record<string, T>>,
transformer: (entry: [string, T]) => [string, O]
): Record<string, O>

Applies the given transformer to all entries in the given record and returns a new record containing the results.

map_keys

Functions

f
mapKeys<T>(
record: Readonly<Record<string, T>>,
transformer: (key: string) => string
): Record<string, T>

Applies the given transformer to all keys in the given record's entries and returns a new record containing the transformed entries.

map_not_nullish

Functions

f
mapNotNullish<T, O>(
array: Iterable<T>,
transformer: (el: T) => O
): NonNullable<O>[]

Returns a new array, containing all elements in the given array transformed using the given transformer, except the ones that were transformed to null or undefined.

map_values

Functions

f
mapValues<T, O, K extends string>(
record: Record<K, T>,
transformer: (
value: T,
key: K
) => O
): any
2 overloads

Applies the given transformer to all values in the given record and returns a new record containing the resulting keys associated to the last value that produced them.

max_by

Functions

f
maxBy<T>(
array: Iterable<T>,
selector:
((el: T) => number)
| ((el: T) => string)
| ((el: T) => bigint)
| ((el: T) => Date)
): T | undefined
4 overloads

Returns the first element that is the largest value of the given function or undefined if there are no elements.

max_of

Functions

f
maxOf<T, S extends ((el: T) => number) | ((el: T) => bigint)>(
array: Iterable<T>,
selector: S
): ReturnType<S> | undefined
2 overloads

Applies the given selector to all elements of the provided collection and returns the max value of all elements. If an empty array is provided the function will return undefined

max_with

Functions

f
maxWith<T>(
array: Iterable<T>,
comparator: (
a: T,
b: T
) => number
): T | undefined

Returns the first element having the largest value according to the provided comparator or undefined if there are no elements.

min_by

Functions

f
minBy<T>(
array: Iterable<T>,
selector:
((el: T) => number)
| ((el: T) => string)
| ((el: T) => bigint)
| ((el: T) => Date)
): T | undefined
4 overloads

Returns the first element that is the smallest value of the given function or undefined if there are no elements

min_of

Functions

f
minOf<T, S extends ((el: T) => number) | ((el: T) => bigint)>(
array: Iterable<T>,
selector: S
): ReturnType<S> | undefined
2 overloads

Applies the given selector to all elements of the given collection and returns the min value of all elements. If an empty array is provided the function will return undefined.

min_with

Functions

f
minWith<T>(
array: Iterable<T>,
comparator: (
a: T,
b: T
) => number
): T | undefined

Returns the first element having the smallest value according to the provided comparator or undefined if there are no elements

partition

Functions

f
partition(
array: Iterable<unknown>,
predicate: (el: unknown) => boolean
): [unknown[], unknown[]]
2 overloads

Returns a tuple of two arrays with the first one containing all elements in the given array that match the given predicate and the second one containing all that do not.

partition_entries

Functions

f
partitionEntries<T>(
record: Readonly<Record<string, T>>,
predicate: (entry: [string, T]) => boolean
): [Record<string, T>, Record<string, T>]

Returns a tuple of two records with the first one containing all entries of the given record that match the given predicate and the second one containing all that do not.

permutations

Functions

f
permutations<T>(inputArray: Iterable<T>): T[][]

Builds all possible orders of all elements in the given array Ignores equality of elements, meaning this will always return the same number of permutations for a given length of input.

reduce_groups

Functions

f
reduceGroups<T, A>(
record: Readonly<Record<string, ReadonlyArray<T>>>,
reducer: (
accumulator: A,
current: T
) => A
,
initialValue: A
): Record<string, A>

Applies the given reducer to each group in the given grouping, returning the results together with the respective group keys.

running_reduce

Functions

f
runningReduce<T, O>(
array: readonly T[],
reducer: (
accumulator: O,
current: T,
currentIndex: number
) => O
,
initialValue: O
): O[]

Calls the given reducer on each element of the given collection, passing its result as the accumulator to the next respective call, starting with the given initialValue. Returns all intermediate accumulator results.

sample

Functions

f
sample<T>(array: readonly T[]): T | undefined

Returns a random element from the given array.

sliding_windows

Functions

f
slidingWindows<T>(
array: readonly T[],
size: number,
unnamed 2?: { step?: number; partial?: boolean; }
): T[][]

Generates sliding views of the given array of the given size and returns a new array containing all of them.

sort_by

Functions

f
sortBy<T>(
array: readonly T[],
selector:
((el: T) => number)
| ((el: T) => string)
| ((el: T) => bigint)
| ((el: T) => Date),
options?: SortByOptions
): T[]
4 overloads

Returns all elements in the given collection, sorted by their result using the given selector. The selector function is called only once for each element. Ascending or descending order can be specified.

Type Aliases

T
Order = "asc" | "desc"

Order option for SortByOptions.

T
SortByOptions = { order: Order; }

Options for sortBy.

  • order: Order
    No documentation available
sum_of

Functions

f
sumOf<T>(
array: Iterable<T>,
selector: (el: T) => number
): number

Applies the given selector to all elements in the given collection and calculates the sum of the results.

take_last_while

Functions

f
takeLastWhile<T>(
array: readonly T[],
predicate: (el: T) => boolean
): T[]

Returns all elements in the given array after the last element that does not match the given predicate.

take_while

Functions

f
takeWhile<T>(
array: readonly T[],
predicate: (el: T) => boolean
): T[]

Returns all elements in the given collection until the first element that does not match the given predicate.

union

Functions

f
union<T>(...arrays: Iterable<T>[]): T[]

Returns all distinct elements that appear in any of the given arrays.

unzip

Functions

f
unzip<T, U>(pairs: readonly [T, U][]): [T[], U[]]

Builds two separate arrays from the given array of 2-tuples, with the first returned array holding all first tuple elements and the second one holding all the second elements.

without_all

Functions

f
withoutAll<T>(
array: readonly T[],
values: readonly T[]
): T[]

Returns an array excluding all given values.

zip

Functions

f
zip<T extends unknown[]>(...arrays: [K in keyof T]: ReadonlyArray<T[K]>): T[]

Builds N-tuples of elements from the given N arrays with matching indices, stopping when the smallest array's end is reached.

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@std/collections

Import symbol

import * as collections from "@std/collections";
or

Import directly with a jsr specifier

import * as collections from "jsr:@std/collections";