 
 Emojis = new Mongo.Collection 'emojis',
   transform: (emoji) ->
-    
+
     # Shortcut helpers
 
     emoji.path = Emojis.basePath() + '/' + imagePath(emoji)
-    
+
     emoji.toHTML = ->
       Emojis.template(this)
 

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._ensureIndex(alias: 1)
  Emojis._ensureIndex(ascii: 1)


# General parse function.
#
# Parses `text` and returns emoji objects from :shortnames: and ASCII
# smileys (:D).
#
# Optionally takes a function which can transform the return value.
parse = (text, fn) ->
  check text, String
  check fn, Match.Optional(Function)

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

    emoji = Emojis.findOne(ascii: smiley)

    if emoji
      return if fn then fn(emoji) else emoji
    else
      return match

  .replace shortnameRegexp, (match, alias) ->
    emoji = Emojis.findOne(alias: alias)

    if emoji
      return if fn then fn(emoji) else emoji
    else
      return match

Emojis._basePath = '/images/emojis'

