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

@sissel/plural

v1.0.0

Published

A simple helper utility for dealing with plurals in a string

Downloads

4

Readme

plural

A tiny 600 byte string parser for word pluralization. Extracted from an internal API logging system I maintain. It is fast, effective and an easy drop in solution for handling basic word plurals.

Links

Usage

The function accepts a string as the first parameter which will contain the plural sentence. The second parameter accepts an array or number. When you pass an array of numbers (eg: number[]) you can reference each index in that array to apply pluralized appenditures based on the position of their indexes. If you pass an array or arrays (eg: any[][]) the function will look to the lengths. It is hard to articulate in words, so see the below examples.

Note that N is sugar for 0 or the first items in a spread.

How it works

Below are a couple of example to help you understand how it works.

// Numbers
plural("#{N} #{0} #{1} #{2} #{3}", 1, 2, 3, 4); // => 1, 1, 2, 3, 4

// Arrays
plural("#{N} #{0} #{1} #{2} #{3}", ["x"], ["x", "x"], ["x", "x", "x"], ["x", "x", "x", "x"]); // => 1, 1, 2, 3, 4

// Pluralize cat and bird (first value is default)
plural("#{dog} #{cat}{1} #{bird}{2}", 1, 2, 3); // => dog, cats, birds

// Pluralize dog (first value is default)
plural("#{dog} #{cat} #{bird}", 2, 1, 1); // => dogs, cat, bird

// Pluralize birds based on 2nd index (it uses 0 based index when referencing)
plural("#{dog} #{cat} #{bird}{2}", 1, 2, 1); // => dog, cat, birds

// Pluralize dogs and birds based on 2nd and 3rd index (it uses 0 based index when referencing)
plural("#{dog}{1} #{cat} #{bird}{2}", 1, 2, 2); // => dogs, cat, birds

// Pluralize dog and use 2nd value (are) in the literal based on 2nd index in the spread
plural("The #{dog}{1} #{is|are}{1} chasing the #{cat}", 1, 2); // => The dogs are chasing the cat

// Pluralize cat based on 2nd index in the spread
plural("The #{dog} #{is|are} chasing the #{cat}{1}", 1, 2); // => The dog is chasing the cats

Number

Passing a number

plural("#{N} #{person|people} #{is|are} boxing in #{N} #{fight}", 1);
// => 1 person is boxing in 1 fight

plural("#{N} #{person|people} #{is|are} boxing in #{N} #{fight}", 3);
// => 3 people are boxing in 3 fights

Spread of Numbers

Passing a spread of numbers and referencing them based on index position. Remember that N is sugar for 0.

plural("#{N} #{person|people} #{is|are} boxing in #{1} #{fight}{1}", 2, 1);
// => 2 people are boxing in 1 fight

plural("#{N} #{person|people} #{is|are} boxing in #{1} #{fight}{1}", 1, 2);
// => 1 person is boxing in 2 fights

Spread of Arrays

Passing a spread of arrays. Each array passed will used the index length as the value.

const arr_1 = [{ foo: "foo" }, { foo: "bar" }, { foo: "baz" }];
const arr_2 = ["random", "values"];

plural("#{N} #{person|people} #{is|are} boxing in #{0} #{fight}{1}", arr_1, arr_2);
// => 3 people are boxing in 2 fights

plural("#{N} #{person|people} #{is|are} boxing in #{N} #{fight}", arr_2);
// => 2 people are boxing in 2 fights

License

MIT