Last active
December 9, 2016 15:55
-
-
Save kimniche/785a3d37038ca9c368eb0fae6401f4b3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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