nope/lib/helpers/arrayMethods.ts

289 lines
6.6 KiB
TypeScript
Raw Normal View History

2020-11-06 08:10:30 +00:00
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2020-11-06 08:53:00
* @modify date 2020-11-06 08:53:01
* @desc [description]
*/
2020-08-25 22:11:26 +00:00
import { rgetattr } from "./objectMethods";
/**
* Sorts a List based on the given property
* Usage :
* 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
*/
2021-08-17 15:52:46 +00:00
export function dynamicSort(_property: string, reverse = false): any {
2020-08-25 22:11:26 +00:00
const _sortOrder = reverse ? -1 : 1;
return function (a, b) {
const _a = rgetattr(a, _property);
const _b = rgetattr(b, _property);
2021-08-17 15:52:46 +00:00
if (typeof _a === "number" && typeof _b === "number") {
2021-12-04 07:25:26 +00:00
const result = _a < _b ? -1 : _a > _b ? 1 : 0;
2020-08-25 22:11:26 +00:00
return result * _sortOrder;
2021-08-17 15:52:46 +00:00
} else if (typeof _a === "number") {
2020-08-25 22:11:26 +00:00
return 1 * _sortOrder;
2021-08-17 15:52:46 +00:00
} else if (typeof _b === "number") {
2020-08-25 22:11:26 +00:00
return -1 * _sortOrder;
2021-08-17 15:52:46 +00:00
} else if (typeof _a === "string" && typeof _b === "string") {
2021-12-04 07:25:26 +00:00
return _a < _b ? -1 : _a > _b ? 1 : 0;
2020-08-25 22:11:26 +00:00
}
2021-08-17 15:52:46 +00:00
return 0;
2020-08-25 22:11:26 +00:00
};
}
/**
* Extracts the Data of the given List
* @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 function extractListElement(list: Array<any>, path: string): Array<any> {
// Define Function to extract the Properties.
function _extract(_property) {
const _ret = rgetattr(_property, path);
/** Returns the Value if it is in the Element */
if (_ret) {
return _ret;
}
}
return list.map(_extract);
}
/**
* Converts a List to Set
*
* @export
* @template T
* @param {Array<T>} list
* @returns {Set<T>}
*/
export function toSet<T>(list: Array<T>): Set<T> {
const _ret = new Set<T>();
for (const item of list) {
_ret.add(item);
}
return _ret;
}
/**
* Extracts the first Element if Possible, which includes the Operand
2021-12-04 07:25:26 +00:00
*
2020-08-25 22:11:26 +00:00
* const a = [{path:'hallo'}, {path:'hallo2'}]
2021-12-04 07:25:26 +00:00
*
2020-08-25 22:11:26 +00:00
* getElement(a, 'hallo2', 'path')
*
* @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)}
*/
2021-12-04 07:25:26 +00:00
export function getElement<T>(
list: Array<T>,
operand: any,
path = ""
): T | null {
2020-08-25 22:11:26 +00:00
/** Iterate through the List an get the requested Element if possible */
for (const _element of list) {
/** Compare the Requested value with the value of the List-Item */
if (operand === rgetattr(_element, path)) {
return _element;
}
}
return null;
}
/**
* Function that will compare two arrays, if they are equal.
* @param a Array Element A
* @param b Array Element B
* @param considerOrder Flag to enable/disable Order checking
*/
2021-12-04 07:25:26 +00:00
export function arraysEqual(
a: Array<any>,
b: Array<any>,
considerOrder = true
): boolean {
2020-08-25 22:11:26 +00:00
if (a === b) return true;
if (a == null || b == null) return false;
if (a.length != b.length) return false;
let _a = a;
let _b = b;
if (!considerOrder) {
_a = a.concat().sort();
_b = b.concat().sort();
}
2021-08-17 15:52:46 +00:00
for (let i = 0; i < _a.length; ++i) {
2020-08-25 22:11:26 +00:00
if (_a[i] !== _b[i]) return false;
}
return true;
}
/**
2021-12-04 07:25:26 +00:00
* Function which will limit the Amount of Element stored in the Array during
2020-08-25 22:11:26 +00:00
* pushing a new Element. If the Maximum is exited the Elementes will be removed
* with the FIFO Principal.
* @param array The considered Array
2021-12-04 07:25:26 +00:00
* @param element The Element which should be added
2020-08-25 22:11:26 +00:00
* @param maxElements The Max. Amount of Elements, which are allowed to store.
*/
2021-12-04 07:25:26 +00:00
export function limitedPush<T>(
array: T[],
element: T,
maxElements: number
): void {
2020-08-25 22:11:26 +00:00
array.push(element);
if (array.length > maxElements) {
array.splice(0, 1);
}
}
/**
2021-12-04 07:25:26 +00:00
* Function to count the Number of Element in an Array. A Dict with the Elements
2020-08-25 22:11:26 +00:00
* as string will be returned.
* @param array The Array
*/
2021-12-04 07:25:26 +00:00
export function countElements<T>(array: Array<T>): Map<T, number> {
const ret: Map<T, number> = new Map();
2020-08-25 22:11:26 +00:00
for (const element of array) {
2021-12-04 07:25:26 +00:00
ret.set(element, (ret.get(element) || 0) + 1);
2020-08-25 22:11:26 +00:00
}
return ret;
}
/**
* Function, which will Flatten the Array.
* @param arrayToFlatten The Array
*/
export function flattenDeep<T>(arrayToFlatten) {
2021-12-04 07:25:26 +00:00
return arrayToFlatten.reduce(
(acc, val) =>
Array.isArray(val) ? acc.concat(flattenDeep(val)) : acc.concat(val),
[]
) as T[];
2020-08-25 22:11:26 +00:00
}
/**
* Function, to Test whether the Element is present in the Array
* @param objToTest The Object to Test
* @param array The array to consider
* @param path The path, under which the element will be found
*/
export function elementInArray<T>(objToTest: T, array: Array<T>, path: string) {
const testData = rgetattr(objToTest, path, false);
for (const [idx, element] of array.entries()) {
if (testData === rgetattr(element, path, true)) {
return idx;
}
}
return -1;
}
/**
* Function to ZIP to Arrays.
2021-12-04 07:25:26 +00:00
* @param arr1
* @param arr2
2020-08-25 22:11:26 +00:00
*/
export function zipArrays<T, K>(arr1: T[], arr2: K[]): Array<[T, K]> {
if (arr1.length !== arr2.length) {
throw Error("Length of the Elements doesnt match!");
}
const res: Array<[T, K]> = arr1.map(function (e, i) {
return [e, arr2[i]];
});
return res;
2021-08-17 15:52:46 +00:00
}
/**
* Helper to determine the average of an array
*
* @author M.Karkowski
* @export
* @param {any[]} arr
* @param {string} path
* @param {number} [defaultValue=0]
* @return {*} {number}
*/
export function avgOfArray(arr: any[], path: string, defaultValue = 0): number {
if (arr.length === 0) {
return defaultValue;
}
2021-12-04 07:25:26 +00:00
const arrOfValues = arr.map((item) => rgetattr(item, path, defaultValue));
2021-08-17 15:52:46 +00:00
const added = arrOfValues.reduce((prev, curr) => prev + curr) as number;
return added / arr.length;
}
2021-12-04 07:25:26 +00:00
export function minOfArray(
arr: any[],
path: string,
defaultValue = 0
): {
min: number;
index: number;
2021-08-17 15:52:46 +00:00
} {
if (arr.length === 0) {
return {
min: defaultValue,
2021-12-04 07:25:26 +00:00
index: -1,
2021-08-17 15:52:46 +00:00
};
}
2021-12-04 07:25:26 +00:00
const arrOfValues = arr.map((item) =>
rgetattr(item, path, defaultValue)
) as number[];
2021-08-17 15:52:46 +00:00
const min = Math.min(...arrOfValues);
return {
min,
2021-12-04 07:25:26 +00:00
index: arrOfValues.indexOf(min),
2021-08-17 15:52:46 +00:00
};
2021-12-04 07:25:26 +00:00
}
2022-01-03 15:27:05 +00:00
export function maxOfArray(
arr: any[],
path: string,
defaultValue = 0
): {
max: number;
index: number;
} {
if (arr.length === 0) {
return {
max: defaultValue,
index: -1,
};
}
const arrOfValues = arr.map((item) =>
rgetattr(item, path, defaultValue)
) as number[];
const max = Math.max(...arrOfValues);
return {
max: max,
index: arrOfValues.indexOf(max),
};
2022-01-03 15:46:36 +00:00
}