
Security News
Meet the Socket Team at RSAC and BSidesSF 2025
Join Socket for exclusive networking events, rooftop gatherings, and one-on-one meetings during BSidesSF and RSA 2025 in San Francisco.
This library provides a simple utility to translate from a regular number to its ordinal representation (1 to 1st, 2 to 2nd, 3 to 3rd)
npm install ordinal-js --save
or if you rather use yarn
yarn add ordinal-js
Currently only english strategy is supported, although there are plans to extend the library and add i18n support.
Converting a number to its ordinal representation is as simple as:
const ordinal = require("ordinal-js");
console.log(ordinal.toOrdinal(1)) // "1st"
console.log(ordinal.toOrdinal(2)) // "2nd"
console.log(ordinal.toOrdinal(3)) // "3rd"
console.log(ordinal.toOrdinal(4)) // "4th"
Also the function ordinalSuffix(number)
is available and will return the suffix
itself without prepending the number to it.
const ordinal = require("ordinal-js");
console.log(ordinal.ordinalSuffix(1)) // "st"
console.log(ordinal.ordinalSuffix(2)) // "nd"
console.log(ordinal.ordinalSuffix(3)) // "rd"
console.log(ordinal.ordinalSuffix(4)) // "th"
If you don't care about prototypes pollution you can invoke the ordinal
function
itself and it will add two new methods (toOrdinal()
and ordinalSuffix()
) to
Number's prototype.
const ordinal = require("ordinal-js");
ordinal(); //only needed once
const someNumber = 1;
console.log(someNumber.ordinalSuffix()) //st
console.log(someNumber.toOrdinal()) //1st
Note that this approach will only work with non literal numbers (you cannot do
1.toOrdinal()
while in strict mode)
This section describes every method in the ordinal object
Returns the ordinal representation of the number
@param {Number} number - a number, if type of the provided element is not a number then function will throw a TypeError. If NaN then "NaN" will be returned.
@param {Function} transform - an optional function that will be invoked with the suffix associated to the number. This can be used to perform some transformation before appending the suffix to the number itself.
@throws {TypeError} - if the provided parameter is not a number.
const ordinal = require("ordinal-js");
console.log(ordinal.toOrdinal(12)) //12th
console.log(ordinal.toOrdinal(12, suffix => ` ${suffix.toUpperCase()}`)) //12 TH
Returns the ordinal suffix for the given number
@param {Number} number - a number, if type of the provided element is not a number then function will throw a TypeError. If NaN undefined will be returned.
@throws {TypeError} - the provided parameter is not a number.
const ordinal = require("ordinal-js");
console.log(ordinal.ordinalSuffix(12)) //th
FAQs
Utility to convert from numbers to their ordinal representations
The npm package ordinal-js receives a total of 1,367 weekly downloads. As such, ordinal-js popularity was classified as popular.
We found that ordinal-js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Join Socket for exclusive networking events, rooftop gatherings, and one-on-one meetings during BSidesSF and RSA 2025 in San Francisco.
Security News
Biome's v2.0 beta introduces custom plugins, domain-specific linting, and type-aware rules while laying groundwork for HTML support and embedded language features in 2025.
Security News
Next.js has patched a critical vulnerability (CVE-2025-29927) that allowed attackers to bypass middleware-based authorization checks in self-hosted apps.