froebel
Version:
TypeScript utility library
60 lines (59 loc) • 3.27 kB
TypeScript
declare class SortedArrayImpl<T> {
#private;
constructor(compare: Cmp<T>, ...values: T[]);
static from<T>(compare: Cmp<T>, source: Iterable<T>): SortedArrayImpl<T>;
add(...values: T[]): void;
delete: (...indices: number[]) => T[];
clear(): void;
every: (predicate: (value: T, index: number, array: SortedArray<T>) => unknown, thisArg?: any) => boolean;
filter: (predicate: (value: T, index: number, array: SortedArray<T>) => unknown, thisArg?: any) => SortedArray<T>;
find: (predicate: (value: T, index: number, obj: SortedArray<T>) => unknown, thisArg?: any) => T | undefined;
findIndex: (predicate: (value: T, index: number, obj: SortedArray<T>) => unknown, thisArg?: any) => number;
forEach: (callbackfn: (value: T, index: number, array: SortedArray<T>) => void, thisArg?: any) => void;
includes: (searchElement: T, fromIndex?: number | undefined) => boolean;
indexOf: (searchElement: T, fromIndex?: number | undefined) => number;
join: (separator?: string | undefined) => string;
lastIndexOf: (searchElement: T, fromIndex?: number | undefined) => number;
map: (callbackfn: (value: T, index: number, array: SortedArray<T>) => unknown, thisArg?: any) => unknown[];
pop: () => T | undefined;
reduce: (callbackfn: (previousValue: unknown, currentValue: T, currentIndex: number, array: SortedArray<T>) => unknown, initialValue: unknown) => unknown;
reduceRight: (callbackfn: (previousValue: unknown, currentValue: T, currentIndex: number, array: SortedArray<T>) => unknown, initialValue: unknown) => unknown;
shift: () => T | undefined;
slice: (start?: number | undefined, end?: number | undefined) => SortedArray<T>;
some: (predicate: (value: T, index: number, array: SortedArray<T>) => unknown, thisArg?: any) => boolean;
at: any;
get length(): number;
[Symbol.iterator](): IterableIterator<T>;
}
/**
* Sorted array. Behaves much like a regular array but its elements remain
* sorted using the `compare` function supplied in the constructor.
*
* Contains most of the methods defined on regular JavaScript arrays as long as
* they don't modify the array's content in place.
*
* New elements are added using the `add(...values)` method.
*
* Elements can still be accessed using bracket notation as in plain JavaScript
* arrays but can't be assigned to using bracket notation (as that could change
* the element's sort position).
*
* Elements can be removed using the `delete(...indices)` method, which returns
* an array containing the deleted values.
* Deleting an element using `delete sorted[index]` will also work, but results
* in a TypeScript error because element access is marked readonly.
*
* Array methods that pass a reference of the array to a callback (e.g. `map`,
* `reduce`, `find`) will pass a reference to the SortedArray instance instead.
*
* The `filter` and `slice` methods will return SortedArray instances instead of
* plain arrays.
*/
export interface SortedArray<T> extends SortedArrayImpl<T> {
readonly [K: number]: T;
}
declare const _default: (new <T>(compare: Cmp<T>, ...value: T[]) => SortedArray<T>) & {
from: typeof SortedArrayImpl.from;
};
export default _default;
declare type Cmp<T> = (a: T, b: T) => number;