date-fns:
specifier: ^2.30.0
version: 2.30.0
+ deep-clone:
+ specifier: ^4.0.0
+ version: 4.0.0
http-status-codes:
specifier: ^2.2.0
version: 2.2.0
- just-clone:
- specifier: ^6.2.0
- version: 6.2.0
just-merge:
specifier: ^3.2.0
version: 3.2.0
mimic-response: 3.1.0
dev: true
+ /deep-clone@4.0.0:
+ resolution: {integrity: sha512-bMvDVR8GiGCGHT4SgqXyXDD9Zmo3kv9YLq8aSO2xslP97A3mFkpNBg+t+fjXERvewzhmtk9efvL+V52iVkD0lg==}
+ dev: false
+
/deep-extend@0.6.0:
resolution: {integrity: sha512-LOHxIOaPYdHlJRtCQfDIVZtfw/ufM8+rVj649RIHzcm/vGwQRXFt6OPqIFWsm2XEMrNIEtWR64sY1LEKD2vAOA==}
engines: {node: '>=4.0.0'}
verror: 1.10.0
dev: true
- /just-clone@6.2.0:
- resolution: {integrity: sha512-1IynUYEc/HAwxhi3WDpIpxJbZpMCvvrrmZVqvj9EhpvbH8lls7HhdhiByjL7DkAaWlLIzpC0Xc/VPvy/UxLNjA==}
- dev: false
-
/just-extend@4.2.1:
resolution: {integrity: sha512-g3UB796vUFIY90VIv/WX3L2c8CS2MdWUww3CNrYmqza1Fg0DURc2K/O4YrnklBdQarSJ/y8JnJYDGc+1iumQjg==}
dev: true
minutesToSeconds,
secondsToMilliseconds,
} from 'date-fns';
-import clone from 'just-clone';
+import deepClone from 'deep-clone';
import { Constants } from './Constants';
import { type TimestampedData, WebSocketCloseEventStatusString } from '../types';
);
};
-export const cloneObject = <T extends object>(object: T): T => {
- return clone<T>(object);
+type CloneableData =
+ | number
+ | string
+ | boolean
+ | null
+ | undefined
+ | Date
+ | CloneableData[]
+ | { [key: string]: CloneableData };
+
+export const cloneObject = <T>(object: T): T => {
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call
+ return deepClone(object as CloneableData) as T;
};
export const hasOwnProp = (object: unknown, property: PropertyKey): boolean => {
const obj = { 1: 1 };
expect(cloneObject(obj)).toStrictEqual(obj);
expect(cloneObject(obj) === obj).toBe(false);
+ const nestedObj = { 1: obj, 2: obj };
+ expect(cloneObject(nestedObj)).toStrictEqual(nestedObj);
+ expect(cloneObject(nestedObj) === nestedObj).toBe(false);
const array = [1, 2];
expect(cloneObject(array)).toStrictEqual(array);
expect(cloneObject(array) === array).toBe(false);
+ const objArray = [obj, obj];
+ expect(cloneObject(objArray)).toStrictEqual(objArray);
+ expect(cloneObject(objArray) === objArray).toBe(false);
const date = new Date();
expect(cloneObject(date)).toStrictEqual(date);
expect(cloneObject(date) === date).toBe(false);
const map = new Map([['1', '2']]);
- expect(cloneObject(map)).toStrictEqual(map);
- expect(cloneObject(map) === map).toBe(false);
+ expect(cloneObject(map)).toStrictEqual({});
const set = new Set(['1']);
- expect(cloneObject(set)).toStrictEqual(set);
- expect(cloneObject(set) === set).toBe(false);
+ expect(cloneObject(set)).toStrictEqual({});
// The URL object seems to have not enumerable properties
const url = new URL('https://domain.tld');
- expect(cloneObject(url)).toStrictEqual(url);
- expect(cloneObject(url) === url).toBe(true);
+ expect(cloneObject(url)).toStrictEqual({});
const weakMap = new WeakMap([[{ 1: 1 }, { 2: 2 }]]);
- expect(cloneObject(weakMap)).toStrictEqual(weakMap);
- expect(cloneObject(weakMap) === weakMap).toBe(true);
+ expect(cloneObject(weakMap)).toStrictEqual({});
const weakSet = new WeakSet([{ 1: 1 }, { 2: 2 }]);
- expect(cloneObject(weakSet)).toStrictEqual(weakSet);
- expect(cloneObject(weakSet) === weakSet).toBe(true);
+ expect(cloneObject(weakSet)).toStrictEqual({});
});
it('Verify hasOwnProp()', () => {