@onesy/utils
Version:
31 lines (30 loc) • 1.22 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getOrdinalNumber = void 0;
const is_1 = __importDefault(require("./is"));
const castParam_1 = __importDefault(require("./castParam"));
const optionsDefault = {
onlySufix: false,
};
const getOrdinalNumber = (value_, options_ = {}) => {
const options = Object.assign(Object.assign({}, optionsDefault), options_);
const value = (0, castParam_1.default)(value_);
if ((0, is_1.default)('number', value)) {
let sufix = 'th';
const string = String(value);
if (value === 1 || (value > 20 && string[string.length - 1] === '1'))
sufix = 'st';
else if (value === 2 || (value > 20 && string[string.length - 1] === '2'))
sufix = 'nd';
else if (value === 3 || (value > 20 && string[string.length - 1] === '3'))
sufix = 'rd';
if (options.onlySufix)
return sufix;
return `${value}${sufix}`;
}
};
exports.getOrdinalNumber = getOrdinalNumber;
exports.default = exports.getOrdinalNumber;