superflected
Version:
A port of ActiveSupport's inflector to Node.js
20 lines (18 loc) • 400 B
text/typescript
export function ordinal(number: string | number) {
const absNumber = Math.abs(Number(number));
const mod100 = absNumber % 100;
if (mod100 === 11 || mod100 === 12 || mod100 === 13) {
return "th";
} else {
switch (absNumber % 10) {
case 1:
return "st";
case 2:
return "nd";
case 3:
return "rd";
default:
return "th";
}
}
}