dashp
Version:
Utilities for monadic promises.
26 lines (20 loc) • 760 B
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _is = require("./is");
const clone = (obj, hash = new WeakMap()) => {
if (Object(obj) !== obj) return obj; // primitives
if (hash.has(obj)) return hash.get(obj); // cyclic references
let result;
if ((0, _is.isArray)(obj)) result = [];else if (obj.constructor) result = new obj.constructor();else result = Object.create(null);
hash.set(obj, result);
if (obj instanceof Map) Array.from(obj, ([key, val]) => result.set(key, clone(val, hash)));
return Object.assign(result, ...Object.keys(obj).map(key => ({
[key]: clone(obj[key], hash)
})));
};
var _default = clone;
exports.default = _default;
//# sourceMappingURL=clone.js.map