expressmultithread
Version:
Fast, light-weight and low dependency [Express.js](https://www.npmjs.com/package/express) multithreaded router.
60 lines • 1.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.deepCopy = void 0;
function deepCopy(obj, hash = new WeakMap()) {
if (obj === null || typeof obj !== "object")
return obj;
if (hash.has(obj))
return hash.get(obj);
if (obj instanceof Date)
return new Date(obj);
if (obj instanceof RegExp)
return new RegExp(obj);
if (obj instanceof Array) {
let copy = [];
hash.set(obj, copy);
copy = obj.map((item) => deepCopy(item, hash));
return copy;
}
if (obj instanceof Map) {
let copy = new Map();
hash.set(obj, copy);
return new Map(Array.from(obj, ([key, value]) => [deepCopy(key, hash), deepCopy(value, hash)]));
}
if (obj instanceof Set) {
let copy = new Set();
hash.set(obj, copy);
return new Set(Array.from(obj, (value) => deepCopy(value, hash)));
}
if (obj instanceof WeakMap) {
let copy = new WeakMap();
hash.set(obj, copy);
for (let [key, value] of obj) {
copy.set(deepCopy(key, hash), deepCopy(value, hash));
}
return copy;
}
if (obj instanceof WeakSet) {
let copy = new WeakSet();
hash.set(obj, copy);
for (let value of obj) {
copy.add(deepCopy(value, hash));
}
return copy;
}
if (obj instanceof Buffer)
return Buffer.from(obj);
if (obj instanceof Object) {
let copy = Object.create(Object.getPrototypeOf(obj));
hash.set(obj, copy);
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
copy[key] = deepCopy(obj[key], hash);
}
}
return copy;
}
throw new Error("Unable to copy obj! Its type isn't supported.");
}
exports.deepCopy = deepCopy;
//# sourceMappingURL=deepCopy.js.map