1 line
26 KiB
Text
1 line
26 KiB
Text
![]() |
{"version":3,"file":"index.cjs","sources":["../../../../src/utils.ts","../../../../src/copier.ts","../../../../src/index.ts"],"sourcesContent":["export interface Cache {\n has: (value: any) => boolean;\n set: (key: any, value: any) => void;\n get: (key: any) => any;\n}\n\nconst { toString: toStringFunction } = Function.prototype;\nconst { create } = Object;\nconst { toString: toStringObject } = Object.prototype;\n\n/**\n * @classdesc Fallback cache for when WeakMap is not natively supported\n */\nclass LegacyCache {\n private _keys: any[] = [];\n private _values: any[] = [];\n\n has(key: any) {\n return !!~this._keys.indexOf(key);\n }\n\n get(key: any) {\n return this._values[this._keys.indexOf(key)];\n }\n\n set(key: any, value: any) {\n this._keys.push(key);\n this._values.push(value);\n }\n}\n\nfunction createCacheLegacy(): Cache {\n return new LegacyCache();\n}\n\nfunction createCacheModern(): Cache {\n return new WeakMap();\n}\n\n/**\n * Get a new cache object to prevent circular references.\n */\nexport const createCache =\n typeof WeakMap !== 'undefined' ? createCacheModern : createCacheLegacy;\n\n/**\n * Get an empty version of the object with the same prototype it has.\n */\nexport function getCleanClone(prototype: any): any {\n if (!prototype) {\n return create(null);\n }\n\n const Constructor = prototype.constructor;\n\n if (Constructor === Object) {\n return prototype === Object.prototype ? {} : create(prototype);\n }\n\n if (\n Constructor &&\n ~toStringFunction.call(Constructor).indexOf('[native code]')\n ) {\n try {\n return new Constructor();\n } catch {}\n }\n\n return create(prototype);\n}\n\nfunction getRegExpFlagsLegacy(regExp: RegExp): string {\n let flags = '';\n\n if (regExp.global) {\n flags += 'g';\n }\n\n if (regExp.ignoreCase) {\n flags += 'i';\n }\n\n if (regExp.multiline) {\n flags += 'm';\n }\n\n if (regExp.unicode) {\n flags += 'u';\n }\n\n if (regExp.sticky) {\n flags += 'y';\n }\n\n return flags;\n}\n\nfunction getRegExpFlagsModern(regExp: RegExp): string {\n return regExp.flags;\n}\n\n/**\n * Get the flags to apply to the copied regexp.\n */\nexport const getRegExpFlags =\n /test/g.flags === 'g' ? getRegExpFlagsModern : getRegExpFlagsLegacy;\n\nfunction getTagLegacy(value: any): string {\n const type = toStringObject.call(value);\n\n return type.substring(8, type.length - 1);\n}\n\nfunction getTagModern(value: any): string {\n return value[Symbol.toStringTag] || getTagLegacy(value);\n}\n\n/**\n * Get the tag of the value passed, so that the correct copier can be used.\n */\nexport const getTag =\n typeof Symbol !== 'undefined' ? getTagModern : getTagLegacy;\n","import { getCleanClone, getRegExpFlags } from './utils';\n\nimport type { Cache } from './utils';\n\nexport type InternalCopier<Value> = (value: Value, state: State) => Value;\n\nexport interface State {\n Constructor: any;\n cache: Cache;\n copier: InternalCopier<any>;\n prototype: any;\n}\n\nconst {\n defineProperty,\n getOwnPropertyDescriptor,\n getOwnPropertyNames,\n getOwnPropertySymbols,\n} = Object;\nconst { hasOwnProperty, propertyIsEnumerable } = Object.prototype;\n\nconst SUPPORTS_SYMBOL = typeof getOwnPropertySymbols === 'function';\n\nfunction getStrictPropertiesModern(object: any): Array<string | symbol> {\n return (getOwnPropertyNames(object) as Array<string | symbol>).concat(\n getOwnPropertySymbols(object)\n );\n}\n\n/**\n * Get the properites used when copying objects strictly. This includes both keys and symbols.\n */\nconst getStrictProperties = SUPPORTS_SYMBOL\n ? getStrictPropertiesModern\n : getOwnPropertyNames;\n\n/**\n * Striclty copy all properties contained on the object.\n */\nfunction copyOwnPropertiesStrict<Value>(\n value: Value,\n clone: Value,\n state: State\n): Value {\n const properties = getStrictProperties(value);\n\n for (\n let index = 0, length = properties.length, property, descriptor;\n index < length;\n ++index\n ) {\n property = properties[index];\n\n if (propert
|