File size: 888 Bytes
bc20498 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
// adapted from klona v2.0.4 - https://github.com/lukeed/klona
// (c) Luke Edwards, under MIT License
// The sole modification is to skip function values in objects when cloning, so we don't break tests.
export function clone(val) {
let k, out, tmp;
if (Array.isArray(val)) {
out = Array((k = val.length));
while (k--) out[k] = (tmp = val[k]) && typeof tmp === 'object' ? clone(tmp) : tmp;
return out;
}
if (Object.prototype.toString.call(val) === '[object Object]') {
out = {}; // null
for (k in val) {
if (k === '__proto__') {
Object.defineProperty(out, k, {
value: clone(val[k]),
configurable: true,
enumerable: true,
writable: true
});
} else if (typeof val[k] !== 'function') {
// MODIFICATION: skip functions
out[k] = (tmp = val[k]) && typeof tmp === 'object' ? clone(tmp) : tmp;
}
}
return out;
}
return val;
}
|