upend/webui/src/util/sort.ts

101 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-02-15 22:05:51 +01:00
import type { UpEntry } from "upend";
export type SortKeys = { [key: string]: string[] };
export function sortByValue(entries: UpEntry[], sortKeys: SortKeys): void {
2022-02-19 18:38:36 +01:00
entries.sort((aEntry, bEntry) => {
if (aEntry.value.t === "Number" && bEntry.value.t === "Number") {
return bEntry.value.c - aEntry.value.c;
}
2022-02-15 22:17:23 +01:00
2022-02-19 18:38:36 +01:00
if (
!sortKeys[aEntry.value.c]?.length ||
!sortKeys[bEntry.value.c]?.length
) {
2022-02-15 22:05:51 +01:00
if (
2022-02-19 18:38:36 +01:00
Boolean(sortKeys[aEntry.value.c]?.length) &&
2022-02-15 22:05:51 +01:00
!sortKeys[bEntry.value.c]?.length
) {
2022-02-19 18:38:36 +01:00
return -1;
} else if (
!sortKeys[aEntry.value.c]?.length &&
Boolean(sortKeys[bEntry.value.c]?.length)
) {
return 1;
2022-02-15 22:05:51 +01:00
} else {
2022-02-19 18:38:36 +01:00
return String(aEntry.value.c).localeCompare(
String(bEntry.value.c),
2022-02-15 22:05:51 +01:00
undefined,
{ numeric: true, sensitivity: "base" }
);
}
2022-02-19 18:38:36 +01:00
} else {
return sortKeys[aEntry.value.c][0].localeCompare(
sortKeys[bEntry.value.c][0],
undefined,
{ numeric: true, sensitivity: "base" }
);
}
});
2022-02-15 22:05:51 +01:00
}
export function sortByValueLength(entries: UpEntry[]) {
entries.sort((aEntry, bEntry) => {
return String(aEntry.value.c).length - String(bEntry.value.c).length;
});
}
2022-02-15 22:05:51 +01:00
export function sortByAttribute(entries: UpEntry[]): void {
entries.sort((aEntry, bEntry) => {
return aEntry.attribute.localeCompare(bEntry.attribute);
});
}
export function sortByEntity(entries: UpEntry[], sortKeys: SortKeys): void {
2022-02-19 18:38:36 +01:00
entries.sort((aEntry, bEntry) => {
if (!sortKeys[aEntry.entity]?.length || !sortKeys[bEntry.entity]?.length) {
2022-02-15 22:05:51 +01:00
if (
2022-02-19 18:38:36 +01:00
Boolean(sortKeys[aEntry.entity]?.length) &&
2022-02-15 22:05:51 +01:00
!sortKeys[bEntry.entity]?.length
) {
2022-02-19 18:38:36 +01:00
return -1;
} else if (
!sortKeys[aEntry.entity]?.length &&
Boolean(sortKeys[bEntry.entity]?.length)
) {
return 1;
2022-02-15 22:05:51 +01:00
} else {
2022-02-19 18:38:36 +01:00
return aEntry.entity.localeCompare(bEntry.entity);
2022-02-15 22:05:51 +01:00
}
2022-02-19 18:38:36 +01:00
} else {
return sortKeys[aEntry.entity][0].localeCompare(
sortKeys[bEntry.entity][0]
);
}
});
2022-02-15 22:05:51 +01:00
}
export function defaultEntitySort(
entries: UpEntry[],
sortKeys: SortKeys
): UpEntry[] {
const result = entries.concat();
2022-02-19 18:38:36 +01:00
sortByValue(result, sortKeys);
sortByValueLength(result);
2022-02-19 18:38:36 +01:00
sortByAttribute(result);
sortByEntity(result, sortKeys);
2022-02-15 22:05:51 +01:00
return result;
}
2022-02-15 22:17:23 +01:00
export function entityValueSort(
entries: UpEntry[],
sortKeys: SortKeys
): UpEntry[] {
const result = entries.concat();
2022-02-19 18:38:36 +01:00
sortByEntity(result, sortKeys);
sortByAttribute(result);
sortByValueLength(result);
2022-02-19 18:38:36 +01:00
sortByValue(result, sortKeys);
2022-02-15 22:17:23 +01:00
return result;
}