@mfederczuk/deeptools
Version:
A set of utility functions that recursively operate on objects
1 lines • 2.64 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:true});exports.deepCopy=void 0;const utils_1=require("./_internal/utils");const deepFreeze_1=require("./deepFreeze");const initCopy=obj=>{if(obj instanceof Array)return new Array(obj.map(deepCopy));if(obj instanceof RegExp)return new RegExp(obj);if(obj instanceof Date)return new Date(obj);if(obj instanceof Map){const copiedEntries=[];for(const[key,value]of obj.entries()){copiedEntries.push([deepCopy(key),deepCopy(value)])}return new Map(copiedEntries)}if(obj instanceof Set){const copiedValues=[];for(const value of obj.values()){copiedValues.push(deepCopy(value))}return new Set(copiedValues)}if(obj instanceof Int8Array)return new Int8Array(obj);if(obj instanceof Uint8Array)return new Uint8Array(obj);if(obj instanceof Uint8ClampedArray)return new Uint8ClampedArray(obj);if(obj instanceof Int16Array)return new Int16Array(obj);if(obj instanceof Uint16Array)return new Uint16Array(obj);if(obj instanceof Int32Array)return new Int32Array(obj);if(obj instanceof Uint32Array)return new Uint32Array(obj);if(obj instanceof Float32Array)return new Float32Array(obj);if(obj instanceof Float64Array)return new Float64Array(obj);if(obj instanceof BigInt64Array)return new BigInt64Array(obj);if(obj instanceof BigUint64Array)return new BigUint64Array(obj);if(obj instanceof ArrayBuffer){const newBuffer=new ArrayBuffer(obj.byteLength);const origView=new Uint8Array(obj);const newView=new Uint8Array(newBuffer);newView.set(origView);return newBuffer}if(obj instanceof WeakMap)throw new TypeError("WeakMap objects cannot be copied");if(obj instanceof WeakSet)throw new TypeError("WeakSet objects cannot be copied");if(obj instanceof SharedArrayBuffer)throw new TypeError("SharedArrayBuffer objects cannot be copied");if(obj instanceof DataView)throw new TypeError("DataView objects cannot be copied");if(obj instanceof Promise)throw new TypeError("Promise objects cannot be copied");return Object.create(obj)};function deepCopy(obj){if(!(0,utils_1.isNonPrimitive)(obj)){return obj}if(typeof obj==="function"){throw new TypeError("Function objects cannot be copied")}const copy=initCopy(obj);for(const propertyKey of(0,utils_1.getPropertyKeys)(obj)){const propertyDescriptor=Object.getOwnPropertyDescriptor(obj,propertyKey);if("value"in propertyDescriptor){propertyDescriptor.value=deepCopy(propertyDescriptor.value)}Object.defineProperty(copy,propertyKey,propertyDescriptor)}const objPrototype=Object.getPrototypeOf(obj);if(Object.getPrototypeOf(copy)!==objPrototype){Object.setPrototypeOf(copy,objPrototype)}return copy}exports.deepCopy=deepCopy;(0,deepFreeze_1.deepFreeze)(deepCopy);