UNPKG

@web3r/flowerkit

Version:

Tree-shakable JavaScript and TypeScript utility library for frontend/browser apps: DOM, events, arrays, objects, strings, date, JSON, and network helpers (ESM/CJS, SSR-friendly).

1 lines 1.52 kB
{"version":3,"file":"index.cjs","sources":[""],"sourcesContent":["export type TGetCopyOfObjArgs = Parameters<typeof getCopyOfObj>;\r\n\r\nexport type TGetCopyOfObjReturn = ReturnType<typeof getCopyOfObj>;\r\n\r\n/**\r\n * Gets a deep copy/clone of an object/array without a reference to the original object\r\n * @param obj{Object|Array} source object (array)\r\n * @returns {Object|Array}\r\n * @see https://developer.mozilla.org/en-US/docs/Glossary/Deep_copy\r\n * @example\r\n * // How to make a deep clone of an object?\r\n * const originalObject = {\r\n * value: 1,\r\n * }\r\n * const copy = getCopyOfObj(originalObject);\r\n * copy.value = 2;\r\n * console.log(originalObject.value === copy.value) // false\r\n */\r\nexport const getCopyOfObj = <T>(obj: T): T => {\r\n if (obj === null || typeof obj !== \"object\" || obj instanceof Date) {\r\n return obj;\r\n }\r\n\r\n const objCopy: any = Array.isArray(obj) ? [] : {};\r\n\r\n return Object.keys(obj as Record<string, unknown>).reduce((nestedObj: any, key) => {\r\n nestedObj[key] = getCopyOfObj((obj as Record<string, unknown>)[key] as unknown);\r\n return nestedObj;\r\n }, objCopy);\r\n};\r\n"],"names":["getCopyOfObj","obj","Date","objCopy","Array","isArray","Object","keys","reduce","nestedObj","key"],"mappings":";;;;;;;;;;;;;;GAkBO,MAAMA,aAAmBC,MAC9B,GAAIA,MAAQ,aAAeA,MAAQ,UAAYA,eAAeC,KAC5D,OAAOD,IAGT,MAAME,QAAeC,MAAMC,QAAQJ,KAAO,GAAK,CAAA,EAE/C,OAAOK,OAAOC,KAAKN,KAAgCO,OAAO,CAACC,UAAgBC,OACzED,UAAUC,KAAOV,aAAcC,IAAgCS,MAC/D,OAAOD,WACNN"}