 toCodePoints = (str) ->
     chars = []
     i = 0
-    
+
     while i < str.length
         c1 = str.charCodeAt(i)
-        
+
         if c1 >= 0xD800 and c1 < 0xDC00 and i+1 < str.length
             c2 = str.charCodeAt(i+1)
             if c2 >= 0xDC00 && c2 < 0xE000
                 chars.push(0x10000 + ((c1-0xD800)<<10) + (c2-0xDC00))
                 i += 2
                 continue
-        
+
         chars.push(c1)
         i++
 

Emojis.template = (emoji) ->
  "<img src='#{Emojis.basePath()}/#{imagePath(emoji)}' title='#{emoji.alias}' alt='#{emoji.emoji or emoji.alias}' class='emoji'>"

Emojis.parse = (text) ->
  if typeof text isnt 'string'
    return ''

  text = text.toString()

  text
  .replace smileyRegexp, (match, smiley, nose) ->
    smiley = smiley.toUpperCase()
    smiley = if nose then smiley.replace(/:-/g, ':') else smiley

    if emoji = Emojis.findOne(ascii: smiley)
      return Emojis.template(emoji)
    else
      return match

  .replace shortnameRegexp, (match, alias) ->
    if emoji = Emojis.findOne(alias: alias)
      return Emojis.template(emoji)
    else
      return match


Emojis = new Mongo.Collection 'emojis',
  transform: (emoji) ->

    # Shortcut helpers

    emoji.path = Emojis.basePath() + '/' + imagePath(emoji)

    emoji.toHTML = ->
      Emojis.template(this)

