Skip to content

Instantly share code, notes, and snippets.

@indiejoseph
Last active August 29, 2015 14:01
Show Gist options
  • Save indiejoseph/89b295250d35098db608 to your computer and use it in GitHub Desktop.
Save indiejoseph/89b295250d35098db608 to your computer and use it in GitHub Desktop.
Coffeescript helpers`
sum = (arr) -> arr.reduce ((a,b) -> a+b), 0
cmp = (x, y) -> (if x > y then 1 else (if x < y then -1 else 0))
keys = Object.keys(list).sort((a,b)-> list[b]-list[a]) # sort by object value DESC
# initial array with default value
_.range(3).map(function () { return 'a' })
# string to bigram array
toBigrams = (str) ->
oneGrams = str.split('')
bigrams = []
for i in [1...oneGrams.length]
bigrams.push [oneGrams[i-1], oneGrams[i]]
(if bigrams.length then bigrams else oneGrams)
# zero pad
String('00'+3).slice -2 # output: 03
String.prototype.replaceHtmlEntites = () ->
s = @
translateRe = /&(nbsp|amp|quot|lt|gt);/g
translate =
'nbsp': ' '
'amp' : '&'
'quot': '"'
'lt' : '<'
'gt' : '>'
return s.replace translateRe, (match, entity) ->
return translate[entity]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment