The @perfective/common/array package provides functions for the standard JS
Array class:
-
array<T>(…elements: T[]): T[]— creates an array from givenelements. -
elements<T>(value: Iterable<T> | ArrayLike<T>): T[]— creates an array from elements of a givenIterableor anArrayLikevalue. -
copy<T>(array: T[]): T[]— creates a shallow copy of a givenarray. -
concatenated<T>(arrays: T[][]): T[]— concatenates givenarraysin 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 givenvaluereplicated a givencountof times. -
replicated<T>(count: number): Unary<T, T[]>— creates a callback that replicates the inputvaluea givencountof times. -
reversed<T>(array: T[]): T[]— creates a shallow copy of a givenarraywith elements in reversed order. -
sorted<T>(array: T[], order?: Compare<T>): T[]— returns a shallow copy of a givenarraysorted with a givenordercallback. -
sorted<T>(order?: Compare<T>): Unary<T[], T[]>— creates a callback to return a shallow copy of the input array sorted with a givenordercallback. -
unique<T>(array: T[]): T[]— returns an array with only the first occurrence of each value in a givenarray. -
wrapped<T>(value: T | T[]): T[]— returns an array that consists only of a givenvalue. If a givenvalueis an array, returns the originalvalue.
-
isArray<T, V = unknown>(value: T[] | V): value is T[]— returnstrueif a givenvalueis an array. Otherwise, returnsfalse. -
isNotArray<T, V = unknown>(value: T[] | V): value is V— returnstrueif a givenvalueis not an array. Otherwise, returnsfalse.
-
isEmpty<T>(value: T[]): boolean— returnstrueif a givenarrayis empty. Otherwise, returnsfalse. -
isNotEmpty<T>(value: T[]): boolean— returnstrueif a givenarrayis not empty. Otherwise, returnsfalse. -
includes<T>(search: T, from?: number): Predicate<T[]>— creates a callback that returnstrueif a givenvalueis included in the input array (optionally, startingfroma given index). -
includedIn<T>(array: T[], from?: number): Predicate<T>— creates a callback that returnstrueif the input value is present in a givenarray(optionally, checkingfroma given index). -
every<T>(condition: Predicate<T>): Predicate<T[]>— creates a callback that returnstrueif all elements of the inputarraysatisfy a givencondition. -
some<T>(condition: Predicate<T>): Predicate<T[]>— creates a callback that returnstrueif the input array contains at least one element that satisfies a givencondition.
-
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 givenarray.
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 inputarraythat satisfy a givencondition. -
by<T, K extends keyof T>(property: K, condition: Predicate<T[K]>): Filter<T, T>— creates a filter callback that returnstrueif a givenpropertyof the inputvaluesatisfies a givencondition. -
isFirstOccurrence<T>(value: T, index: number, array: T[]): boolean— returnstrue, if a givenvaluehas its first occurrence at givenindexin a givenarray. Otherwise, returnsfalse. -
isLastOccurrence<T>(value: T, index: number, array: T[]): boolean— returnstrue, if a givenvaluehas its last occurrence at givenindexin a givenarray. Otherwise, returnsfalse.
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 givencallbackapplied to each element of the input array.
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 givenreducercallback using a giveninitialvalue. -
reduceTo<T>(reducer: Reducer<T, T>): Unary<T[], T>— creates a callback that reduces the input array with a givenreducercallback 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 givenreducercallback using a giveninitialvalue 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 givenreducercallback 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 inputarraywith a givenseparator.
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 givenorderfunction.
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 givenarray. -
tail<T>(array: T[]): T[]— returns a sub-array of a givenarray, without the first element. -
end<T>(array: T[]): T | undefined— returns the last element of a givenarray. -
init<T>(array: T[]): T[]— returns a sub-array of a givenarray, without the last element. -
element<T>(index: number): Unary<T[], T | undefined>— creates a callback that returns an element at a givenindexin thearrayinput. -
find<T>(condition: Predicate<T>): Unary<T[], T | undefined>— creates a callback that returns the first element of the inputarraythat satisfies a givencondition; or returnsundefinedif no elements satisfy thecondition. -
pop<T>(array: T[]): T | undefined— returns the last element of a givenarrayand removes it from thearray. Returnsundefinedif the array is empty. -
push<T>(…elements: T[]): Unary<T[], number>— creates a callback that adds givenelementsto the end of the input array and returns its new length. -
pushInto<T>(array: T[]): (…elements: T[]) ⇒ number— creates a callback that adds the inputelementsto the end of a givenarrayand returns its new length. -
shift<T>(array: T[]): T | undefined— removes the first element of a givenarrayand it. -
unshift<T>(…elements: T[]): Unary<T[], number>— creates a callback that adds givenelementsto 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 inputarraythat satisfies a givencondition; or returns-1if no elements satisfy thecondition. -
indexOf<T>(search: T, from?: number): Unary<T[], number | -1>— creates a callback that returns the first index of a givenvaluein the input array (optionally, startingfroma given index); or returns-1if thevalueis not present. -
lastIndexOf<T>(search: T, from?: number): Unary<T[], number | -1>— creates a callback that returns the last index of a givenvaluein the input array; or returns-1if thevalueis not present. -
first<T>(count: number = 1): Unary<T[], T[]>— creates a callback that returns an array of the firstcountof the inputarrayelements. -
last<T>(count: number = 1): Unary<T[], T[]>— creates a callback that returns an array of the lastcountof the inputarrayelements. -
append<T>(element: T): Unary<T[], T[]>— creates a callback that returns a shallow copy of the inputarraywith a givenelementadded as the last element. -
prepend<T>(element: T): Unary<T[], T[]>— creates a callback that returns a shallow copy of the inputarraywith a givenelementadded as the first element. -
insert<T>(index: number, element: T): Unary<T[], T[]>— creates a callback that returns a shallow copy of the inputarraywith a givenelementinserted at a givenindex. -
insertInto<T>(array: T[], index: number): Unary<T, T[]>— creates a callback that returns a shallow copy of a givenarraywith the inputelementinserted at a givenindex. -
replace<T>(index: number, element: T): Unary<T[], T[]>— creates a callback that returns a shallow copy of the inputarraywith a givenelementreplacing the input array element at a givenindex. -
remove<T>(index: number): Unary<T[], T[]>— creates a callback that returns a shallow copy of the inputarraywithout an element at a givenindex. -
concat<T>(…items: ConcatArray<T>[]): Unary<T[], T[]>— creates a callback that merges givenitemsto the inputarray. -
slice<T>(start?: number, end?: number): Unary<T[], T[]>— creates a callback that returns an array of elements between givenstartandend(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 givenstarttoendrange starting from thetargetindex. -
fill<T>(value: T, start?: number, end?: number): Unary<T[], T[]>— creates a callback that changes all elements of the inputarraywithin a givenstarttoendrange to a givenvalue. -
reverse<T>(array: T[]): T[]— reverses a givenarray(in-place). -
splice<T>(start: number, deleteCount?: number): Unary<T[], T[]>— creates a callback that removes (in-place)countnumber of elements of the input array from a givenstartindex and returns the array. -
spliceWith<T>(start: number, deleteCount: number, …elements: T[]): Unary<T[], T[]>— creates a callback that replaces (in-place)countnumber of elements of the input array from a givenstartindex with givenelementsand returns the array. -
forEach<T>(procedure: UnaryVoid<T>): UnaryVoid<T[]>— creates a callback that executes a givenprocedureon every element of the inputarray.