@eclipse-scout/core
Version:
Eclipse Scout runtime
221 lines • 10.7 kB
TypeScript
export declare const arrays: {
/**
* Ensures the given parameter is an array.
*/
ensure<T>(array: T[] | T): T[];
/**
* Creates an array with the given length and initializes each value with the given initValue.
*/
init<T>(length: number, initValue: T): T[];
/**
* Removes the first occurrence of the specified element from the array.
* If the array does not contain the element, it stays unchanged.
*
* @returns true if an element was removed
*/
remove<T>(arr: T[], element: T): boolean;
/**
* Removes the first array element that matches the given predicate.
* If no element matches the given predicate, the array stays unchanged.
*
* @returns true if an element was removed
*/
removeByPredicate<T>(arr: T[], predicate: (elem: T, index: number, arr: T[]) => boolean, thisArg?: any): boolean;
/**
* Removes every given element from the array
*
* @returns true if the array contained at least one of the specified elements
*/
removeAll<T>(arr: T[], elements: T[]): boolean;
/**
* @returns the index of the replaced element
*/
replace<T>(arr: T[], element: T, replacement: T): number;
/**
* Inserts the given element at the specified index.
* <p>
* This function uses {@link insertAll} which relies on Array.prototype.splice(). Check its js-doc for details.
*/
insert<T>(arr: T[], element: T, index: number): void;
/**
* Inserts all elements of the `source` at the specified index into the `target`.
*
* If many elements are passed, they are inserted as chunks of 100'000 to prevent maximum call stack errors.
* (Chrome throws a Maximum call stack size exceeded when using spread operator at around 125'000 items, Firefox can handle around 500'000).
*
* This function is based on Array.prototype.splice().
* Thus, if the 'index' is greater than the length of the array, 'elements' will be added to the end of the array 'arr'.
* This may cause unexpected behavior on accessing arr[index] after insertion.
*
* The caller must ensure the size of the array.
*/
insertAll<T>(target: T[], source: T | T[], index: number): void;
/**
* Inserts the given element into the array according to the sort order indicated by the given comparison function.
*
* All arguments are mandatory.
*/
insertSorted<T>(arr: T[], element: T, compareFunc: (a: T, b: T) => number): void;
/**
* Inserts to given element into the array directly BEFORE the first array element that matches the given predicate.
* If no such element can be found, the new element is inserted at the BEGINNING of the array.
*
* @param thisArg optional "this" binding for predicate function
*/
insertBefore<T>(arr: T[], elementToInsert: T, predicate: (elem: T, index: number, arr: T[]) => boolean, thisArg?: any): void;
/**
* Inserts to given element into the array directly AFTER the first array element that matches the given predicate.
* If no such element can be found, the new element is inserted at the END of the array.
*/
insertAfter<T>(arr: T[], elementToInsert: T, predicate: (elem: T, index: number, arr: T[]) => boolean): void;
/**
* Moves the element at fromIndex to toIndex.
*
* This function uses {@link insert} which relies on Array.prototype.splice(). Check its js-doc for details.
*/
move<T>(arr: T[], fromIndex: number, toIndex: number): void;
/**
* Moves the given element before the sibling and returns the array with the new order.
*/
moveBefore<T>(arr: T[], elementToMove: T, sibling: T): T[];
/**
* Moves the given element after the sibling and returns the array with the new order.
*/
moveAfter<T>(arr: T[], elementToMove: T, sibling: T): T[];
/**
* Moves the given element to the position returns the array with the new order.
*/
moveTo<T>(arr: T[], elementToMove: T, position: number): T[];
contains<T>(haystack: T[], needle: T): boolean;
containsAny<T>(haystack: T[] | T, needles: T[] | T): boolean;
containsAll<T>(haystack: T[] | T, needles: T[] | T): boolean;
first<T>(arr: T[]): T;
last<T>(arr: T[]): T;
/**
* @returns true if the given argument is an array and has a length > 0, false in any other case.
*/
hasElements<T>(arr: T[] | T): boolean;
/**
* @returns true if the given argument is not an array or the length of the array is 0, false in any other case.
*/
empty<T>(arr: T[] | T): boolean;
/**
* @returns the size of the array, or 0 if the argument is not an array
*/
length<T>(arr: T[] | T): number;
/**
* Pushes all elements of `source` to `target`.
*
* The implementation uses a loop to push the elements rather than `Array.apply` or the spread operator so it can handle very large arrays
* (Chrome throws a Maximum call stack size exceeded when using spread operator at around 125'000 items, Firefox can handle around 500'000).
*
* In terms of performance, using a loop is about the same as with the spread operator.
*/
pushAll<T>(target: T[], source: T | T[]): void;
/**
* Merges the two given arrays and removes duplicate entries in O(n).
* If the arrays contain objects instead of primitives, it uses their id to check for equality.
*/
union<T extends number | string | {
id: string;
}>(array1: T[], array2: T[]): T[];
equalsIgnoreOrder(arr: any[], arr2: any[]): boolean;
equals(arr: ArrayLike<any>, arr2: ArrayLike<any>): boolean;
greater(arr: [], arr2: []): boolean;
eachSibling<T>(arr: ArrayLike<T>, element: T, func: (elem: T, index: number) => void): void;
/**
* Alternative implementation of Array.findIndex(callback [, thisArg]), which is supported by most browsers.
* See Array.findIndex for a detailed description.
*
* @param optional "this" binding for predicate function
*/
findIndex<T>(arr: ArrayLike<T>, predicate: (elem: T, index: number, arr: T[]) => boolean, thisArg?: any): number;
/**
*
* @param thisArg optional "this" binding for predicate function
*/
find<T>(arr: ArrayLike<T>, predicate: (elem: T, index: number, arr: T[]) => boolean, thisArg?: any): T;
findFrom<T>(arr: ArrayLike<T>, startIndex: number, predicate: (elem: T, index: number) => boolean, reverse?: boolean): T;
findIndexFrom<T>(arr: ArrayLike<T>, startIndex: number, predicate: (elem: T, index: number) => boolean, reverse?: boolean): number;
findFromForward<T>(arr: ArrayLike<T>, startIndex: number, predicate: (elem: T, index: number) => boolean): T;
findIndexFromForward<T>(arr: ArrayLike<T>, startIndex: number, predicate: (elem: T, index: number) => boolean): number;
findFromReverse<T>(arr: ArrayLike<T>, startIndex: number, predicate: (elem: T, index: number) => boolean): T;
findIndexFromReverse<T>(arr: ArrayLike<T>, startIndex: number, predicate: (elem: T, index: number) => boolean): number;
/**
* Pushes all elements to the given array that are not null or undefined.
*/
pushIfDefined<T>(arr: T[], ...elements: T[]): void;
/**
* Pushes the given element if it does not already exist in the array and the element is truthy. Thus, the array is like a Set where every element
* can only be added once to the collection. Note: the comparison is done with the === operator.
*/
pushSet<T>(arr: T[], element: T): void;
/**
* Creates a string containing all elements in the array separated by the given delimiter.
* @param encodeHtml true to encode the elements, false if not. Default is false
*/
format(arr: ArrayLike<string>, delimiter?: string, encodeHtml?: boolean): string;
formatEncoded(arr: ArrayLike<string>, delimiter?: string): string;
max(arr: number[]): number;
min(arr: number[]): number;
/**
* @returns all elements of the first array which are not in the second array
*/
diff<T>(arr1: T[], arr2: T[]): T[];
flatMap<T, R>(arr: T[] | T, func?: (T: any) => R | R[]): R[];
/**
* Returns a flat array of all elements and their recursive child elements.
*
* @param arr The top-level list of all elements
* @param childrenAccessor Function than extracts a list of child elements from a given element. Used to traverse the object structure.
*/
flattenRec<T>(arr: T[], childrenAccessor: (T: any) => T[]): T[];
/**
* Replacement for indexOf() that works for arrays of jQuery objects (compares DOM nodes).
*
* @returns the first index of `$element` in `arr` or -1 if the element could not be found.
*/
$indexOf(arr: JQuery[], $element: JQuery): number;
/**
* Replacement for remove() that works for arrays of jQuery objects (compares DOM nodes).
*
* @returns true if the element was removed, false otherwise.
*/
$remove(arr: JQuery[], $element: JQuery): boolean;
randomElement<T>(array: T[]): T;
/**
* Converts the given array to a map. For each element, key and value is determined by the given functions.
* If no function is provided, the element itself is used.
*
* @param array array of elements
* @param keyMapper function that maps each element to the target key
* @param valueExtractor function that maps each element to the target value
*/
toMap<T>(array: T[], keyMapper?: (el: T) => PropertyKey, valueMapper?: (el: T) => any): any;
/**
* If the argument is an empty array, null is returned. Otherwise, the argument is returned unchanged.
*/
nullIfEmpty<T>(array: T[]): T[];
/**
* Clears the content of an array <i>in-place</i>. All elements are removed from the array and the
* length will be set to 0. If the given argument is not an array, nothing happens.
*
* This is a more readable version of `array.splice(0, a.length)`.
*
* The return value is an array of all deleted elements (never null).
*/
clear<T>(array: T[]): T[];
/**
* Swaps the two elements in the array.
* Does nothing if array is null or undefined or one of the element is not part of the array.
*/
swap<T>(array: T[], element1: T, element2: T): void;
/**
* Sorts the given array _in place_ according to the order of the template array.
* Does nothing, if the array is null or undefined.
*
* @returns the given array which is now in order.
*/
sortBy<T>(array: T[], template: T[], direction?: "asc" | "desc"): T[];
};
//# sourceMappingURL=arrays.d.ts.map