nope-js-browser
Version:
NoPE Runtime for the Browser. For nodejs please use nope-js-node
261 lines (260 loc) • 7.65 kB
TypeScript
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
*/
/**
* Sorts a List based on the given property
* Usage :
* ```javascript
* a = [{a : 1, b: 2}, {a : 2, b: 1}];
* b = a.sort(dynamicSort('b'))
* b => [{a : 2, b: 1}, {a : 1, b: 2}]
* ```
* @export
* @param {string} _property Property Name / Path to Sort the List
* @returns
*/
export declare function dynamicSort(_property: string, reverse?: boolean): any;
/**
* Extracts the Data of the given List
*
* # Example:
*
* ```javascript
* const list = [{"id":1, "data":"a"},{"id":2, "data":"b"}]
* const res = extractListElement(list, "data") // => ["a","b"]
* ```
*
* @export
* @param {Array<any>} list List, where data should be extracted
* @param {string} path path pointing to the Data in the List
* @returns {Array<any>} List only containing the Elements.
*/
export declare function extractListElement(list: Array<any>, path: string): Array<any>;
/**
* Converts a List to Set.
*
* @export
* @template T
* @param {Array<T>} list The list as input
* @returns {Set<T>} The Set
*/
export declare function toSet<T>(list: Array<T>): Set<T>;
/**
* Extracts the first Element if Possible, which includes the Operand
*
* # Example
*
* ```javascript
* const a = [{path:'hallo'}, {path:'hallo2'}]
* const res = getElement(a, 'hallo2', 'path') // => {path:'hallo2'}
* ```
*
* @export
* @template T
* @param {Array<T>} list The list which is considered
* @param {*} operand The Element which should looked for
* @param {string} [path=''] The where the Element should be found
* @returns {(T | null)}
*/
export declare function getElement<T>(list: Array<T>, operand: any, path?: string): T | null;
/**
* Function that will compare two arrays, if they are equal.
*
* # Example:
*
* ```javascript
* const a = [1,2,3,4,5]
* arraysEqual(a, [1,2,3,4]) // => false;
* arraysEqual(a, [1,2,3,4,5]) // => true;
* arraysEqual(a, [1,2,3,5,4], false) // => true;
* ```
*
* @param a Array Element A
* @param b Array Element B
* @param considerOrder Flag to enable/disable Order checking
*/
export declare function arraysEqual(a: Array<any>, b: Array<any>, considerOrder?: boolean): boolean;
/**
* Function which will limit the Amount of Element stored in the Array during
* pushing a new Element. If the Maximum is exited the Elementes will be removed
* with the FIFO Principal.
*
* # Example 1:
*
* In this example the limit will be reached.
*
* ```javascript
* const a = [1,2,3,4,5]
* limitedPush(a, 6,5) // => [2,3,4,5,6];
* ```
* # Example 2:
*
* The limit wont be excided
*
* ```javascript
* const a = [1,2,3,4,5]
* limitedPush(a, 6, 10) // => [1,2,3,4,5,6];
* ```
* @param array The considered Array
* @param element The Element which should be added
* @param maxElements The Max. Amount of Elements, which are allowed to store.
*/
export declare function limitedPush<T>(array: T[], element: T, maxElements: number): void;
/**
* Function to count the Number of Element in an Array. A Dict with the Elements
* as string will be returned.
*
* # Example:
*
* ```javascript
* const a = [1,2,3,4,5,5,5]
* countElements(a) // => Map<{1:1, 2:1,3:1,4:1,5:3}>;
* ```
* @param array The Array
*/
export declare function countElements<T>(array: Array<T>): Map<T, number>;
/**
* Function, which will Flatten the Array.
*
* # Example:
*
* ```javascript
* const a = [1,[2,[3,[4,[5]]]]]
* flattenDeep(a) // => [1,2,3,4,5]
* ```
* @param arrayToFlatten The Array
*/
export declare function flattenDeep<T>(arrayToFlatten: any): T[];
/**
* Function, to Test whether the Element is present in the Array
*
* # Example 1:
*
* ```javascript
* const list = [{"id":1, "data":"a"},{"id":2, "data":"b"},{"id":3, "data":"c"}]
* elementInArray("b", list, "data") // => true
* elementInArray(5, list, "data") // => false
* elementInArray("b", list, "property_not_in_data") // => false
* ```
*
* # Example 2:
*
* ```javascript
* const list = [{"id":1, "data":["a"]},{"id":2, "data":["b"]},{"id":3, "data":["c"]}]
* elementInArray("b", list, "data/0") // => true
* elementInArray("d", list, "data/0") // => false
* elementInArray("b", list, "data") // => false <- Path doesnt contain 'b'
* elementInArray("b", list, "data/1") // => false <- Path wrong, no elements there
* ```
* @param objToTest The Object to Test
* @param array The array to consider
* @param path The path, under which the element will be found
*/
export declare function elementInArray<T>(objToTest: T, array: Array<T>, path: string): number;
/**
* Function to ZIP to Arrays.
*
* # Example 1:
*
* ```javascript
* const a = [1,2,3,4]
* const b = ["a","b","c","d"]
* zipArrays(a,b) // => [(1,"a"), (2, "b"), ...]
* ```
* @param arr1
* @param arr2
*/
export declare function zipArrays<T, K>(arr1: T[], arr2: K[]): Array<[T, K]>;
/**
* Helper to determine the average of an array
*
* # Example 1:
*
* ```javascript
* const a = [1,2,3,4]
* // default behavior:
* avgOfArray(a,"") // => 2.5
* // if no data present at the path the default value is used:
* avgOfArray(a,"a",1) // => 1
* ```
*
* # Example 2:
*
* ```javascript
* const a = [{value:1},{value:2},{value:3},{value:4}]
* // default behavior:
* avgOfArray(a,"value") // => 2.5
* // if no data present at the path the default value is used:
* avgOfArray(a,"a",1) // => 1
* ```
* @author M.Karkowski
* @export
* @param {any[]} arr The array
* @param {string} path The path to the data.
* @param {number} [defaultValue=0] if no data present at the path the default value is used.
* @return {*} {number}
*/
export declare function avgOfArray(arr: any[], path: string, defaultValue?: number): number;
/**
* Helper to determine the minimum of an array
*
* # Example 1:
*
* ```javascript
* const a = [1,2,3,4]
* // default behavior:
* minOfArray(a,"") // => 1
* // if no data present at the path the default value is used:
* minOfArray(a,"a",1) // => 1
* ```
*
* # Example 2:
*
* ```javascript
* const a = [{value:1},{value:2},{value:3},{value:4}]
* // default behavior:
* minOfArray(a,"value") // => 1
* // if no data present at the path the default value is used:
* minOfArray(a,"a",1) // => 1
* ```
* @param {any[]} arr The array
* @param {string} path The path to the data.
* @param {number} [defaultValue=0] if no data present at the path the default value is used.
* @returns
*/
export declare function minOfArray<T>(arr: T[], path: keyof T | string, defaultValue?: number): {
min: number;
index: number;
};
/**
* Helper to determine the maximum of an array
*
* # Example 1:
*
* ```javascript
* const a = [1,2,3,4]
* // default behavior:
* maxOfArray(a,"") // => 4
* // if no data present at the path the default value is used:
* maxOfArray(a,"a",1) // => 1
* ```
*
* # Example 2:
*
* ```javascript
* const a = [{value:1},{value:2},{value:3},{value:4}]
* // default behavior:
* maxOfArray(a,"value") // => 4
* // if no data present at the path the default value is used:
* maxOfArray(a,"a",1) // => 1
* ```
* @param {any[]} arr The array
* @param {string} path The path to the data.
* @param {number} [defaultValue=0] if no data present at the path the default value is used.
* @returns
*/
export declare function maxOfArray(arr: any[], path: string, defaultValue?: number): {
max: number;
index: number;
};