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')
@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