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
|
|
|
// Sort: treat numbers as number, strings as case-insensitive
|
2020-09-18 14:39:31 +00:00
|
|
|
export function sortElements (arr: unknown[]): unknown[] {
|
2020-07-29 13:42:51 +00:00
|
|
|
arr.sort((a, b) => {
|
2020-09-18 14:39:31 +00:00
|
|
|
if (typeof a === "number" && typeof b == "number") {
|
2020-07-29 13:42:51 +00:00
|
|
|
// numerical comparison
|
|
|
|
return a - b;
|
|
|
|
}
|
|
|
|
if ((typeof a === 'string' || a instanceof String) && (typeof b === 'string' || b instanceof String)) {
|
2020-09-18 14:39:31 +00:00
|
|
|
if (!isNaN(Number(a)) && !isNaN(Number(b))) {
|
|
|
|
// numerical comparison
|
|
|
|
return Number(a) - Number(b);
|
|
|
|
}
|
2020-07-29 13:42:51 +00:00
|
|
|
return a.toLowerCase() > b.toLowerCase() ? 1 : -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
2020-09-18 14:39:31 +00:00
|
|
|
export function makeUnique(arr: string[]): string[] {
|
|
|
|
return Array.from(new Set(arr));
|
|
|
|
}
|
2020-07-29 13:42:51 +00:00
|
|
|
|
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[] {
|
2020-09-18 14:39:31 +00:00
|
|
|
const setB = new Set(b);
|
|
|
|
return a.filter(e => !setB.has(e));
|
2020-07-29 13:42:51 +00:00
|
|
|
}
|
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[] {
|
2020-09-18 14:39:31 +00:00
|
|
|
const setB = new Set(b.map(e => e.toLowerCase()));
|
|
|
|
return a.filter(e => !setB.has(e.toLowerCase()));
|
2020-08-07 14:01:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function findDuplicates(list: string[]): string[] {
|
2020-09-18 14:39:31 +00:00
|
|
|
const m = new Map<string, number>();
|
|
|
|
const 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
|
2020-09-18 14:39:31 +00:00
|
|
|
export function arrayEqual(a1: string[], a2: string[]): boolean {
|
2020-08-07 13:12:16 +00:00
|
|
|
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;
|
|
|
|
}
|
2020-08-10 14:56:02 +00:00
|
|
|
|
|
|
|
export function reverseCase(s: string): string {
|
|
|
|
const n = s.length;
|
2020-09-18 14:39:31 +00:00
|
|
|
let out = "";
|
|
|
|
for (let i = 0; i < n; ++i) {
|
2020-08-10 14:56:02 +00:00
|
|
|
const c = s[i];
|
|
|
|
if (isLowerCase(c)) {
|
|
|
|
out += c.toUpperCase();
|
|
|
|
} else if (isUpperCase(s[i])) {
|
|
|
|
out += c.toLowerCase();
|
|
|
|
} else {
|
|
|
|
out += c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|