@amaui/utils
Version:
21 lines (20 loc) • 810 B
JavaScript
import is from './is';
import castParam from './castParam';
const optionsDefault = {
onlySufix: false
};
export const getOrdinalNumber = function (value_) {
let options_ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
const options = { ...optionsDefault,
...options_
};
const value = castParam(value_);
if (is('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 "".concat(value).concat(sufix);
}
};
export default getOrdinalNumber;