declare namespace denostd { export namespace collections { /** * Applies the given aggregator to each group in the given Grouping, returning the results together with the respective group keys * * ```ts * import { aggregateGroups } from "https://deno.land/std@$STD_VERSION/collections/mod.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/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', * }) * ``` */ export function aggregateGroups(record: Readonly>>, aggregator: (current: T, key: string, first: boolean, accumulator?: A) => A): Record; /** Get array values type */ type ArrayValueType = T extends Array ? V : never; /** This module is browser compatible. */ /** Compares its two arguments for ascending order using JavaScript's built in comparison operators. */ export function ascend(a: T, b: T): 1 | -1 | 0; /** * 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). * * Example: * * ```ts * import { associateBy } from "https://deno.land/std@$STD_VERSION/collections/mod.ts" * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * const users = [ * { id: 'a2e', userName: 'Anna' }, * { id: '5f8', userName: 'Arnold' }, * { id: 'd2c', userName: 'Kim' }, * ] * const usersById = associateBy(users, it => it.id) * * assertEquals(usersById, { * 'a2e': { id: 'a2e', userName: 'Anna' }, * '5f8': { id: '5f8', userName: 'Arnold' }, * 'd2c': { id: 'd2c', userName: 'Kim' }, * }) * ``` */ export function associateBy(array: readonly T[], selector: (el: T) => string): Record; /** * 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). * * Example: * * ```ts * import { associateWith } from "https://deno.land/std@$STD_VERSION/collections/mod.ts" * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * const names = [ 'Kim', 'Lara', 'Jonathan' ] * const namesToLength = associateWith(names, it => it.length) * * assertEquals(namesToLength, { * 'Kim': 3, * 'Lara': 4, * 'Jonathan': 8, * }) * ``` */ export function associateWith(array: readonly string[], selector: (key: string) => T): Record; /** * A priority queue implemented with a binary heap. The heap is in decending order by default, * using JavaScript's built in comparison operators to sort the values. */ export class BinaryHeap implements Iterable { private compare; private data; constructor(compare?: (a: T, b: T) => number); /** Creates a new binary heap from an array like or iterable object. */ static from(collection: ArrayLike | Iterable | BinaryHeap): BinaryHeap; static from(collection: ArrayLike | Iterable | BinaryHeap, options: { compare?: (a: T, b: T) => number; }): BinaryHeap; static from(collection: ArrayLike | Iterable | BinaryHeap, options: { compare?: (a: U, b: U) => number; map: (value: T, index: number) => U; thisArg?: V; }): BinaryHeap; /** The amount of values stored in the binary heap. */ get length(): number; /** Returns the greatest value in the binary heap, or undefined if it is empty. */ peek(): T | undefined; /** Removes the greatest value from the binary heap and returns it, or null if it is empty. */ pop(): T | undefined; /** Adds values to the binary heap. */ push(...values: T[]): number; /** Removes all values from the binary heap. */ clear(): void; /** Checks if the binary heap is empty. */ isEmpty(): boolean; /** Returns an iterator for retrieving and removing values from the binary heap. */ drain(): IterableIterator; [Symbol.iterator](): IterableIterator; } class BSNode { parent: BSNode | null; value: T; left: BSNode | null; right: BSNode | null; constructor(parent: BSNode | null, value: T); static from(node: BSNode): BSNode; directionFromParent(): direction | null; findMinNode(): BSNode; findMaxNode(): BSNode; findSuccessorNode(): BSNode | null; } /** * An unbalanced binary search tree. The values are in ascending order by default, * using JavaScript's built in comparison operators to sort the values. */ export class BSTree implements Iterable { protected compare: (a: T, b: T) => number; protected root: BSNode | null; protected _size: number; constructor(compare?: (a: T, b: T) => number); /** Creates a new binary search tree from an array like or iterable object. */ static from(collection: ArrayLike | Iterable | BSTree): BSTree; static from(collection: ArrayLike | Iterable | BSTree, options: { compare?: (a: T, b: T) => number; }): BSTree; static from(collection: ArrayLike | Iterable | BSTree, options: { compare?: (a: U, b: U) => number; map: (value: T, index: number) => U; thisArg?: V; }): BSTree; /** The amount of values stored in the binary search tree. */ get size(): number; protected findNode(value: T): BSNode | null; protected rotateNode(node: BSNode, direction: direction): void; protected insertNode(Node: typeof BSNode, value: T): BSNode | null; protected removeNode(value: T): BSNode | null; /** * Adds the value to the binary search tree if it does not already exist in it. * Returns true if successful. */ insert(value: T): boolean; /** * Removes node value from the binary search tree if found. * Returns true if found and removed. */ remove(value: T): boolean; /** Returns node value if found in the binary search tree. */ find(value: T): T | null; /** Returns the minimum value in the binary search tree or null if empty. */ min(): T | null; /** Returns the maximum value in the binary search tree or null if empty. */ max(): T | null; /** Removes all values from the binary search tree. */ clear(): void; /** Checks if the binary search tree is empty. */ isEmpty(): boolean; /** * Returns an iterator that uses in-order (LNR) tree traversal for * retrieving values from the binary search tree. */ lnrValues(): IterableIterator; /** * Returns an iterator that uses reverse in-order (RNL) tree traversal for * retrieving values from the binary search tree. */ rnlValues(): IterableIterator; /** * Returns an iterator that uses pre-order (NLR) tree traversal for * retrieving values from the binary search tree. */ nlrValues(): IterableIterator; /** * Returns an iterator that uses post-order (LRN) tree traversal for * retrieving values from the binary search tree. */ lrnValues(): IterableIterator; /** * Returns an iterator that uses level order tree traversal for * retrieving values from the binary search tree. */ lvlValues(): IterableIterator; /** * Returns an iterator that uses in-order (LNR) tree traversal for * retrieving values from the binary search tree. */ [Symbol.iterator](): IterableIterator; } /** * Splits the given array into chunks of the given size and returns them * * Example: * * ```ts * import { chunk } from "https://deno.land/std@$STD_VERSION/collections/mod.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/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' ], * ]) * ``` */ export function chunk(array: readonly T[], size: number): T[][]; /** Merge deeply two objects */ export type DeepMerge> = [ T, U ] extends [Record, Record] ? Merge : // Handle primitives T | U; /** * 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. * * Example: * * ```ts * import { deepMerge } from "https://deno.land/std@$STD_VERSION/collections/mod.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * const a = {foo: true} * const b = {foo: {bar: true}} * * assertEquals(deepMerge(a, b), {foo: {bar: true}}); * ``` */ export function deepMerge>(record: Partial>, other: Partial>, options?: Readonly): T; export function deepMerge, U extends Record, Options extends DeepMergeOptions>(record: Readonly, other: Readonly, options?: Readonly): DeepMerge; /** Deep merge options */ export type DeepMergeOptions = { /** Merging strategy for arrays */ arrays?: MergingStrategy; /** Merging strategy for Maps */ maps?: MergingStrategy; /** Merging strategy for Sets */ sets?: MergingStrategy; }; /** Compares its two arguments for descending order using JavaScript's built in comparison operators. */ export function descend(a: T, b: T): 1 | -1 | 0; /** This module is browser compatible. */ type direction = "left" | "right"; /** * Returns all distinct elements in the given array, preserving order by first occurrence * * Example: * * ```ts * import { distinct } from "https://deno.land/std@$STD_VERSION/collections/mod.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * const numbers = [ 3, 2, 5, 2, 5 ] * const distinctNumbers = distinct(numbers) * * assertEquals(distinctNumbers, [ 3, 2, 5 ]) * ``` */ export function distinct(array: readonly T[]): T[]; /** * Returns all elements in the given array that produce a distinct value using the given selector, preserving order by first occurrence * * Example: * * ```ts * import { distinctBy } from "https://deno.land/std@$STD_VERSION/collections/mod.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * const names = [ 'Anna', 'Kim', 'Arnold', 'Kate' ] * const exampleNamesByFirstLetter = distinctBy(names, it => it.charAt(0)) * * assertEquals(exampleNamesByFirstLetter, [ 'Anna', 'Kim' ]) * ``` */ export function distinctBy(array: readonly T[], selector: (el: T) => D): T[]; /** * Returns a new array that drops all elements in the given collection until the * last element that does not match the given predicate * * Example: * ```ts * import { dropLastWhile } from "https://deno.land/std@$STD_VERSION/collections/mod.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * const numbers = [22, 30, 44]; * * const notFortyFour = dropLastWhile(numbers, i => i != 44); * * assertEquals( * notFortyFour, * [22, 30] * ); * ``` */ export function dropLastWhile(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 * * Example: * * ```ts * import { dropWhile } from "https://deno.land/std@$STD_VERSION/collections/mod.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * const numbers = [ 3, 2, 5, 2, 5 ] * const dropWhileNumbers = dropWhile(numbers, i => i !== 2) * * assertEquals(dropWhileNumbers, [2, 5, 2, 5 ]) * ``` */ export function dropWhile(array: readonly T[], predicate: (el: T) => boolean): T[]; /** * How does recursive typing works ? * * Deep merging process is handled through `DeepMerge` type. * If both T and U are Records, we recursively merge them, * else we treat them as primitives. * * Merging process is handled through `Merge` type, in which * we remove all maps, sets, arrays and records so we can handle them * separately depending on merging strategy: * * Merge< * {foo: string}, * {bar: string, baz: Set}, * > // "foo" and "bar" will be handled with `MergeRightOmitComplexes` * // "baz" will be handled with `MergeAll*` type * * `MergeRightOmitComplexes` will do the above: all T's * exclusive keys will be kept, though common ones with U will have their * typing overridden instead: * * MergeRightOmitComplexes< * {foo: string, baz: number}, * {foo: boolean, bar: string} * > // {baz: number, foo: boolean, bar: string} * // "baz" was kept from T * // "foo" was overridden by U's typing * // "bar" was added from U * * For Maps, Arrays, Sets and Records, we use `MergeAll*` utility * types. They will extract relevant data structure from both T and U * (providing that both have same data data structure, except for typing). * * From these, `*ValueType` will extract values (and keys) types to be * able to create a new data structure with an union typing from both * data structure of T and U: * * MergeAllSets< * {foo: Set}, * {foo: Set} * > // `SetValueType` will extract "number" for T * // `SetValueType` will extract "string" for U * // `MergeAllSets` will infer type as Set * // Process is similar for Maps, Arrays, and Sets * * `DeepMerge` is taking a third argument to be handle to * infer final typing depending on merging strategy: * * & (Options extends { sets: "replace" } ? PartialByType> * : MergeAllSets) * * In the above line, if "Options" have its merging strategy for Sets set to * "replace", instead of performing merging of Sets type, it will take the * typing from right operand (U) instead, effectively replacing the typing. * * An additional note, we use `ExpandRecursively` utility type to expand * the resulting typing and hide all the typing logic of deep merging so it is * more user friendly. */ /** Force intellisense to expand the typing to hide merging typings */ type ExpandRecursively = T extends Record ? T extends infer O ? { [K in keyof O]: ExpandRecursively; } : never : T; /** * Returns a new record with all entries of the given record except the ones that do not match the given predicate * * Example: * * ```ts * import { filterEntries } from "https://deno.land/std@$STD_VERSION/collections/mod.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * const menu = { * 'Salad': 11, * 'Soup': 8, * 'Pasta': 13, * } as const; * const myOptions = filterEntries(menu, * ([ item, price ]) => item !== 'Pasta' && price < 10, * ) * * assertEquals(myOptions, { * 'Soup': 8, * }) * ``` */ export function filterEntries(record: Readonly>, predicate: (entry: [string, T]) => boolean): Record; /** * 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 * * Example: * * ```ts * import { filterKeys } from "https://deno.land/std@$STD_VERSION/collections/mod.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * const menu = { * 'Salad': 11, * 'Soup': 8, * 'Pasta': 13, * } * const menuWithoutSalad = filterKeys(menu, it => it !== 'Salad') * * assertEquals(menuWithoutSalad, { * 'Soup': 8, * 'Pasta': 13, * }) * ``` */ export function filterKeys(record: Readonly>, predicate: (key: string) => boolean): Record; /** * 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 * * Example: * * ```ts * import { filterValues } from "https://deno.land/std@$STD_VERSION/collections/mod.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * type Person = { age: number }; * * const people: Record = { * 'Arnold': { age: 37 }, * 'Sarah': { age: 7 }, * 'Kim': { age: 23 }, * }; * const adults = filterValues(people, it => it.age >= 18) * * assertEquals(adults, { * 'Arnold': { age: 37 }, * 'Kim': { age: 23 }, * }) * ``` */ export function filterValues(record: Readonly>, predicate: (value: T) => boolean): Record; /** * Returns an element if and only if that element is the only one matching the given condition. Returns `undefined` otherwise. * * Example: * * ```ts * import { findSingle } from "https://deno.land/std@$STD_VERSION/collections/mod.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/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 * ``` */ export function findSingle(array: readonly T[], predicate: (el: T) => boolean): T | 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 * * Example: * * ```ts * import { firstNotNullishOf } from "https://deno.land/std@$STD_VERSION/collections/mod.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * const tables = [ * { number: 11, order: null }, * { number: 12, order: 'Soup' }, * { number: 13, order: 'Salad' }, * ] * const nextOrder = firstNotNullishOf(tables, it => it.order) * * assertEquals(nextOrder, 'Soup') * ``` */ export function firstNotNullishOf(array: readonly T[], selector: (item: T) => O | undefined | null): NonNullable | undefined; /** * 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. * * Example: * * ```ts * import { groupBy } from "https://deno.land/std@$STD_VERSION/collections/mod.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * type Person = { * name: string; * }; * * const people: Person[] = [ * { name: 'Anna' }, * { name: 'Arnold' }, * { name: 'Kim' }, * ]; * const peopleByFirstLetter = groupBy(people, it => it.name.charAt(0)) * * assertEquals(peopleByFirstLetter, { * 'A': [ { name: 'Anna' }, { name: 'Arnold' } ], * 'K': [ { name: 'Kim' } ], * }) * ``` */ export function groupBy(array: readonly T[], selector: (el: T) => K): Partial>; /** * 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. * * Example: * ```ts * import { includesValue } from "https://deno.land/std@$STD_VERSION/collections/mod.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * const input = { * first: 33, * second: 34, * }; * * assertEquals(includesValue(input, 34), true); */ export function includesValue(record: Readonly>, value: T): boolean; /** * Returns all distinct elements that appear at least once in each of the given arrays * * Example: * * ```ts * import { intersect } from "https://deno.land/std@$STD_VERSION/collections/mod.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * const lisaInterests = [ 'Cooking', 'Music', 'Hiking' ] * const kimInterests = [ 'Music', 'Tennis', 'Cooking' ] * const commonInterests = intersect(lisaInterests, kimInterests) * * assertEquals(commonInterests, [ 'Cooking', 'Music' ]) * ``` */ export function intersect(...arrays: (readonly T[])[]): T[]; /** * 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. * * Example: * * ```ts * import { joinToString } from "https://deno.land/std@$STD_VERSION/collections/mod.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/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"); * ``` */ export function joinToString(array: readonly T[], selector: (el: T) => string, { separator, prefix, suffix, limit, truncated, }?: Readonly): string; /** * Options for joinToString */ export type JoinToStringOptions = { separator?: string; prefix?: string; suffix?: string; limit?: number; truncated?: string; }; /** * Applies the given transformer to all entries in the given record and returns a new record containing the results * * Example: * * ```ts * import { mapEntries } from "https://deno.land/std@$STD_VERSION/collections/mod.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * const usersById = { * 'a2e': { name: 'Kim', age: 22 }, * 'dfe': { name: 'Anna', age: 31 }, * '34b': { name: 'Tim', age: 58 }, * } as const; * * const agesByNames = mapEntries(usersById, * ([ id, { name, age } ]) => [ name, age ], * ) * * assertEquals(agesByNames, { * 'Kim': 22, * 'Anna': 31, * 'Tim': 58, * }) * ``` */ export function mapEntries(record: Readonly>, transformer: (entry: [string, T]) => [string, O]): Record; /** * Applies the given transformer to all keys in the given record's entries and returns a new record containing the * transformed entries. * * If the transformed entries contain the same key multiple times, only the last one will appear in the returned record. * * Example: * * ```ts * import { mapKeys } from "https://deno.land/std@$STD_VERSION/collections/mod.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * const counts = { a: 5, b: 3, c: 8 } * * assertEquals(mapKeys(counts, it => it.toUpperCase()), { * A: 5, * B: 3, * C: 8, * }) * ``` */ export function mapKeys(record: Readonly>, transformer: (key: string) => string): Record; /** Get map values types */ type MapKeyType = T extends Map ? K : never; /** * 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` * * Example: * * ```ts * import { mapNotNullish } from "https://deno.land/std@$STD_VERSION/collections/mod.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * const people = [ * { middleName: null }, * { middleName: 'William' }, * { middleName: undefined }, * { middleName: 'Martha' }, * ] * const foundMiddleNames = mapNotNullish(people, it => it.middleName) * * assertEquals(foundMiddleNames, [ 'William', 'Martha' ]) * ``` */ export function mapNotNullish(array: readonly T[], transformer: (el: T) => O): NonNullable[]; /** * 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. * * Example: * * ```ts * import { mapValues } from "https://deno.land/std@$STD_VERSION/collections/mod.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * const usersById = { * 'a5ec': { name: 'Mischa' }, * 'de4f': { name: 'Kim' }, * } * const namesById = mapValues(usersById, it => it.name) * * assertEquals(namesById, { * 'a5ec': 'Mischa', * 'de4f': 'Kim', * }); * ``` */ export function mapValues(record: Readonly>, transformer: (value: T) => O): Record; /** Get map values types */ type MapValueType = T extends Map ? V : never; /** * Returns the first element that is the largest value of the given function or undefined if there are no elements. * * Example: * * ```ts * import { maxBy } from "https://deno.land/std@$STD_VERSION/collections/mod.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * const people = [ * { name: 'Anna', age: 34 }, * { name: 'Kim', age: 42 }, * { name: 'John', age: 23 }, * ]; * * const personWithMaxAge = maxBy(people, i => i.age); * * assertEquals(personWithMaxAge, { name: 'Kim', age: 42 }); * ``` */ export function maxBy(array: readonly T[], selector: (el: T) => number): T | undefined; export function maxBy(array: readonly T[], selector: (el: T) => string): T | undefined; export function maxBy(array: readonly T[], selector: (el: T) => bigint): T | undefined; export function maxBy(array: readonly T[], selector: (el: T) => Date): T | undefined; /** * 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 * * Example: * * ```ts * import { maxOf } from "https://deno.land/std@$STD_VERSION/collections/mod.ts" * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts" * * const inventory = [ * { name: "mustard", count: 2 }, * { name: "soy", count: 4 }, * { name: "tomato", count: 32 }, * ]; * const maxCount = maxOf(inventory, (i) => i.count); * * assertEquals(maxCount, 32); * ``` */ export function maxOf(array: readonly T[], selector: (el: T) => number): number | undefined; export function maxOf(array: readonly T[], selector: (el: T) => bigint): bigint | undefined; /** * Returns the first element having the largest value according to the provided * comparator or undefined if there are no elements. * * The comparator is expected to work exactly like one passed to `Array.sort`, which means * that `comparator(a, b)` should return a negative number if `a < b`, a positive number if `a > b` * and `0` if `a == b`. * * Example: * * ```ts * import { maxWith } from "https://deno.land/std@$STD_VERSION/collections/mod.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * const people = ["Kim", "Anna", "John", "Arthur"]; * const largestName = maxWith(people, (a, b) => a.length - b.length); * * assertEquals(largestName, "Arthur"); * ``` */ export function maxWith(array: readonly T[], comparator: (a: T, b: T) => number): T | undefined; /** Merge two objects */ type Merge & MergeAllRecords & (Options extends { sets: "replace"; } ? PartialByType> : MergeAllSets) & (Options extends { arrays: "replace"; } ? PartialByType> : MergeAllArrays) & (Options extends { maps: "replace"; } ? PartialByType> : MergeAllMaps)> = ExpandRecursively; /** Merge all sets types definitions from keys present in both objects */ type MergeAllArrays>, Y = PartialByType>, Z = { [K in keyof X & keyof Y]: Array | ArrayValueType>; }> = Z; /** Merge all sets types definitions from keys present in both objects */ type MergeAllMaps>, Y = PartialByType>, Z = { [K in keyof X & keyof Y]: Map | MapKeyType, MapValueType | MapValueType>; }> = Z; /** Merge all records types definitions from keys present in both objects */ type MergeAllRecords>, Y = PartialByType>, Z = { [K in keyof X & keyof Y]: DeepMerge; }> = Z; /** Merge all sets types definitions from keys present in both objects */ type MergeAllSets>, Y = PartialByType>, Z = { [K in keyof X & keyof Y]: Set | SetValueType>; }> = Z; /** Merge two objects, with left precedence */ type MergeRightOmitComplexes & OmitComplexes<{ [K in keyof U]: U[K]; }>> = X; /** Merging strategy */ export type MergingStrategy = "replace" | "merge"; /** * Returns the first element that is the smallest value of the given function or undefined if there are no elements. * * Example: * * ```ts * import { minBy } from "https://deno.land/std@$STD_VERSION/collections/mod.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts" * * const people = [ * { name: 'Anna', age: 34 }, * { name: 'Kim', age: 42 }, * { name: 'John', age: 23 }, * ]; * * const personWithMinAge = minBy(people, i => i.age); * * assertEquals(personWithMinAge, { name: 'John', age: 23 }); * ``` */ export function minBy(array: readonly T[], selector: (el: T) => number): T | undefined; export function minBy(array: readonly T[], selector: (el: T) => string): T | undefined; export function minBy(array: readonly T[], selector: (el: T) => bigint): T | undefined; export function minBy(array: readonly T[], selector: (el: T) => Date): T | 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 * * Example: * * ```ts * import { minOf } from "https://deno.land/std@$STD_VERSION/collections/mod.ts" * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts" * * const inventory = [ * { name: "mustard", count: 2 }, * { name: "soy", count: 4 }, * { name: "tomato", count: 32 }, * ]; * const minCount = minOf(inventory, (i) => i.count); * * assertEquals(minCount, 2); * ``` */ export function minOf(array: readonly T[], selector: (el: T) => number): number | undefined; export function minOf(array: readonly T[], selector: (el: T) => bigint): bigint | undefined; /** * Returns the first element having the smallest value according to the provided comparator or undefined if there are no elements * * Example: * * ```ts * import { minWith } from "https://deno.land/std@$STD_VERSION/collections/mod.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * const people = ["Kim", "Anna", "John"]; * const smallestName = minWith(people, (a, b) => a.length - b.length); * * assertEquals(smallestName, "Kim"); * ``` */ export function minWith(array: readonly T[], comparator: (a: T, b: T) => number): T | undefined; /** Object with keys in either T or U but not in both */ type ObjectXorKeys & Omit, Y = { [K in keyof X]: X[K]; }> = Y; /** Exclude map, sets and array from type */ type OmitComplexes = Omit | Set | Array | Record>>; /** Filter of keys matching a given type */ type PartialByType = { [K in keyof T as T[K] extends U ? K : never]: T[K]; }; /** * 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 * * Example: * * ```ts * import { partition } from "https://deno.land/std@$STD_VERSION/collections/mod.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/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 ]) * ``` */ export function partition(array: readonly T[], predicate: (el: T) => boolean): [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. * * Example: * * ```ts * import { permutations } from "https://deno.land/std@$STD_VERSION/collections/mod.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * const numbers = [ 1, 2 ] * const windows = permutations(numbers) * * assertEquals(windows, [ * [ 1, 2 ], * [ 2, 1 ], * ]) * ``` */ export function permutations(inputArray: readonly T[]): T[][]; class RBNode extends BSNode { parent: RBNode | null; left: RBNode | null; right: RBNode | null; red: boolean; constructor(parent: RBNode | null, value: T); static from(node: RBNode): RBNode; } /** * A red-black tree. This is a kind of self-balancing binary search tree. * The values are in ascending order by default, * using JavaScript's built in comparison operators to sort the values. */ export class RBTree extends BSTree { protected root: RBNode | null; constructor(compare?: (a: T, b: T) => number); /** Creates a new red-black tree from an array like or iterable object. */ static from(collection: ArrayLike | Iterable | RBTree): RBTree; static from(collection: ArrayLike | Iterable | RBTree, options: { Node?: typeof RBNode; compare?: (a: T, b: T) => number; }): RBTree; static from(collection: ArrayLike | Iterable | RBTree, options: { compare?: (a: U, b: U) => number; map: (value: T, index: number) => U; thisArg?: V; }): RBTree; protected removeFixup(parent: RBNode | null, current: RBNode | null): void; /** * Adds the value to the binary search tree if it does not already exist in it. * Returns true if successful. */ insert(value: T): boolean; /** * Removes node value from the binary search tree if found. * Returns true if found and removed. */ remove(value: T): boolean; } /** * Applies the given reducer to each group in the given Grouping, returning the results together with the respective group keys * * ```ts * import { reduceGroups } from "https://deno.land/std@$STD_VERSION/collections/mod.ts" * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * const votes = { * 'Woody': [ 2, 3, 1, 4 ], * 'Buzz': [ 5, 9 ], * } * const totalVotes = reduceGroups(votes, (sum, it) => sum + it, 0) * * assertEquals(totalVotes, { * 'Woody': 10, * 'Buzz': 14, * }) * ``` */ export function reduceGroups(record: Readonly>>, reducer: (accumulator: A, current: T) => A, initialValue: A): Record; /** * 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. * * Example: * * ```ts * import { runningReduce } from "https://deno.land/std@$STD_VERSION/collections/mod.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * const numbers = [1, 2, 3, 4, 5]; * const sumSteps = runningReduce(numbers, (sum, current) => sum + current, 0); * * assertEquals(sumSteps, [1, 3, 6, 10, 15]); * ``` */ export function runningReduce(array: readonly T[], reducer: (accumulator: O, current: T, currentIndex: number) => O, initialValue: O): O[]; /** * Returns a random element from the given array. * * Example: * * ```ts * import { sample } from "https://deno.land/std@$STD_VERSION/collections/mod.ts" * import { assert } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * const numbers = [1, 2, 3, 4]; * const random = sample(numbers); * * assert(numbers.includes(random as number)); * ``` */ export function sample(array: readonly T[]): T | undefined; /** Get set values type */ type SetValueType = T extends Set ? V : never; /** * 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) * * Example: * * ```ts * import { slidingWindows } from "https://deno.land/std@$STD_VERSION/collections/mod.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * const numbers = [1, 2, 3, 4, 5]; * * const windows = slidingWindows(numbers, 3); * assertEquals(windows, [ * [1, 2, 3], * [2, 3, 4], * [3, 4, 5], * ]); * * const windowsWithStep = slidingWindows(numbers, 3, { step: 2 }); * assertEquals(windowsWithStep, [ * [1, 2, 3], * [3, 4, 5], * ]); * * const windowsWithPartial = slidingWindows(numbers, 3, { partial: true }); * assertEquals(windowsWithPartial, [ * [1, 2, 3], * [2, 3, 4], * [3, 4, 5], * [4, 5], * [5], * ]); * ``` */ export function slidingWindows(array: readonly T[], size: number, { step, partial }?: { /** * If step is set, each window will start that many elements after the last * window's start. (Default: 1) */ step?: number; /** * 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) */ partial?: boolean; }): T[][]; /** * 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. * * Example: * * ```ts * import { sortBy } from "https://deno.land/std@$STD_VERSION/collections/mod.ts" * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * const people = [ * { name: 'Anna', age: 34 }, * { name: 'Kim', age: 42 }, * { name: 'John', age: 23 }, * ] * const sortedByAge = sortBy(people, it => it.age) * * assertEquals(sortedByAge, [ * { name: 'John', age: 23 }, * { name: 'Anna', age: 34 }, * { name: 'Kim', age: 42 }, * ]) * ``` */ export function sortBy(array: readonly T[], selector: ((el: T) => number)): T[]; export function sortBy(array: readonly T[], selector: ((el: T) => string)): T[]; export function sortBy(array: readonly T[], selector: ((el: T) => bigint)): T[]; export function sortBy(array: readonly T[], selector: ((el: T) => Date)): T[]; /** * Applies the given selector to all elements in the given collection and calculates the sum of the results * * Example: * * ```ts * import { sumOf } from "https://deno.land/std@$STD_VERSION/collections/mod.ts" * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts" * * const people = [ * { name: 'Anna', age: 34 }, * { name: 'Kim', age: 42 }, * { name: 'John', age: 23 }, * ] * const totalAge = sumOf(people, i => i.age) * * assertEquals(totalAge, 99) * ``` */ export function sumOf(array: readonly T[], selector: (el: T) => number): number; /** * Returns all elements in the given array after the last element that does not * match the given predicate. * * Example: * ```ts * import { takeLastWhile } from "https://deno.land/std@$STD_VERSION/collections/mod.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * const arr = [1, 2, 3, 4, 5, 6]; * * assertEquals( * takeLastWhile(arr, (i) => i > 4), * [5, 6], * ); * ``` */ export function takeLastWhile(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. * * Example: * ```ts * import { takeWhile } from "https://deno.land/std@$STD_VERSION/collections/mod.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * const arr = [1, 2, 3, 4, 5, 6]; * * assertEquals( * takeWhile(arr, (i) => i !== 4), * [1, 2, 3], * ); * ``` */ export function takeWhile(array: readonly T[], predicate: (el: T) => boolean): T[]; /** * Returns all distinct elements that appear in any of the given arrays * * Example: * * ```ts * import { union } from "https://deno.land/std@$STD_VERSION/collections/mod.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * const soupIngredients = [ 'Pepper', 'Carrots', 'Leek' ] * const saladIngredients = [ 'Carrots', 'Radicchio', 'Pepper' ] * const shoppingList = union(soupIngredients, saladIngredients) * * assertEquals(shoppingList, [ 'Pepper', 'Carrots', 'Leek', 'Radicchio' ]) * ``` */ export function union(...arrays: (readonly T[])[]): T[]; /** * 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 * * Example: * * ```ts * import { unzip } from "https://deno.land/std@$STD_VERSION/collections/mod.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * const parents = [ * [ 'Maria', 'Jeff' ], * [ 'Anna', 'Kim' ], * [ 'John', 'Leroy' ], * ] as [string, string][]; * * const [ moms, dads ] = unzip(parents); * * assertEquals(moms, [ 'Maria', 'Anna', 'John' ]); * assertEquals(dads, [ 'Jeff', 'Kim', 'Leroy' ]); * ``` */ export function unzip(pairs: readonly [T, U][]): [T[], U[]]; /** * Returns an array excluding all given values. * * Example: * * ```ts * import { withoutAll } from "https://deno.land/std@$STD_VERSION/collections/mod.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * const withoutList = withoutAll([2, 1, 2, 3], [1, 2]); * * assertEquals(withoutList, [3]); * ``` */ export function withoutAll(array: readonly T[], values: readonly T[]): T[]; export function zip(...arrays: { [K in keyof T]: T[K][]; }): T[]; export { } } }