New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@qit.tools/ordinal-format

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@qit.tools/ordinal-format

This tiny library (🪶 726 bytes) given number into its ordinal representation based on the specified locale and custom suffixes. Ordinal numbers denote position or order (e.g., 1st, 2nd, 3rd).

  • 0.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Ordinal Number Formatter

This tiny library (🪶 726 bytes) converts numbers into their ordinal representation based on the specified locale and custom suffixes. Ordinal numbers denote position or order (e.g., 1st, 2nd, 3rd).

The library provides a simple utility to format numbers as ordinal strings, supporting different locales and custom suffixes, utilizing 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 ordinal = new OrdinalFormat();
console.log(ordinal.format(1)); // '1st'
console.log(ordinal.format(2)); // '2nd'
console.log(ordinal.format(3)); // '3rd'

Custom Locale and Suffixes

const frSuffixes = [
  ["one", "er"],
  ["other", "e"]
];

const ordinal = new OrdinalFormat({ locale: "fr-FR", suffixes: frSuffixes });
console.log(ordinal.format(1)); // '1er'
console.log(ordinal.format(2)); // '2e'
console.log(ordinal.format(3)); // '3e'

Plural Locale and Suffixes

const pluralSuffixes = [
  ["one", " cat"],
  ["other", " cats"]
];

const ordinal = new OrdinalFormat({ locale: "en-US", suffixes: pluralSuffixes });
console.log(ordinal.format(1)); // '1 cat'
console.log(ordinal.format(2)); // '2 cats'
console.log(ordinal.format(3)); // '3 cats'

📝 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.

Keywords

FAQs

Package last updated on 06 Jun 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc