2022-06-08 19:01:38 +00:00
|
|
|
const booleanAsc = (a: unknown, b: unknown): number => {
|
|
|
|
if (!a && !!b) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!!a && !b) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
2022-04-13 16:08:37 +00:00
|
|
|
const caseInsensitiveAsc = (a: any, b: any): number => {
|
|
|
|
a = typeof a === "string" ? a.toLowerCase() : a;
|
|
|
|
b = typeof b === "string" ? b.toLowerCase() : b;
|
2021-08-30 23:02:53 +00:00
|
|
|
|
2021-09-29 04:04:58 +00:00
|
|
|
if (a < b) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (a > b) {
|
2021-08-30 23:02:53 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2021-09-29 04:04:58 +00:00
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Parses string representations of dates (e.g., "2021-02-21") and sorts in ascending order (i.e. "2021-02-01"
|
|
|
|
// appears before "2021-03-01").
|
|
|
|
// Values that are not parsable as dates return NaN and are sorted to appear before date-parsable values.
|
|
|
|
const dateStringsAsc = (a: string, b: string): number => {
|
|
|
|
const parsedA = Date.parse(a);
|
|
|
|
const parsedB = Date.parse(b);
|
|
|
|
|
|
|
|
if (isNaN(parsedA) && isNaN(parsedB)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (isNaN(parsedA)) {
|
2021-08-30 23:02:53 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2021-09-29 04:04:58 +00:00
|
|
|
if (isNaN(parsedB)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (parsedA < parsedB) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (parsedA > parsedB) {
|
|
|
|
return 1;
|
|
|
|
}
|
2021-08-30 23:02:53 +00:00
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
2022-04-13 16:08:37 +00:00
|
|
|
const hasLength = (a: unknown[], b: unknown[]): number => {
|
|
|
|
if (!a?.length && b?.length) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (a?.length && !b?.length) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
2022-06-08 19:01:38 +00:00
|
|
|
booleanAsc,
|
2022-04-13 16:08:37 +00:00
|
|
|
caseInsensitiveAsc,
|
|
|
|
dateStringsAsc,
|
|
|
|
hasLength,
|
|
|
|
};
|