bigbuffet-rw/packages/pl-fe/src/utils/comparators.ts
marcin mikołajczak 4d5690d0c1 Switch to workspace
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
2024-08-28 13:01:08 +02:00

37 lines
946 B
TypeScript

/**
* Compare numerical primary keys represented as strings.
* For example, '10' (as a string) is considered less than '9'
* when sorted alphabetically. So compare string length first.
*
* - `0`: id1 == id2
* - `1`: id1 > id2
* - `-1`: id1 < id2
*/
const compareId = (id1: string, id2: string) => {
if (id1 === id2) {
return 0;
}
if (id1.length === id2.length) {
return id1 > id2 ? 1 : -1;
} else {
return id1.length > id2.length ? 1 : -1;
}
};
/**
* Compare by dates, where most recent date is returned first.
*
* @param dateString1 - string that is parsable by Date
* @param dateString2 - string that is parsable by Date
* @returns 1 | -1 | 0
*/
const compareDate = (dateString1: string, dateString2: string) => {
const date1 = new Date(dateString1);
const date2 = new Date(dateString2);
if (date2 < date1) return -1;
if (date2 > date1) return 1;
return 0;
};
export { compareId, compareDate };