From 4a0500d4934b5f6b5a32841ad74d3bbd5717b2af Mon Sep 17 00:00:00 2001 From: Tony Quetano Date: Mon, 26 Sep 2022 09:34:39 -0400 Subject: [PATCH] reduce code size --- index.d.ts | 12 ++---------- src/index.ts | 52 +++++++++++++++++++++++++--------------------------- 2 files changed, 27 insertions(+), 37 deletions(-) diff --git a/index.d.ts b/index.d.ts index f4d89c0..834a4ec 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,10 +1,2 @@ -export interface Cache { - _keys?: any[]; - _values?: any[]; - has: (value: any) => boolean; - set: (key: any, value: any) => void; - get: (key: any) => any; -} - -export const copy: (value: Value) => Value; -export const copyStrict: (value: Value) => Value; +export const copy: (value: Value) => Value; +export const copyStrict: (value: Value) => Value; diff --git a/src/index.ts b/src/index.ts index 75c7dd7..75ad05a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,12 +8,6 @@ import { import type { Cache } from './utils'; -export interface Options { - isStrict?: boolean; -} - -export interface StrictOptions extends Omit {} - type GetArrayClone = typeof getArrayCloneLoose | typeof getObjectCloneStrict; type GetObjectClone = typeof getObjectCloneLoose | typeof getObjectCloneStrict; @@ -21,6 +15,26 @@ const { isArray } = Array; const { getPrototypeOf } = Object; const { toString } = Object.prototype; +const ARRAY_BUFFER_OBJECT_CLASSES: Record = { + ['[object ArrayBuffer]']: true, + ['[object Float32Array]']: true, + ['[object Float64Array]']: true, + ['[object Int8Array]']: true, + ['[object Int16Array]']: true, + ['[object Int32Array]']: true, + ['[object Uint8Array]']: true, + ['[object Uint8ClampedArray]']: true, + ['[object Uint16Array]']: true, + ['[object Uint32Array]']: true, + ['[object Uint64Array]']: true, +}; +const UNCOPIABLE_OBJECT_CLASSES: Record = { + ['[object Error]']: true, + ['[object Promise]']: true, + ['[object WeakMap]']: true, + ['[object WeakSet]']: true, +}; + function performCopy( value: Value, getObjectClone: GetObjectClone, @@ -108,19 +122,7 @@ function performCopy( } // array buffers - if ( - objectClass === '[object Float32Array]' || - objectClass === '[object Float64Array]' || - objectClass === '[object Int8Array]' || - objectClass === '[object Int16Array]' || - objectClass === '[object Int32Array]' || - objectClass === '[object Uint8Array]' || - objectClass === '[object Uint8ClampedArray]' || - objectClass === '[object Uint16Array]' || - objectClass === '[object Uint32Array]' || - objectClass === '[object Uint64Array]' || - objectClass === '[object ArrayBuffer]' - ) { + if (ARRAY_BUFFER_OBJECT_CLASSES[objectClass]) { const clone = value.slice(0); cache.set(value, clone); @@ -128,16 +130,12 @@ function performCopy( return clone; } - // if the value cannot / should not be cloned, don't + // if the value cannot / should not be copied deeply, return the reference if ( // promise-like typeof value.then === 'function' || - // errors - objectClass === '[object Error]' || - // weakmaps - objectClass === '[object WeakMap]' || - // weaksets - objectClass === '[object WeakSet]' + // object classes which cannot be introspected for copy + UNCOPIABLE_OBJECT_CLASSES[objectClass] ) { return value; } @@ -161,6 +159,6 @@ export function copy(value: Value): Value { * are maintained. All properties (including non-enumerable ones) are copied with their * original property descriptors on both objects and arrays. */ -export function copyStrict(value: any) { +export function copyStrict(value: Value): Value { return performCopy(value, getObjectCloneStrict, getObjectCloneStrict); }