Next.js Discord

Discord Forum

Looking for shorthand

Answered
Japanese Bobtail posted this in #help-forum
Open in Discord
Avatar
Japanese BobtailOP
Hello there. I'am looking for a shorthand with condition:

If only 1 article, there's no need for the "s" in articles. In other words add "s" to article if count is greater 1.

aria-label={Category ${name.replace('&', '&')} with ${count} articles}
Answered by B33fb0n3
you can use packages like [pluralize](https://www.npmjs.com/package/pluralize) to do this for you.

The syntax is easy:
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"

And it's used by many others as well (see attached)
Image
View full answer

9 Replies

Avatar
you can use packages like [pluralize](https://www.npmjs.com/package/pluralize) to do this for you.

The syntax is easy:
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"

And it's used by many others as well (see attached)
Image
Answer
Avatar
Japanese BobtailOP
That's fine for more than one condition. But in my opinion thats better:
aria-label={Category ${name.replace('&', '&')} with ${count} article${count !== 1 ? 's' : ''}}
Avatar
of course there are multiple versions on how you can solve a problem.

I like to use:
pluralize('article', 2, true)

Instead of doing
aria-label={${count} article${count !== 1 ? 's' : ''}}
Avatar
Japanese BobtailOP
Is it possible to shorthand ${count !== 1 ? 's' : ''} we do not need the else.
Avatar
for some words the plural isn't just an "s" at the end. So no, not with this package
Avatar
Japanese BobtailOP
ok i will keep the plugin in mind. thanks for help sir.
Avatar
sure thing, is your initial question resolved like that?
Avatar
Japanese BobtailOP
yes
Avatar
happy to help