Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# accessor: returns the article "a" or "an" depending on the context string. Eg. "Computer" will return "a"
# and "Elephant" will return "an". Can pass a hash option `capitalize` which will capitalize the article.
# usage: {{#article}}string{{/article}} outputs "a string"
Handlebars.registerHelper "article", (options) ->
# options: pass `capitalize` true to get a capital article
string = options.fn(@)
vowels = ['a', 'e', 'i', 'o', 'u']
vowel = false
letter = string.toLowerCase()[0]
vowel = true for v in vowels when letter == v
article = 'an ' if vowel
article = 'a ' if not vowel
article = _.str.capitalize(article) if options.hash?.capitalize
return article + string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.