Coder Social home page Coder Social logo

pluralize's Introduction

Pluralize

NPM version NPM downloads Build status Test coverage File Size CDNJS

Pluralize and singularize any word.

Installation

npm install pluralize --save
yarn add pluralize
bower install pluralize --save

Node

var pluralize = require('pluralize')

AMD

define(function (require, exports, module) {
  var pluralize = require('pluralize')
})

<script> tag

<script src="pluralize.js"></script>

Why?

This module uses a pre-defined list of rules, applied in order, to singularize or pluralize a given word. There are many cases where this is useful, such as any automation based on user input. For applications where the word(s) are known ahead of time, you can use a simple ternary (or function) which would be a much lighter alternative.

Usage

  • word: string The word to pluralize
  • count: number How many of the word exist
  • inclusive: boolean Whether to prefix with the number (e.g. 3 ducks)

Examples:

pluralize('test') //=> "tests"
pluralize('test', 0) //=> "tests"
pluralize('test', 1) //=> "test"
pluralize('test', 5) //=> "tests"
pluralize('test', 1, true) //=> "1 test"
pluralize('test', 5, true) //=> "5 tests"
pluralize('蘋果', 2, true) //=> "2 蘋果"

// Example of new plural rule:
pluralize.plural('regex') //=> "regexes"
pluralize.addPluralRule(/gex$/i, 'gexii')
pluralize.plural('regex') //=> "regexii"

// Example of new singular rule:
pluralize.singular('singles') //=> "single"
pluralize.addSingularRule(/singles$/i, 'singular')
pluralize.singular('singles') //=> "singular"

// Example of new irregular rule, e.g. "I" -> "we":
pluralize.plural('irregular') //=> "irregulars"
pluralize.addIrregularRule('irregular', 'regular')
pluralize.plural('irregular') //=> "regular"

// Example of uncountable rule (rules without singular/plural in context):
pluralize.plural('paper') //=> "papers"
pluralize.addUncountableRule('paper')
pluralize.plural('paper') //=> "paper"

// Example of asking whether a word looks singular or plural:
pluralize.isPlural('test') //=> false
pluralize.isSingular('test') //=> true

License

MIT

pluralize's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pluralize's Issues

Minification broken for version 1.1.4

I'm pointing my bower.json to 1.1.2, and it just picked up version 1.1.4. During minification, it produced bad javascript, resulting in this error in the console:

Uncaught TypeError: (intermediate value)(...) is not a function

Can post more details here once I put out flames :) but my guess is missing semi-colon

genesis is singular

Pluralize.isSingular('genesis') returns false, but is infact singular. Geneses is the plural of genesis. Could this be patched?

Add to Bower

Would it be possible to add this package to bower?
Thanks.

Handle amounts that are less than 1 correctly?

Thanks for building this, super helpful! I may have found a bug though.

const unit = "apple";
const amount = 0.5;
const formatedUnit = pluralize(unit, amount);

In this example, formatedUnit is equal to "apples" but should be "apple". Right?

incorrect results for words ending '-ties'

Words like "varieties", "niceties" etc. are incorrectly singularized:

pluralize('variety', 2) //=> "varieties"
pluralize('varieties', 1) //=> "varietie"

because they match the rule [ /(^[pl]|zomb|^(?:neck)?t|[aeo][lt]|cut)ies$/i, "$1ie" ].

Judging from the tests, the [aeo] part in this rule is to protect the "-ie" ending on the words "floatie" and "goalie" (the former of which is especially questionable).

Need to pluralize multiple words in a string

0/no files include "cake" in their names.
1 file includes "cake" in its name.
2 files include "cake" in their names.

This could be written with multiple calls to pluralize but I think a template based string function would be better.

'[number/no] [pl:file] [!pl:includes] "cake" in [pl:its/their] [pl:name]'

Language.makePlural(
'[number] [pl:file] [!pl:includes] “cake” in [pl:its/their] [pl:name]'
, value
)

I need this for an application so will write one. If you want to include in your library let me know. Suggestions for alternate token/syntax welcome.

Using in ES2015 project

I can transpile Pluralize into an ES2015 library I've created but when I use rollup to transpile to CJS and UMD it gives me the warning that:

'plural' is not exported by 'node_modules/pluralize/pluralize.js'

Modules isn't a complete dark art to me but there's still a lot to learn. I just wondered if there was something about the export method that's been used in this library that would prevent me from doing this transpilation.

Multiple Instances

It'd be cool to create multiple pluralize instances. The use-case that's been brought up before is acronyms (so perhaps have an option here for output case or a function that handles sanitization manually?). However, I think multiple locales would be perfect too. E.g. require('pluralize/de'). If anyone is willing to try create automated pluralizations for other locales that'd would be very cool.

Camelcase support?

var plural = pluralize.plural('myCamelCaseWord');
plural === 'mycamelcasewords'

Is there a way to have plural === 'myCamelCaseWords'

isPlural & isSingular

I use your package for switching between entity names for repository queries. It would be very helpfull if boolean functions isPlural and isSingular were inside!

unexpected result

I'm using pluralize right now as a dep of sails.js

i'm not sure if it's how sails has implemented pluralize,

however the word bureau has unexpectedly been pluralized to bureaux, which has broken my application routing.

Abbreviated words

Hey,

I'm currently working on a project that needs to support pluralization of the abbreviated words, so I need to add lowercase 's' letter to the end of these words (e.g. VIP => VIPs, currently I get VIPS).

Are you planning to add this feature to your library? Something like pluralize.addAbbreviatedRule() or similar.

Regards

P.S. Thanks for the great library!

Add zero rule

I was wondering if it's possible to substitute 0 for a word like No

Example:
pluralize.addZeroRule('test', 'no') would yield No tests instead of 0 tests

Wrong pluralization of "slice"

Fairly self-explanatory, it seems that the term "slice" is being incorrectly pluralized. See below:

pluralize('slice', 1)
"slice" // Expected
"slouse" // Actual

pluralize('slice', 2)
"slices" // Expected
"slice" // Actual

My current workaround consists of an additional singular and plural rule:

pluralize.addSingularRule(/slice$/i, 'slice');
pluralize.addPluralRule(/slice$/i, 'slices');

Is anybody else able to reproduce this behaviour, and if so, would this warrant a PR to fix the bug? Let me know if so!

whisky vs whiskey, when from plural to singular

    console.log("whisky", pluralize("whisky")) // whisky whiskies
    console.log("whiskey", pluralize("whiskey")) // whiskey whiskies

    console.log("whiskies", pluralize.singular("whiskies")) // whiskies whiskey.
    ~console.log("whiskeys", pluralize.singular("whiskey")) // whiskeys whiskey~
    EDIT: made a mistake here ^^^. meant this 
    console.log("whiskeys", pluralize.singular("whiskeys")) // whiskeys whiskey

so whisky to plural => whiskies and back to singular and we have new word => whiskey

i18n

Is there any concept for internationalization?

Pluralize negative values

Currently pluralize('test', -1) returns tests but the singular form would make more sense. Use cases include point, coin, etc. What do you think?

Supports common verbs?

I'm just facing a situation where I use pluralize to pluralize a noun (membership/memberships) but then the following word is a verb (is/are) and I will have to modify it by hands.

What about supporting common verbs like to be to have to do ?
Could look like

pluralize(
      'to be',
      count,
      false
    );

?

Maybe add the extra function in the package? pluralize_verb? or an argument to specify it's a verb?

Change case unexpectedly

> const pluralize = require('pluralize');
undefined
> s = 'RingOutRequestTo'
'RingOutRequestTo'
> pluralize(s, 1);
'RingOutRequestto'

The final result should be RingOutRequestTo instead of RingOutRequestto.

Polymer JS import not working on Chrome or Safari / Partially on Firefox

I am writing a simple app in Polymer. When I import pluralize I get partial functionality on Firefox but no functionality on Chrome or Safari.

I am using the following line to import into my app:
import '../node_modules/pluralize/pluralize.js'

I've also tried other variants with the same or worse results:
import pluralize from 'pluralize'
const pluralize = require('../node_modules/pluralize/pluralize.js')

The import fails on Chrome with the error:

TypeError: Cannot set property 'pluralize' of undefined

The failing line in pluralize.js is:
root.pluralize = pluralize();
which is the fallback case for exporting the function. root is undefined.

On Firefox is doesn't fail, yet I must reference by using from the window object:
window.pluralize.isPlural(thing) ? "are" : "is"

How do I get the pluralize object imported and initialized in my Polymer application.

Question: use plural for zero?

Hi,

Thanks for the lib, so happy to see he even recognizes Chinese characters.

I'm running the following test and it passed:

expect(pluralize('test', 0, true)).to.equal('0 tests');

But I'm curious that is it common to use plural for zero in English? (I'm not native English speaker)

Thanks.

Update usage

The usage in readme says
pluralize.plural('singles', 1)
to singularize a word. This seems not to work, since it gives me singles.
After digging into the sources, it turns out to be
pluralize.singular('singles')

So, I think, the readme/usage is outdated?!

Pluralize and performance

Thanks for this important library. I was surprised to read
"For applications where the word(s) are known ahead of time, you can use a simple ternary (or function) which would be a much lighter alternative."
I'd expect such a library to be very light on performance. Isn't it the case?
Thanks in advance!

Plural after a number is in upper rather than lower case

First off - thank you for sharing this with everyone, a lovely little library for Node JS.

I have spotted that if you call pluralize with a string ending in a number then it always appends the "s" as upper case. For example:

pluralize.plural('Blank Application2');

will return 'Blank Application2S' rather than 'Blank Application2s' which I think would look nicer :)

Thanks
Rob

Is it possible to pluralize more than one word

As an example this would be cleaner;
pluralize('file has', 1, true) = 1 file has
pluralize('file has', 5, true) = 5 files have

than:
pluralize('file', 1, true) + ' ' + pluralize('has', 1, false) = 1 file has
pluralize('file', 5, true) + ' ' + pluralize('has', 5, false) = 5 files have

Garbages

I think "garbage" should be considered uncountable.

pluralize('is', 1) yields 'i'?

pluralize('is', 0) === 'is'
pluralize('is', 1) === 'i'
pluralize('is', 2) === 'is'

pluralize.addIrregularRule('is', 'are')

// Expected behavior...
pluralize('is', 0) === 'are'
pluralize('is', 1) === 'is'
pluralize('is', 2) === 'are'

I'm most confused by the pluralize('is', 1) === 'i' case. Before adding the irregular case, my expectation is that pluralize wouldn't mutate the word...

NPM Publish

I'm excited to start using this library, but I need to use the isPlural() function from v5.1.0 which is not available in NPM (latest is 5.0.0). Would you mind publishing the latest to NPM, please?

Thanks!
Ben

Pluralize trailing underscore: What is the expected behavior for pluralize('frog_')?

What is the correct behavior for pluralizing words that end in non-word characters like a trailing underscore?

I recently changed my hack-of-a-pluralize code (just add "s"!) to this awesome library. However, I had one surprise result. When pluralizing "frog_" I ended up with "frog_S". Why the upper-case "S"? Shouldn't it at least be lower-case?

Thinking about it, I think the best behavior would be to remove any non-word characters from the end first, then pluralize, then re-attach the removed characters.

Examples:

pluralize("frog_")
// currently outputs > "frog_S" -- probably wrong
// should probably output > "frog_s" or "frogs_" 

The "remove then reattach" method makes more sense when we have non-trivial pluralization:

pluralize("person_")
// currently outputs > "person_S"
// should probably output > "person_s" or "people_"

Why do I care? My parser-generator (npm caffeine-eight) automatically generates pluralized names from client-provided rule-names. Sometimes those rule-names have trailing underscores. This turns out to be a useful pattern for expressing if a rule consumes whitespace - something that must be done manually with parsing-expression-grammar parsers.

How should I run the tests?

Hello,
I was wondering how should I run the library test files. I just used npm install to get chai and mocha and then I used node test.js but I get an error describe is undefined. Would you please add some details about running tests?

SemVer compliance

Hey!

Thanks for writing this module. I've seen it used in a lot of my own projects and others. One thing I've noticed is that you seem to be incrementing the major version of the module even when the changes are backwards compatible. The guideline for SemVer is defined here: http://semver.org/ and it states:

Minor version Y (x.Y.z | x > 0) MUST be incremented if new, backwards compatible functionality is introduced to the public API. It MUST be incremented if any public API functionality is marked as deprecated. It MAY be incremented if substantial new functionality or improvements are introduced within the private code. It MAY include patch level changes. Patch version MUST be reset to 0 when minor version is incremented.

Major version X (X.y.z | X > 0) MUST be incremented if any backwards incompatible changes are introduced to the public API. It MAY include minor and patch level changes. Patch and minor version MUST be reset to 0 when major version is incremented.

Most changesets in this project do not appear to fall into the major category. For open source projects keeping their NPM packages up to date with things like Greenkeeper, these major releases create a lot of noise for a package for which most projects would not need to rapidly update.

Thanks so much for your work on this project and have a great week!

Thanks,
Max

Singularizing contractions and possessives?

Words like it's have the s after the apostrophe removed. What do you think of modifying the library to leave the s on if it follows an apostrophe?

> singular("it's")
'it\''

Remove an irregular rule

Is it possible to have an option to remove an irregular rule?
I'd like to remove media in order to have pluralize.plural('media') => medias instead of media.

Olife

pluralize("olives", 1) // "olife" (should be "olive")

Some words to be added to this rule include olive, adhesive, explosive and maybe some others from this list

Yous

pluralize("you") // "yous"

While "yous" (or "youse") may be considered a word by some dictionaries, the accepted correct pluralization of "you" is "you".

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.