Last active
February 18, 2020 21:50
-
-
Save mohdovais/be3b86cb775577f008476bd5ca807d9c to your computer and use it in GitHub Desktop.
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
/** | |
* Assign nested properties from `copy` to `obj` | |
* @template O, C object and copy | |
* @param {O} obj The object to copy properties to | |
* @param {C} copy The object to copy properties from | |
* @returns {O & C} a new object | |
*/ | |
function assign(obj, copy) { | |
var draft = {}; | |
var O = Object; | |
var objectKeys = O.keys; | |
var isObject = object => | |
O.prototype.toString.call(object) === "[object Object]"; | |
objectKeys(obj).forEach(function(key) { | |
draft[key] = obj[key]; | |
}); | |
objectKeys(copy).forEach(function(key) { | |
var ck = copy[key]; | |
var dk = draft[key]; | |
draft[key] = isObject(ck) && isObject(dk) ? assign(dk, ck) : ck; | |
}); | |
return draft; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment