ordinal-number-suffix
Version:
add suffixes to numbers (1st, 33rd, etc.)
32 lines (29 loc) • 537 B
JavaScript
/**
* Get the ordinal number with suffix from `n`
*
* @api public
* @param {Number} n
* @return {String}
*/
exports = module.exports = function (n) {
return n + exports.suffix(+n);
};
/**
* Get the suffix for the given `n`
*
* @api private
* @param {Number} n
* @return {String}
*/
exports.suffix = function (n) {
n %= 100
return Math.floor(n / 10) === 1
? 'th'
: (n % 10 === 1
? 'st'
: (n % 10 === 2
? 'nd'
: (n % 10 === 3
? 'rd'
: 'th')));
};