Skip to content

Instantly share code, notes, and snippets.

@ndabAP
Last active November 22, 2021 00:42
Show Gist options
  • Save ndabAP/d7a338407bb22794418bc64875af14ee to your computer and use it in GitHub Desktop.
Save ndabAP/d7a338407bb22794418bc64875af14ee to your computer and use it in GitHub Desktop.
Vue.js pluralize filter
<template>
<div>
<p>I got {{ amount }} {{ 'cookie' | pluralize(amount) }}</p>
<button @click="decrement">Decrement</button>
</div>
</template>
<script>
export default {
data: () => ({
amount: 5
}),
methods: {
decrement () {
this.amount--
}
}
}
</script>
import Vue from 'vue'
Vue.filter('pluralize', (word, amount) => (amount > 1 || amount === 0) ? `${word}s` : word)
new Vue({render: create => create(App)}).$mount('#app')
@dav0r
Copy link

dav0r commented Sep 13, 2019

Really useful, thanks!

I added a slight modification so 0 would also append 's'.

Vue.filter('pluralize', (word, amount) => (amount > 1 || amount == 0) ? `${word}s` : word)

@ndabAP
Copy link
Author

ndabAP commented Sep 13, 2019

Great input, I adapted your version.

@bit9labs
Copy link

bit9labs commented Jan 22, 2021

This is not sufficient as the English language is much more complex:
{{ 'lady' | pluralize(2) }} becomes 'ladys' and not the correct 'ladies'

@dav0r
Copy link

dav0r commented Jan 22, 2021

It's worth noting there are complete packages available such as https://www.npmjs.com/package/pluralize weighing in at ~18kb which will handle everything thrown at it includes some other useful features such as testing whether a word is singular or plural.

There's also an interesting solution over on SO: https://stackoverflow.com/a/57129703/10074973 but that will require additional work as it doesn't cover all special cases, meaning the former may be preferable.

@iSWORD
Copy link

iSWORD commented Feb 28, 2021

I decided to do it the more manual way with:

Vue.filter('pluralize', (amount, singular, plural) => (amount > 1 || amount === 0) ? plural : singular)

so that I can use it like this:

<p>I got {{ amount }} {{ amount | pluralize('cookie', 'cookies') }}</p>

This is not very different from going completely manual but still saves a few strokes and ensures proper pluralization without adding more packages.

@connorshea
Copy link

@iSWORD to make it even simpler to use:

Vue.filter('pluralize', (amount, singular, plural = null) => {
  if (plural === null) {
    plural = `${singular}s`;
  }
  return (amount > 1 || amount === 0) ? plural : singular;
});

Then if the word pluralizes "normally" you can just exclude the third argument:

// NOTE: Not how you actually use this, just using it as a normal function for example purposes
pluralize(1, 'cookie'); // => cookie
pluralize(2, 'cookie'); // => cookies
pluralize(1, 'octopus', 'octopi'); // => octopus
pluralize(2, 'octopus', 'octopi'); // => octopi

@tamlyn
Copy link

tamlyn commented May 17, 2021

And while we're at it...

Vue.filter('pluralize', (amount, singular, plural = `${singular}s`) => amount === 1 ? singular : plural);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment