Skip to content

Instantly share code, notes, and snippets.

@kimniche
Last active December 9, 2016 15:55
Show Gist options
  • Save kimniche/785a3d37038ca9c368eb0fae6401f4b3 to your computer and use it in GitHub Desktop.
Save kimniche/785a3d37038ca9c368eb0fae6401f4b3 to your computer and use it in GitHub Desktop.
function pluralizer(strings, ...values) {
// `strings` is [ 'I ate ', ' ' ]
let output = []
suffix = ''
if (values[0] !== 1) {
suffix = 's'
}
strings.forEach((string, i) => {
output.push(string, values[i])
})
output.push(suffix)
return output.join('')
}
let quantity = 3, noun = 'pizza'
console.log(pluralizer`I ate ${quantity} ${noun}`) // -> "I ate 3 pizzas"
quantity = 1; noun = 'potato'
console.log(pluralizer`I ate ${quantity} ${noun}`) // -> "I ate 1 potato"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment