Skip to content
/ plural Public

A simple helper utility for dealing with plurals in a string

Notifications You must be signed in to change notification settings

panoply/plural

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

A simple helper utility for dealing with plurals in a string

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published