@sap-cloud-sdk/util
Version:
SAP Cloud SDK for JavaScript general utilities
84 lines (83 loc) • 4.1 kB
TypeScript
/**
* Flatten a two dimensional array into a one dimensional array.
* @param arr - The array to be flattened.
* @returns A one dimensional array.
*/
export declare function flat<T>(arr: T[][]): T[];
/**
* Remove all duplicates from an array.
* @param arr - Array that might contain duplicates.
* @returns Array of unique items.
*/
export declare function unique<T>(arr: T[]): T[];
/**
* Get the last item from an array. Returns `undefined`, if the array is empty.
* @param arr - Array to get the last item of.
* @returns Last item of the array or `undefined`, if the array was empty.
*/
export declare function last<T>(arr: T[]): T | undefined;
/**
* Get the first item from an array. Returns `undefined`, if the array is empty.
* @param arr - Array to get the first item of.
* @returns Fist item of the array or `undefined`, if the array was empty.
*/
export declare function first<T>(arr: T[]): T | undefined;
/**
* Split the given array in chunks.
* @param arr - Array to be split into chunks.
* @param chunkSize - Size of the chunks.
* @returns Two dimensional array with arrays of length chunkSize. The last subarray could be shorter.
*/
export declare function splitInChunks<T>(arr: T[], chunkSize: number): T[][];
/**
* We want to provide methods which accept a variable single number of elements and arrays.
* The overloaded signature to achieve this is:
* ```
* function doSomething(array: T[])
* function doSomething(...varArgs: T[])
* function doSomething(first: undefined | T | T[], ...rest: T[]) {
* //implementation
* }
* ```
* This wrapper methods makes it easy build an array from the input.
* @param firstOrArray - Either an array, the first element of the var args or `undefined`, if no argument was given.
* @param rest - Second to last element, if var args were used, empty array, if the first argument is an array.
* @returns Array from the input or empty array if no input was given.
*/
export declare function transformVariadicArgumentToArray<T>(firstOrArray: undefined | T | T[], rest: T[]): T[];
/**
* Flattens a array: [1,[2,[3,4]],5] will become [1,2,3,4,5].
* Non primitive values are copied by reference.
* @param input - Array to be flattened.
* @returns The flattened array.
*/
export declare const flatten: (input: any[]) => any[];
/**
* Merge two arrays by alternately adding inserting values from both arrays, starting from the left.
* @example `zip([1, 2], [3, 4, 5, 6])` results in `[1, 3, 2, 4, 5, 6]`.
* @param left - Array to start alternately merging from.
* @param right - Second array to merge.
* @returns Zipped array.
*/
export declare function zip<T>(left: T[], right: T[]): T[];
/**
* Split an array into two based on a condition.
* @param arr - Array to partition.
* @param condition - Function to determine to where to put each item.
* @returns A two dimensional array containing two arrays, where the first one includes all items where the given condition was met and the second one includes all items where it was not met.
*/
export declare function partition<T>(arr: T[], condition: (item: T) => boolean): [T[], T[]];
/**
* Filter an array by removing duplicates and keeping the left most occurrence. By default this compares by identity.
* @param arr - Array to remove duplicates from.
* @param comparator - Optional comparator function, indicating whether two items are equal and therefore handled as duplicates. Defaults to identity.
* @returns A filtered array containing no duplicates.
*/
export declare function filterDuplicates<T>(arr: T[], comparator?: (left: T, right: T) => boolean): T[];
/**
* Filter an array by removing duplicates and keeping the right most occurrence. By default this compares by identity.
* @param arr - Array to remove duplicates from.
* @param comparator - Optional comparator function, indicating whether two items are equal and therefore handled as duplicates. Defaults to identity.
* @returns A filtered array containing no duplicates.
*/
export declare function filterDuplicatesRight<T>(arr: T[], comparator?: (left: T, right: T) => boolean): T[];