npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ordinal-js

v1.1.0

Published

Utility to convert from numbers to their ordinal representations

Downloads

9,362

Readme

ordinal.js

Build Status npm npm Coverage Status

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)

Installation

npm install ordinal-js --save

or if you rather use yarn

yarn add ordinal-js

Usage

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:

Example

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.

Example

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.

Example

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)

API

This section describes every method in the ordinal object

toOrdinal

Description

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.

Example

const ordinal = require("ordinal-js");

console.log(ordinal.toOrdinal(12)) //12th
console.log(ordinal.toOrdinal(12, suffix => ` ${suffix.toUpperCase()}`)) //12 TH

ordinalSuffix

Description

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.

Example

const ordinal = require("ordinal-js");

console.log(ordinal.ordinalSuffix(12)) //th