Last active
June 9, 2016 13:23
-
-
Save stasgavrylov/67c4eedf6f5b7eacac75c19af8c2827d 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
function assign(target, ...sources) { | |
if (target == null) throw new TypeError | |
let to = Object(target) | |
if (!sources.length) return to | |
sources.forEach(nextSource => { | |
if (nextSource == null) return | |
let from = Object(nextSource) | |
let keys = Reflect.ownKeys(from) | |
keys.forEach(nextKey => { | |
let desc = Object.getOwnPropertyDescriptor(from, nextKey) | |
if (desc && desc.enumerable) | |
to[nextKey] = from[nextKey] | |
}) | |
}) | |
return to | |
} | |
function any(...args) | |
{ | |
return { | |
[Symbol.hasInstance]: (v) => args.some(arg => v instanceof arg) | |
} | |
} | |
function deepAssign(target, ...sources) { | |
if (target == null) throw new TypeError | |
let to = Object(target) | |
if (!sources.length) return to | |
sources.forEach(nextSource => { | |
if (nextSource == null) return | |
let from = Object(nextSource) | |
let keys = Reflect.ownKeys(from) | |
keys.forEach(nextKey => { | |
let desc = Object.getOwnPropertyDescriptor(from, nextKey) | |
if (desc && desc.enumerable) { | |
let val = from[nextKey] | |
if (val === Object(val)) | |
to[nextKey] = val instanceof any(RegExp, Date, Map, Set) | |
? deepAssign(new val.constructor(val), val) | |
: deepAssign(new val.constructor(), val) | |
else | |
to[nextKey] = val | |
} | |
}) | |
}) | |
return to | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment