2023-01-26 18:12:20 +00:00
|
|
|
export const groupBy = (array: any[], key: string) => {
|
|
|
|
const innerKey = key.split("."); // split the key by dot
|
|
|
|
return array.reduce((result, currentValue) => {
|
|
|
|
const key = innerKey.reduce((obj, i) => obj?.[i], currentValue) ?? "None"; // get the value of the inner key
|
|
|
|
(result[key] = result[key] || []).push(currentValue);
|
|
|
|
return result;
|
|
|
|
}, {});
|
|
|
|
};
|
|
|
|
|
2023-11-08 15:01:46 +00:00
|
|
|
export const orderArrayBy = (orgArray: any[], key: string, ordering: "ascending" | "descending" = "ascending") => {
|
2023-07-13 11:21:08 +00:00
|
|
|
if (!orgArray || !Array.isArray(orgArray) || orgArray.length === 0) return [];
|
2023-05-25 07:08:15 +00:00
|
|
|
|
2023-07-13 11:21:08 +00:00
|
|
|
const array = [...orgArray];
|
2023-05-10 20:45:39 +00:00
|
|
|
|
2023-04-13 13:39:55 +00:00
|
|
|
if (key[0] === "-") {
|
|
|
|
ordering = "descending";
|
|
|
|
key = key.slice(1);
|
|
|
|
}
|
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
const innerKey = key.split("."); // split the key by dot
|
2023-04-13 13:39:55 +00:00
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
return array.sort((a, b) => {
|
|
|
|
const keyA = innerKey.reduce((obj, i) => obj[i], a); // get the value of the inner key
|
|
|
|
const keyB = innerKey.reduce((obj, i) => obj[i], b); // get the value of the inner key
|
|
|
|
if (keyA < keyB) {
|
|
|
|
return ordering === "ascending" ? -1 : 1;
|
|
|
|
}
|
|
|
|
if (keyA > keyB) {
|
|
|
|
return ordering === "ascending" ? 1 : -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
};
|
2023-04-21 19:29:57 +00:00
|
|
|
|
|
|
|
export const checkDuplicates = (array: any[]) => new Set(array).size !== array.length;
|
2023-05-11 12:08:46 +00:00
|
|
|
|
2023-09-21 10:33:33 +00:00
|
|
|
export const findStringWithMostCharacters = (strings: string[]): string => {
|
|
|
|
if (!strings || strings.length === 0) return "";
|
|
|
|
|
|
|
|
return strings.reduce((longestString, currentString) =>
|
2023-05-11 12:08:46 +00:00
|
|
|
currentString.length > longestString.length ? currentString : longestString
|
|
|
|
);
|
2023-09-21 10:33:33 +00:00
|
|
|
};
|
2023-07-04 17:43:07 +00:00
|
|
|
|
|
|
|
export const checkIfArraysHaveSameElements = (arr1: any[] | null, arr2: any[] | null): boolean => {
|
|
|
|
if (!arr1 || !arr2) return false;
|
|
|
|
if (!Array.isArray(arr1) || !Array.isArray(arr2)) return false;
|
|
|
|
if (arr1.length === 0 && arr2.length === 0) return true;
|
|
|
|
|
|
|
|
return arr1.length === arr2.length && arr1.every((e) => arr2.includes(e));
|
|
|
|
};
|
2023-11-08 15:01:46 +00:00
|
|
|
|
|
|
|
type GroupedItems<T> = { [key: string]: T[] };
|
|
|
|
|
|
|
|
export const groupByField = <T>(array: T[], field: keyof T): GroupedItems<T> =>
|
|
|
|
array.reduce((grouped: GroupedItems<T>, item: T) => {
|
|
|
|
const key = String(item[field]);
|
|
|
|
grouped[key] = (grouped[key] || []).concat(item);
|
|
|
|
return grouped;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
export const sortByField = (array: any[], field: string): any[] =>
|
|
|
|
array.sort((a, b) => (a[field] < b[field] ? -1 : a[field] > b[field] ? 1 : 0));
|
|
|
|
|
|
|
|
export const orderGroupedDataByField = <T>(groupedData: GroupedItems<T>, orderBy: keyof T): GroupedItems<T> => {
|
|
|
|
for (const key in groupedData) {
|
|
|
|
if (groupedData.hasOwnProperty(key)) {
|
|
|
|
groupedData[key] = groupedData[key].sort((a, b) => {
|
|
|
|
if (a[orderBy] < b[orderBy]) return -1;
|
|
|
|
if (a[orderBy] > b[orderBy]) return 1;
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return groupedData;
|
|
|
|
};
|