pluralize icon indicating copy to clipboard operation
pluralize copied to clipboard

Plurals of acronyms

Open echo-bravo-yahoo opened this issue 6 years ago • 5 comments
trafficstars

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?

echo-bravo-yahoo avatar Jun 03 '19 16:06 echo-bravo-yahoo

Would be great to handle this case

dfeinzeig avatar Sep 11 '19 21:09 dfeinzeig

This will be awesome

vimtor avatar May 20 '21 07:05 vimtor

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

Black-Stork avatar Jul 12 '21 23:07 Black-Stork

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
}

cirosantilli avatar Jun 14 '22 05:06 cirosantilli

@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. 🤷

aaronbeall avatar Sep 08 '23 13:09 aaronbeall