Ordinal Number Formatter
This library provides a simple utility to format numbers as ordinal strings, supporting different locales and custom suffixes based on Intl.PluralRules().
📦 Installation
🎉 NPM
npm i @qit.tools/ordinal-format
🌟 PNPM
pnpm add @qit.tools/ordinal-format
🧶 Yarn
yarn add @qit.tools/ordinal-format
🎓 How to use
Importing the Class
import { OrdinalFormat } from "@qit.tools/ordinal-format";
Basic Usage
const formatter = new OrdinalFormat();
console.log(formatter.formatOrdinal(1));
console.log(formatter.formatOrdinal(2));
console.log(formatter.formatOrdinal(3));
Custom Locale and Suffixes
const frSuffixes = [
["one", "er"],
["other", "e"]
];
const formatter = new OrdinalFormat({ locale: "fr-FR", suffixes: frSuffixes });
console.log(formatter.formatOrdinal(1));
console.log(formatter.formatOrdinal(2));
console.log(formatter.formatOrdinal(3));
📝 Class and Method
OrdinalFormat
A class to format numbers as ordinal strings.
Constructor
constructor(options?: Options)
options
(Options, optional): An optional object to specify the locale and custom suffixes.
Method
formatOrdinal
Formats a number as an ordinal string.
formatOrdinal(num: number): string
num
(number): The number to be formatted as an ordinal.
Options
type Options = {
locale?: string;
suffixes?: [string, string][];
};
locale
(optional): The locale code (e.g., "en-US", "fr-FR") that determines the language rules for ordinal formatting. Defaults to "en-US".suffixes
(optional): An array of tuples, where each tuple consists of a plural rule category (e.g., "one", "two", "few", "other") and its corresponding ordinal suffix.
License
MIT
Contributions
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
Author
Developed by Qit.tools.
Feel free to explore the source code and contribute to this library on GitHub.