Created
May 10, 2012 13:18
-
-
Save Meettya/2652928 to your computer and use it in GitHub Desktop.
Internal method for object merge, adopted form obj.merge from Sugar.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### | |
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