public
Created

converted some underscore methods to coffeescript

  • Download Gist
addons.coffee
CoffeeScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
# Returns only the objects that the callback returns true to
# replaces the underscore _.select method
filter = (obj, callback) ->
element for element in obj when callback(element)
 
# Deep clones an object, replaces underscores _.clone method
clone = (obj) ->
if not obj? or typeof obj isnt 'object'
return obj
newInstance = new obj.constructor()
for key of obj
newInstance[key] = clone obj[key]
newInstance
 
# Replaces underscores _.isEmpty function
empty = (obj) ->
(return false for own prop of obj)
true
 
# Replaces underscores _.extend function
extend = (object, extenders...) ->
return {} if not object?
for other in extenders
for own key, val of other
if not object[key]? or typeof val isnt "object"
object[key] = val
else
object[key] = extend object[key], val
object
 
###
flat_array= ['foo', 'bar', 'moo', 'cow']
Output:
foo:
bar:
moo: 'cow'
###
make_multi= (flat_array, obj= {}) ->
if not flat_array.length then return {}
if flat_array.length == 1 then flat_array[0] else
result = {}
result[flat_array[0]] = make_multi(flat_array[1..])
return result
 
# isNumber ('123'); // true
# isNumber (5); // true
# isNumber ('q345'); // false
isNumber= (o) ->
not isNaN(o-0)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.