Skip to content

Instantly share code, notes, and snippets.

@stasgavrylov
Last active June 9, 2016 13:23
Show Gist options
  • Save stasgavrylov/67c4eedf6f5b7eacac75c19af8c2827d to your computer and use it in GitHub Desktop.
Save stasgavrylov/67c4eedf6f5b7eacac75c19af8c2827d to your computer and use it in GitHub Desktop.
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