Sha256: f7d25da2635fe46a7617561f4320d5186ba9a9b281443f143874db289bd9c0bb

Contents?: true

Size: 1.03 KB

Versions: 26

Compression:

Stored size: 1.03 KB

Contents

import { Logger } from '@open-draft/logger'

const logger = new Logger('cloneObject')

function isPlainObject(obj?: Record<string, any>): boolean {
  logger.info('is plain object?', obj)

  if (obj == null || !obj.constructor?.name) {
    logger.info('given object is undefined, not a plain object...')
    return false
  }

  logger.info('checking the object constructor:', obj.constructor.name)
  return obj.constructor.name === 'Object'
}

export function cloneObject<ObjectType extends Record<string, any>>(
  obj: ObjectType
): ObjectType {
  logger.info('cloning object:', obj)

  const enumerableProperties = Object.entries(obj).reduce<Record<string, any>>(
    (acc, [key, value]) => {
      logger.info('analyzing key-value pair:', key, value)

      // Recursively clone only plain objects, omitting class instances.
      acc[key] = isPlainObject(value) ? cloneObject(value) : value
      return acc
    },
    {}
  )

  return isPlainObject(obj)
    ? enumerableProperties
    : Object.assign(Object.getPrototypeOf(obj), enumerableProperties)
}

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
clapton-0.0.26 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/utils/cloneObject.ts
clapton-0.0.25 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/utils/cloneObject.ts
clapton-0.0.24 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/utils/cloneObject.ts
clapton-0.0.23 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/utils/cloneObject.ts
clapton-0.0.22 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/utils/cloneObject.ts
clapton-0.0.21 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/utils/cloneObject.ts
clapton-0.0.20 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/utils/cloneObject.ts
clapton-0.0.19 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/utils/cloneObject.ts
clapton-0.0.18 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/utils/cloneObject.ts
clapton-0.0.17 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/utils/cloneObject.ts
clapton-0.0.16 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/utils/cloneObject.ts
clapton-0.0.15 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/utils/cloneObject.ts
clapton-0.0.14 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/utils/cloneObject.ts
clapton-0.0.13 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/utils/cloneObject.ts
clapton-0.0.12 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/utils/cloneObject.ts
clapton-0.0.11 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/utils/cloneObject.ts
clapton-0.0.10 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/utils/cloneObject.ts
clapton-0.0.9 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/utils/cloneObject.ts
clapton-0.0.8 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/utils/cloneObject.ts
clapton-0.0.7 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/utils/cloneObject.ts