Skip to content

Latest commit

 

History

History
262 lines (232 loc) · 11.1 KB

File metadata and controls

262 lines (232 loc) · 11.1 KB

Array

The @perfective/common/array package provides functions for the standard JS Array class:

Constructors

  • array<T>(…​elements: T[]): T[] — creates an array from given elements.

  • elements<T>(value: Iterable<T> | ArrayLike<T>): T[] — creates an array from elements of a given Iterable or an ArrayLike value.

  • copy<T>(array: T[]): T[] — creates a shallow copy of a given array.

  • concatenated<T>(arrays: T[][]): T[] — concatenates given arrays in order they are given.

  • concatenated<T>(initial: T[], …​arrays: T[][]): T[] — concatenates given arrays in order they are given.

  • intersection<T>(array1: T[], array2: T[]): T[] — returns an array of unique elements that are included in both given arrays.

  • replicated<T>(value: T, count: number): T[] — creates an array with a given value replicated a given count of times.

  • replicated<T>(count: number): Unary<T, T[]> — creates a callback that replicates the input value a given count of times.

  • reversed<T>(array: T[]): T[] — creates a shallow copy of a given array with elements in reversed order.

  • sorted<T>(array: T[], order?: Compare<T>): T[] — returns a shallow copy of a given array sorted with a given order callback.

  • sorted<T>(order?: Compare<T>): Unary<T[], T[]> — creates a callback to return a shallow copy of the input array sorted with a given order callback.

  • unique<T>(array: T[]): T[] — returns an array with only the first occurrence of each value in a given array.

  • wrapped<T>(value: T | T[]): T[] — returns an array that consists only of a given value. If a given value is an array, returns the original value.

Type guards

  • isArray<T, V = unknown>(value: T[] | V): value is T[] — returns true if a given value is an array. Otherwise, returns false.

  • isNotArray<T, V = unknown>(value: T[] | V): value is V — returns true if a given value is not an array. Otherwise, returns false.

Predicates

  • isEmpty<T>(value: T[]): boolean — returns true if a given array is empty. Otherwise, returns false.

  • isNotEmpty<T>(value: T[]): boolean — returns true if a given array is not empty. Otherwise, returns false.

  • includes<T>(search: T, from?: number): Predicate<T[]> — creates a callback that returns true if a given value is included in the input array (optionally, starting from a given index).

  • includedIn<T>(array: T[], from?: number): Predicate<T> — creates a callback that returns true if the input value is present in a given array (optionally, checking from a given index).

  • every<T>(condition: Predicate<T>): Predicate<T[]> — creates a callback that returns true if all elements of the input array satisfy a given condition.

  • some<T>(condition: Predicate<T>): Predicate<T[]> — creates a callback that returns true if the input array contains at least one element that satisfies a given condition.

Iterators

  • entries<T>(array: T[]): IterableIterator<[number, T]> — returns a new array iterator that contains the key/value pairs for each index of a given array.

  • keys<T>(array: T[]): IterableIterator<number> — returns a new array iterator that contains the keys of each index in the array.

  • values<T>(array: T[]): IterableIterator<T> — returns a new array iterator that iterates the value of each item in a given array.

Filter

Filter<T, S extends T> — a callback that can be passed into the Array.prototype.filter() method.

  • filter<T>(condition: Predicate<T>): Unary<T[], T[]> — creates a callback that returns an array with element the input array that satisfy a given condition.

  • by<T, K extends keyof T>(property: K, condition: Predicate<T[K]>): Filter<T, T> — creates a filter callback that returns true if a given property of the input value satisfies a given condition.

  • isFirstOccurrence<T>(value: T, index: number, array: T[]): boolean — returns true, if a given value has its first occurrence at given index in a given array. Otherwise, returns false.

  • isLastOccurrence<T>(value: T, index: number, array: T[]): boolean — returns true, if a given value has its last occurrence at given index in a given array. Otherwise, returns false.

Map

Map<T, U> — a callback that can be passed into the Array.prototype.map() method.

  • map<T, V>(callback: Unary<T, V>): Unary<T[], V[]> — creates a callback that returns an array of results of a given callback applied to each element of the input array.

Reduce

Reduce<T, V> — a callback that can passed into the Array.prototype.reduce() and Array.prototype.reduceRight() methods.

  • reduce<T, V>(reducer: Reducer<T, V>, initial: V): Unary<T[], V> — creates a callback that reduces the input array with a given reducer callback using a given initial value.

  • reduceTo<T>(reducer: Reducer<T, T>): Unary<T[], T> — creates a callback that reduces the input array with a given reducer callback without an initial value.

  • reduceRight<T, V>(reducer: Reducer<T, V>, initial: V): Unary<T[], V> — creates a callback that reduces the input array with a given reducer callback using a given initial value starting from the end of the array.

  • reduceRightTo<T>(reducer: Reducer<T, T>): Unary<T[], T> — creates a callback that reduces the input array with a given reducer callback without an initial value starting from te end of the array.

  • join<T>(separator: string = ','): Unary<T[], string> — creates a callback that returns a string concatenated from elements of the input array with a given separator.

Sort

Compare<T> = (a: T, b: T) ⇒ number — a function that defines the sort order and can be passed into the Array.prototype.sort() method.

  • sort<T>(order?: Compare<T>): Unary<T[], T[]> — creates a callback that returns the input array sorted in-place using a given order function.

Element

type Element<A> = A extends readonly (infer T)[] ? T : undefined — infers the element type of a given array A.

  • head<T>(array: T[]): T | undefined — returns the first element of a given array.

  • tail<T>(array: T[]): T[] — returns a sub-array of a given array, without the first element.

  • end<T>(array: T[]): T | undefined — returns the last element of a given array.

  • init<T>(array: T[]): T[] — returns a sub-array of a given array, without the last element.

  • element<T>(index: number): Unary<T[], T | undefined> — creates a callback that returns an element at a given index in the array input.

  • find<T>(condition: Predicate<T>): Unary<T[], T | undefined> — creates a callback that returns the first element of the input array that satisfies a given condition; or returns undefined if no elements satisfy the condition.

  • pop<T>(array: T[]): T | undefined — returns the last element of a given array and removes it from the array. Returns undefined if the array is empty.

  • push<T>(…​elements: T[]): Unary<T[], number> — creates a callback that adds given elements to the end of the input array and returns its new length.

  • pushInto<T>(array: T[]): (…​elements: T[]) ⇒ number — creates a callback that adds the input elements to the end of a given array and returns its new length.

  • shift<T>(array: T[]): T | undefined — removes the first element of a given array and it.

  • unshift<T>(…​elements: T[]): Unary<T[], number> — creates a callback that adds given elements to the beginning of the input array and returns its new length.

  • findIndex<T>(condition: Predicate<T>): Unary<T[], number | -1> — creates a callback that returns the index of the first element of the input array that satisfies a given condition; or returns -1 if no elements satisfy the condition.

  • indexOf<T>(search: T, from?: number): Unary<T[], number | -1> — creates a callback that returns the first index of a given value in the input array (optionally, starting from a given index); or returns -1 if the value is not present.

  • lastIndexOf<T>(search: T, from?: number): Unary<T[], number | -1> — creates a callback that returns the last index of a given value in the input array; or returns -1 if the value is not present.

  • first<T>(count: number = 1): Unary<T[], T[]> — creates a callback that returns an array of the first count of the input array elements.

  • last<T>(count: number = 1): Unary<T[], T[]> — creates a callback that returns an array of the last count of the input array elements.

  • append<T>(element: T): Unary<T[], T[]> — creates a callback that returns a shallow copy of the input array with a given element added as the last element.

  • prepend<T>(element: T): Unary<T[], T[]> — creates a callback that returns a shallow copy of the input array with a given element added as the first element.

  • insert<T>(index: number, element: T): Unary<T[], T[]> — creates a callback that returns a shallow copy of the input array with a given element inserted at a given index.

  • insertInto<T>(array: T[], index: number): Unary<T, T[]> — creates a callback that returns a shallow copy of a given array with the input element inserted at a given index.

  • replace<T>(index: number, element: T): Unary<T[], T[]> — creates a callback that returns a shallow copy of the input array with a given element replacing the input array element at a given index.

  • remove<T>(index: number): Unary<T[], T[]> — creates a callback that returns a shallow copy of the input array without an element at a given index.

  • concat<T>(…​items: ConcatArray<T>[]): Unary<T[], T[]> — creates a callback that merges given items to the input array.

  • slice<T>(start?: number, end?: number): Unary<T[], T[]> — creates a callback that returns an array of elements between given start and end (exclusive) indices of the input array.

  • copyWithin<T>(target: number, start: number = 0, end?: number): Unary<T[], T[]> — creates a callback that shallow copies elements within a given start to end range starting from the target index.

  • fill<T>(value: T, start?: number, end?: number): Unary<T[], T[]> — creates a callback that changes all elements of the input array within a given start to end range to a given value.

  • reverse<T>(array: T[]): T[] — reverses a given array (in-place).

  • splice<T>(start: number, deleteCount?: number): Unary<T[], T[]> — creates a callback that removes (in-place) count number of elements of the input array from a given start index and returns the array.

  • spliceWith<T>(start: number, deleteCount: number, …​elements: T[]): Unary<T[], T[]> — creates a callback that replaces (in-place) count number of elements of the input array from a given start index with given elements and returns the array.

  • forEach<T>(procedure: UnaryVoid<T>): UnaryVoid<T[]> — creates a callback that executes a given procedure on every element of the input array.