UNPKG

@azizbecha/strkit

Version:

strkit is a utility library offering a collection of essential string functions including validation, case conversion, truncation, and more. Ideal for both JavaScript and TypeScript developers to simplify string operations in their applications.

40 lines 1.37 kB
(function (factory) { if (typeof module === "object" && typeof module.exports === "object") { var v = factory(require, exports); if (v !== undefined) module.exports = v; } else if (typeof define === "function" && define.amd) { define(["require", "exports"], factory); } })(function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = toOrdinal; /** * Converts a number into its ordinal form as a string (e.g., 1 → "1st", 2 → "2nd"). * * @param num - The number to convert to its ordinal form. * @returns The ordinal form of the number as a string. * * @example * toOrdinal(1); // Output: "1st" * toOrdinal(2); // Output: "2nd" * toOrdinal(3); // Output: "3rd" * toOrdinal(11); // Output: "11th" * toOrdinal(101); // Output: "101st" */ function toOrdinal(num) { const absNum = Math.abs(num); const suffix = absNum % 100 >= 11 && absNum % 100 <= 13 ? 'th' : absNum % 10 === 1 ? 'st' : absNum % 10 === 2 ? 'nd' : absNum % 10 === 3 ? 'rd' : 'th'; return `${num}${suffix}`; } }); //# sourceMappingURL=toOrdinal.js.map