Last active
August 29, 2015 14:13
-
-
Save Andreyco/2b5036aa4c8c3abeb6ba to your computer and use it in GitHub Desktop.
Deep object extend
This file contains hidden or 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
# Check if obj is realy object. | |
# Note: returns 'false' for 'null' | |
# (unlike 'typeof null' which returns 'object') | |
isObject = (obj) -> | |
type = typeof obj | |
type is 'function' || type is 'object' && !!obj | |
# Copy properties from source object | |
# to target object. | |
setProperties = (obj, source) -> | |
for own prop, value of source | |
if isObject(obj[prop]) and isObject(source[prop]) | |
deepExtend(obj[prop], value) | |
else | |
obj[prop] = value | |
# Define function to global scope | |
window.deepExtend = (obj) -> | |
return obj if not isObject(obj) | |
for i in [1...arguments.length] by 1 | |
setProperties(obj, arguments[i]) | |
obj |
This file contains hidden or 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
(function() { | |
var isObject, setProperties; | |
// Check if obj is realy object. | |
// Note: returns 'false' for 'null' | |
// (unlike 'typeof null' which returns 'object') | |
isObject = function(obj) { | |
var type; | |
type = typeof obj; | |
return type === 'function' || type === 'object' && !!obj; | |
}; | |
// Copy properties from source object | |
// to target object. | |
setProperties = function(obj, source) { | |
var prop, value, _results; | |
_results = []; | |
for (prop in source) { | |
if (!__hasProp.call(source, prop)) continue; | |
value = source[prop]; | |
if (isObject(obj[prop]) && isObject(source[prop])) { | |
_results.push(deepExtend(obj[prop], value)); | |
} else { | |
_results.push(obj[prop] = value); | |
} | |
} | |
return _results; | |
}; | |
// Define function to global scope | |
window.deepExtend = function(obj) { | |
var i, _i, _ref; | |
if (!isObject(obj)) { | |
return obj; | |
} | |
for (i = _i = 1, _ref = arguments.length; _i < _ref; i = _i += 1) { | |
setProperties(obj, arguments[i]); | |
} | |
return obj; | |
}; | |
})(); |
This file contains hidden or 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
// | |
// Usage | |
// deepExtend(source, obj1, [obj2], ... [objN]) | |
// | |
var user = { | |
first_name: "John", | |
email: { | |
main: '[email protected]' | |
} | |
} | |
user = deepExtend(user, {last_name: 'Doe'}, {email:{work: '[email protected]'}}) | |
// Will result into | |
{ | |
first_name: 'John', | |
last_name: 'Doe', | |
email: { | |
main: '[email protected]', | |
work: '[email protected]' | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment