sectom
Version:
Sectom is a useful npm package that has multiple easy to use functions.
29 lines (26 loc) • 578 B
JavaScript
/**
* Returns the ordinal suffix of a given string or number
* @param {string | number} o
* @example
* ordinal_suffix("1") // 1st
* ordinal_suffix(2) // 2nd
* ordinal_suffix("3") // 3rd
* ordinal_suffix(4) // 4th
* @returns {string}
*/
function ordinal_suffix(o) {
const i = parseInt(o);
var j = i % 10,
k = i % 100;
if (j == 1 && k != 11) {
return i + "st";
}
if (j == 2 && k != 12) {
return i + "nd";
}
if (j == 3 && k != 13) {
return i + "rd";
}
return i + "th";
}
module.exports = ordinal_suffix;