Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
converted some underscore methods to coffeescript
# Returns only the objects that the callback returns true to
# replaces the underscore 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]
# Replaces underscores _.isEmpty function
empty = (obj) ->
(return false for own prop of obj)
# 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
object[key] = extend object[key], val
flat_array= ['foo', 'bar', 'moo', 'cow']
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment