Move 'compareId' to comparators utils

This commit is contained in:
Chewbacca
2022-11-15 08:58:55 -05:00
parent a202fe68d1
commit db5d0d6400
5 changed files with 7 additions and 5 deletions

View File

@ -0,0 +1,23 @@
'use strict';
/**
* 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
*/
function 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;
}
}
export { compareId };