Skip to content

Instantly share code, notes, and snippets.

@Meettya
Created May 10, 2012 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Meettya/2652928 to your computer and use it in GitHub Desktop.
Save Meettya/2652928 to your computer and use it in GitHub Desktop.
Internal method for object merge, adopted form obj.merge from Sugar.js
###
Internal method for object merge, adopted form obj.merge from Sugar.js
###
_merge: (target, source, deep, resolve) ->
###
Some checking at start, we are dont need merge void objects at all
###
unless target? and source?
throw new Error """
null object given to _merge
target |#{target}|
source |#{source}|
"""
###
Strings cannot be reliably merged thanks to their properties not being enumerable in < IE8
###
throw new Error "cant merge with String type source |#{source}|" if _.isString source
for own key, val of source
# Conflict!
unless target[key]
continue if resolve is false # Do not merge.
# Use the result of the callback as the result
if _.isFunction resolve
val = resolve.call source, key, target[key], source[key]
if deep is true and _.isObject val
unless target[key]
target[key] = if _.isArray val then [] else {}
@_merge target[key], source[key], deep
continue
target[key] = val
target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment