2020-08-06 19:17:38 +00:00
|
|
|
export const isLowerCase = (str: string): boolean => str.toLowerCase() === str;
|
|
|
|
export const isUpperCase = (str: string): boolean => str.toUpperCase() === str;
|
|
|
|
|
2020-07-29 13:42:51 +00:00
|
|
|
export const mapList = arr => {
|
|
|
|
return arr.reduce((acm, val) => {
|
|
|
|
acm[val] = "";
|
|
|
|
return acm;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
2020-08-07 14:01:57 +00:00
|
|
|
export function mapListTolower(arr: string[]): {} {
|
|
|
|
return arr.reduce((acm, val) => {
|
|
|
|
acm[val.toLowerCase()] = "";
|
|
|
|
return acm;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
2020-07-29 13:42:51 +00:00
|
|
|
// Sort: treat numbers as number, strings as case-insensitive
|
|
|
|
export const sortElements = (arr: any[]): any[] => {
|
|
|
|
arr.sort((a, b) => {
|
|
|
|
if (!isNaN(a) && !isNaN(b)) {
|
|
|
|
// numerical comparison
|
|
|
|
return a - b;
|
|
|
|
}
|
|
|
|
if ((typeof a === 'string' || a instanceof String) && (typeof b === 'string' || b instanceof String)) {
|
|
|
|
return a.toLowerCase() > b.toLowerCase() ? 1 : -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const makeUnique = (arr: any[]): any[] => Array.from(new Set(arr));
|
|
|
|
|
2020-08-07 14:01:57 +00:00
|
|
|
// Remove from set A elements of set B.
|
2020-07-29 13:42:51 +00:00
|
|
|
export function arrayDiff(a: string[], b: string[]): string[] {
|
|
|
|
const mappedB = mapList(b);
|
|
|
|
return a.filter(e => !mappedB.hasOwnProperty(e));
|
|
|
|
}
|
2020-08-06 19:17:38 +00:00
|
|
|
|
2020-08-07 14:01:57 +00:00
|
|
|
// Remove from set A elements of set B, case insensitive
|
|
|
|
export function arrayDiffNocase(a: string[], b: string[]): string[] {
|
|
|
|
const mappedB = mapListTolower(b);
|
|
|
|
return a.filter(e => !mappedB.hasOwnProperty(e.toLowerCase()));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function findDuplicates(list: string[]): string[] {
|
2020-08-06 19:17:38 +00:00
|
|
|
let m = new Map<string, number>();
|
2020-08-07 14:01:57 +00:00
|
|
|
let duplicates: string[] = [];
|
2020-08-06 19:17:38 +00:00
|
|
|
list.forEach(val => {
|
2020-08-07 14:01:57 +00:00
|
|
|
if (m.has(val.toLowerCase())) {
|
|
|
|
duplicates.push(val);
|
2020-08-06 19:17:38 +00:00
|
|
|
} else {
|
2020-08-07 14:01:57 +00:00
|
|
|
m.set(val.toLowerCase(), 0);
|
2020-08-06 19:17:38 +00:00
|
|
|
}
|
|
|
|
});
|
2020-08-07 14:01:57 +00:00
|
|
|
return makeUnique(duplicates);
|
2020-08-06 19:17:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that two lists have no common elements, and no duplicates in either.
|
|
|
|
// Do a single check: checking for duplicates in the concatenated list.
|
2020-08-07 14:01:57 +00:00
|
|
|
export function findCommonElementsOrDuplicates(list1: string[], list2: string[]): string[] {
|
|
|
|
return findDuplicates(list1.concat(list2));
|
2020-08-06 19:17:38 +00:00
|
|
|
}
|
2020-08-07 13:12:16 +00:00
|
|
|
|
|
|
|
// Compare two arrays, order does not matter
|
|
|
|
export function arrayEqual(a1: any[], a2: any[]): boolean {
|
|
|
|
if (a1.length != a2.length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!(arrayDiff(a1, a2).length == 0)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!(arrayDiff(a2, a1).length == 0)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|