CoolFace
Datasetpublic

basant307/AI_Governance_Project

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
0likes48downloads
cloneObject.ts37 linesDownload Raw Back to utils
1import { Logger } from '@open-draft/logger'2 3const logger = new Logger('cloneObject')4 5function isPlainObject(obj?: Record<string, any>): boolean {6  logger.info('is plain object?', obj)7 8  if (obj == null || !obj.constructor?.name) {9    logger.info('given object is undefined, not a plain object...')10    return false11  }12 13  logger.info('checking the object constructor:', obj.constructor.name)14  return obj.constructor.name === 'Object'15}16 17export function cloneObject<ObjectType extends Record<string, any>>(18  obj: ObjectType19): ObjectType {20  logger.info('cloning object:', obj)21 22  const enumerableProperties = Object.entries(obj).reduce<Record<string, any>>(23    (acc, [key, value]) => {24      logger.info('analyzing key-value pair:', key, value)25 26      // Recursively clone only plain objects, omitting class instances.27      acc[key] = isPlainObject(value) ? cloneObject(value) : value28      return acc29    },30    {}31  )32 33  return isPlainObject(obj)34    ? enumerableProperties35    : Object.assign(Object.getPrototypeOf(obj), enumerableProperties)36}37 
basant307/AI_Governance_Project · CoolFace