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).
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).
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
Applies the given selector to each element in the given array, returning a Record containing the results as keys
and all values that produced that key as values.
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.
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.
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
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.
Applies the given selector to all elements of the given collection and
returns the max value of all elements. If an empty array is provided the
function will return undefined
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
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
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.
Calls the given reducer on each element of the given collection, passing it's
result as the accumulator to the next respective call, starting with the given
initialValue. Returns all intermediate accumulator results.
Returns all elements in the given collection, sorted stably by their result using the given selector. The selector function is called only once for each element.
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
This module includes pure functions for specific common tasks around collection
types like Array and Record. This module is heavily inspired by kotlins
stdlib.
All provided functions are pure, which also means that they do not
mutate your inputs, returning a new value instead.
All functions are importable on their own by referencing their snake_case
named file (e.g. collections/sort_by.ts)
If you want to contribute or understand why this is done the way it is, see the
contribution guide.
Usage
aggregateGroups
Applies the given aggregator to each group in the given grouping, returning the
results together with the respective group keys
import{ aggregateGroups }from"https://deno.land/std@0.116.0/collections/mod.ts";import{ assertEquals }from"https://deno.land/std@0.116.0/testing/asserts.ts";const foodProperties ={"Curry":["spicy","vegan"],"Omelette":["creamy","vegetarian"],};const descriptions =aggregateGroups(
foodProperties,(current, key, first, acc)=>{if(first){return`${key} is ${current}`;}return`${acc} and ${current}`;},);assertEquals(descriptions,{"Curry":"Curry is spicy and vegan","Omelette":"Omelette is creamy and vegetarian",});
associateBy
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).
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).
Splits the given array into chunks of the given size and returns them.
import{ chunk }from"https://deno.land/std@0.116.0/collections/mod.ts";import{ assertEquals }from"https://deno.land/std@0.116.0/testing/asserts.ts";const words =["lorem","ipsum","dolor","sit","amet","consetetur","sadipscing",];const chunks =chunk(words,3);assertEquals(
chunks,[["lorem","ipsum","dolor"],["sit","amet","consetetur"],["sadipscing"],],);
deepMerge
Merges the two given Records, recursively merging any nested Records with the
second collection overriding the first in case of conflict
For arrays, maps and sets, a merging strategy can be specified to either
replace values, or merge them instead. Use includeNonEnumerable option to
include non enumerable properties too.
import{ deepMerge }from"https://deno.land/std@0.116.0/collections/mod.ts";import{ assertEquals }from"https://deno.land/std@0.116.0/testing/asserts.ts";const a ={ foo:true};const b ={ foo:{ bar:true}};assertEquals(deepMerge(a, b),{ foo:{ bar:true}});
distinctBy
Returns all elements in the given array that produce a distinct value using the
given selector, preserving order by first occurrence.
Returns an element if and only if that element is the only one matching the
given condition. Returns undefined otherwise.
import{ findSingle }from"https://deno.land/std@0.116.0/collections/mod.ts";import{ assertEquals }from"https://deno.land/std@0.116.0/testing/asserts.ts";const bookings =[{ month:"January", active:false},{ month:"March", active:false},{ month:"June", active:true},];const activeBooking =findSingle(bookings,(it)=> it.active);const inactiveBooking =findSingle(bookings,(it)=>!it.active);assertEquals(activeBooking,{ month:"June", active:true});assertEquals(inactiveBooking,undefined);// there are two applicable items
firstNotNullishOf
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
Applies the given selector to each element in the given array, returning a
Record containing the results as keys and all values that produced that key as
values.
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.
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.
import{ joinToString }from"https://deno.land/std@0.116.0/collections/mod.ts";import{ assertEquals }from"https://deno.land/std@0.116.0/testing/asserts.ts";const users =[{ name:"Kim"},{ name:"Anna"},{ name:"Tim"},];const message =joinToString(users,(it)=> it.name,{
suffix:" are winners",
prefix:"result: ",
separator:" and ",
limit:1,
truncated:"others",});assertEquals(message,"result: Kim and others are winners");
mapEntries
Applies the given transformer to all entries in the given record and returns a
new record containing the results.
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.
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.
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
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
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.
import{ partition }from"https://deno.land/std@0.116.0/collections/mod.ts";import{ assertEquals }from"https://deno.land/std@0.116.0/testing/asserts.ts";const numbers =[5,6,7,8,9];const[even, odd]=partition(numbers,(it)=> it %2==0);assertEquals(even,[6,8]);assertEquals(odd,[5,7,9]);
permutations
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.
import{ permutations }from"https://deno.land/std@0.116.0/collections/mod.ts";import{ assertEquals }from"https://deno.land/std@0.116.0/testing/asserts.ts";const numbers =[1,2];const windows =permutations(numbers);assertEquals(
windows,[[1,2],[2,1],],);
reduceGroups
Applies the given reducer to each group in the given Grouping, returning the
results together with the respective group keys
Calls the given reducer on each element of the given collection, passing it's
result as the accumulator to the next respective call, starting with the given
initialValue. Returns all intermediate accumulator results.
import{ sample }from"https://deno.land/std@0.116.0/collections/mod.ts";import{ assert }from"https://deno.land/std@0.116.0/testing/asserts.ts";const numbers =[1,2,3,4];const random =sample(numbers);assert(numbers.includes(random asnumber));
slidingWindows
Generates sliding views of the given array of the given size and returns a new
array containing all of them.
If step is set, each window will start that many elements after the last
window's start. (Default: 1)
If partial is set, windows will be generated for the last elements of the
collection, resulting in some undefined values if size is greater than 1.
(Default: false)
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.
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