Skip to content

Commit

Permalink
reduce code size
Browse files Browse the repository at this point in the history
  • Loading branch information
planttheidea committed Sep 26, 2022
1 parent 34306e2 commit 4a0500d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 37 deletions.
12 changes: 2 additions & 10 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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 = any>(value: Value) => Value;
export const copyStrict: <Value = any>(value: Value) => Value;
export const copy: <Value>(value: Value) => Value;
export const copyStrict: <Value>(value: Value) => Value;
52 changes: 25 additions & 27 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,33 @@ import {

import type { Cache } from './utils';

export interface Options {
isStrict?: boolean;
}

export interface StrictOptions extends Omit<Options, 'isStrict'> {}

type GetArrayClone = typeof getArrayCloneLoose | typeof getObjectCloneStrict;
type GetObjectClone = typeof getObjectCloneLoose | typeof getObjectCloneStrict;

const { isArray } = Array;
const { getPrototypeOf } = Object;
const { toString } = Object.prototype;

const ARRAY_BUFFER_OBJECT_CLASSES: Record<string, boolean> = {
['[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<string, boolean> = {
['[object Error]']: true,
['[object Promise]']: true,
['[object WeakMap]']: true,
['[object WeakSet]']: true,
};

function performCopy<Value>(
value: Value,
getObjectClone: GetObjectClone,
Expand Down Expand Up @@ -108,36 +122,20 @@ function performCopy<Value>(
}

// 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);

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;
}
Expand All @@ -161,6 +159,6 @@ export function copy<Value>(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): Value {
return performCopy(value, getObjectCloneStrict, getObjectCloneStrict);
}

0 comments on commit 4a0500d

Please sign in to comment.