plurals / pluralize

Pluralize or singularize any word based on a count
MIT License
2.15k stars 184 forks source link

Plurals of acronyms #127

Open echo-bravo-yahoo opened 5 years ago

echo-bravo-yahoo commented 5 years ago

I have an acronym that needs pluralized; ATM is a good proxy:

p('ATM')
'ATMS'

Unfortunately, I think the plural should be ATMs, since the "word" is uppercase not for reasons of formatting, but because it's an acronym. This is a reasonable thing for the library to do, but I can't seem to force the behavior I'm expecting, even with the various rules options:

p.addIrregularRule('ATM', 'ATMs')
p('ATM')
'ATMS'

p.addPluralRule(/^ATM$/, 'ATMs')
p('ATM')
'ATMS'

Is the "all uppercase" rule too high in precedence? Is there a way to override it with the version of the library currently released?

dfeinzeig commented 5 years ago

Would be great to handle this case

vimtor commented 3 years ago

This will be awesome

Black-Stork commented 3 years ago

I'm also looking for this, maybe there could be some flag to pass or something.. We have the PIN word and it's getting converted into PINS, but should be PINs

cirosantilli commented 2 years ago
function pluralize_wrap(s, n) {
  let ret = pluralize(s, n)
  if (n === undefined || n > 1 && s !== ret) {
    const last = ret[ret.length - 1]
    if (last === 'S') {
      ret = ret.substring(0, ret.length - 1) + last.toLowerCase()
    }
  }
  return ret
}
aaronbeall commented 1 year ago

@cirosantilli We did something similar, but the problem with this is that changing {word}S to {word}s is questionably opinionated, it'll turn all uppercase words like CHICKEN into CHICKENs (a current test case), and with your code ELF will become ELVEs... there's kind of no completely reliable way to know if you are dealing with an acronym or an all uppercase word without further context.

That said we went with assuming user provided words that are all caps are acronyms (which is common in our use case), with the added check that if the plural word changes the base word, it'll leave the pluralized word alone, so ELF does not become ELVEs or ELFs, it remains as ELVES:

const toPlural = (word) => {
  const pluralWord = pluralize(word);

  // Fixes: https://github.com/plurals/pluralize/issues/127
  return pluralWord == `${ word }S`
    ? `${ word }s`
    : pluralWord;
};

But this is still very opinionated. What if ELF is an acronym? We could go with this:

return word == word.toUpperCase()
  ? `${ word }s`
  : pluralize(word);

Which turns ELF into ELFs, but what if ELF is just Santa screaming at his lazy worker?

Only way to properly deal with this is to control input, for example disallow all-caps unless it's meant to be an acronym or require an explicit flag for acronyms. 🤷