Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial setup
Browse files Browse the repository at this point in the history
planttheidea committed Sep 23, 2022
1 parent 7a4d7e6 commit e5e5eb0
Showing 10 changed files with 132 additions and 105 deletions.
60 changes: 27 additions & 33 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,33 @@
declare namespace FastCopy {
export type Realm = Record<string, any>;

export interface Cache {
_keys?: any[];
_values?: any[];
has: (value: any) => boolean;
set: (key: any, value: any) => void;
get: (key: any) => any;
}

export type Copier = <Value = any>(value: Value, cache: Cache) => Value;

export type ObjectCloner = <Value>(
object: Value,
realm: Realm,
handleCopy: Copier,
cache: Cache,
) => Value;

export type Options = {
isStrict?: boolean;
realm?: Realm;
};
export interface Cache {
_keys?: any[];
_values?: any[];
has: (value: any) => boolean;
set: (key: any, value: any) => void;
get: (key: any) => any;
}

declare function copy<Value = any>(
value: Value,
options?: FastCopy.Options,
): Value;
export interface Copy {
<Value = any>(value: Value, options?: Options): Value;

declare namespace copy {
function strictCopy<Value = any>(
value: Value,
options?: FastCopy.Options,
): Value;
strict<Value = any>(value: Value, options?: Options): Value;
}

export type InternalCopier = <Value = any>(value: Value, cache: Cache) => Value;

export type ObjectCloner = <Value>(
object: Value,
realm: Realm,
handleCopy: InternalCopier,
cache: Cache
) => Value;

export interface Options {
isStrict?: boolean;
realm?: Realm;
}

export type Realm = Record<string, any>;

declare const copy: Copy;

export default copy;
12 changes: 6 additions & 6 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module.exports = {
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
roots: ["<rootDir>"],
testRegex: "/__tests__/.*\\.(ts|tsx)$",
export default {
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
roots: ['<rootDir>'],
testRegex: '/__tests__/.*\\.(ts|tsx)$',
transform: {
"\\.(ts|tsx)$": "ts-jest"
'\\.(ts|tsx)$': 'ts-jest',
},
verbose: true
verbose: true,
};
29 changes: 24 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
"contributors": [
"Dariusz Rzepka <[email protected]>"
],
"browser": "dist/fast-copy.js",
"browser": "dist/umd/index.js",
"bugs": {
"url": "https://github.com/planttheidea/fast-copy/issues"
},
@@ -44,6 +44,22 @@
"webpack-cli": "^4.10.0",
"webpack-dev-server": "^4.11.1"
},
"exports": {
".": {
"import": {
"types": "./dist/esm/types/index.d.ts",
"default": "./dist/esm/index.mjs"
},
"require": {
"types": "./dist/cjs/types/index.d.ts",
"default": "./dist/cjs/index.cjs"
},
"default": {
"types": "./dist/umd/types/index.d.ts",
"default": "./dist/umd/index.js"
}
}
},
"homepage": "https://github.com/planttheidea/fast-copy#readme",
"keywords": [
"clone",
@@ -52,16 +68,18 @@
"fast"
],
"license": "MIT",
"main": "dist/fast-copy.cjs.js",
"module": "dist/fast-copy.esm.js",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
"name": "fast-copy",
"repository": {
"type": "git",
"url": "git+https://github.com/planttheidea/fast-copy.git"
},
"scripts": {
"benchmark": "npm run build && node benchmark/index.js",
"build": "NODE_ENV=production rollup -c",
"benchmark": "npm run build:files && node benchmark/index.js",
"build": "npm run build:files && npm run build:types",
"build:files": "NODE_ENV=production rollup -c",
"build:types": "tsc -p ./tsconfig.cjs.json && tsc -p ./tsconfig.esm.json",
"clean": "rimraf dist",
"dev": "NODE_ENV=development webpack-dev-server --config=webpack/webpack.config.js",
"dist": "npm run clean && npm run build",
@@ -77,6 +95,7 @@
"test:watch": "npm run test -- --watch",
"typecheck": "tsc src/*.ts --noEmit"
},
"type": "module",
"types": "index.d.ts",
"version": "2.1.7"
}
3 changes: 1 addition & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -54,8 +54,7 @@ const MINIFIED_CONFIG = {
...UMD_CONFIG,
output: {
...UMD_CONFIG.output,
file: pkg.browser.replace('.js', '.min.js'),
sourcemap: false,
file: pkg.browser.replace('umd', 'minified').replace('.js', '.min.js'),
},
plugins: [...UMD_CONFIG.plugins, terser()],
};
37 changes: 0 additions & 37 deletions src/fast-copy.d.ts

This file was deleted.

27 changes: 18 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
// utils
import {
createCache,
getObjectCloneLoose,
getObjectCloneStrict,
getRegExpFlags,
} from './utils';

import type { Cache, InternalCopier, Realm } from './utils';

export interface Copy {
<Value = any>(value: Value, options?: Options): Value;

strict<Value = any>(value: Value, options?: Options): Value;
}

export interface Options {
isStrict?: boolean;
realm?: Realm;
}

const { isArray } = Array;
const { getPrototypeOf } = Object;

const GLOBAL_THIS: FastCopy.Realm = (function () {
const GLOBAL_THIS: Realm = (function () {
if (typeof globalThis !== 'undefined') {
return globalThis;
}
@@ -52,7 +64,7 @@ const GLOBAL_THIS: FastCopy.Realm = (function () {
* @param [options.realm] the realm (this) value the value is copied from
* @returns the copied value
*/
function copy<Value>(value: Value, options?: FastCopy.Options): Value {
function copy<Value>(value: Value, options?: Options): Value {
// manually coalesced instead of default parameters for performance
const isStrict = !!(options && options.isStrict);
const realm = (options && options.realm) || GLOBAL_THIS;
@@ -67,10 +79,7 @@ function copy<Value>(value: Value, options?: FastCopy.Options): Value {
* @param value the value to copy
* @returns the copied value
*/
const handleCopy: FastCopy.Copier = (
value: any,
cache: FastCopy.Cache,
): any => {
const handleCopy: InternalCopier = (value: any, cache: Cache): any => {
if (!value || typeof value !== 'object') {
return value;
}
@@ -119,7 +128,7 @@ function copy<Value>(value: Value, options?: FastCopy.Options): Value {
if (value instanceof realm.RegExp) {
clone = new Constructor(
value.source,
value.flags || getRegExpFlags(value),
value.flags || getRegExpFlags(value)
);

clone.lastIndex = value.lastIndex;
@@ -222,7 +231,7 @@ copy.default = copy;
* @param [options.realm] the realm (this) value the value is copied from
* @returns the copied value
*/
copy.strict = function strictCopy(value: any, options?: FastCopy.Options) {
copy.strict = function strictCopy(value: any, options?: Options) {
return copy(value, {
isStrict: true,
realm: options ? options.realm : void 0,
45 changes: 32 additions & 13 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
export interface Cache {
_keys?: any[];
_values?: any[];
has: (value: any) => boolean;
set: (key: any, value: any) => void;
get: (key: any) => any;
}

export type InternalCopier = <Value = any>(value: Value, cache: Cache) => Value;

export type ObjectCloner = <Value>(
object: Value,
realm: Realm,
handleCopy: InternalCopier,
cache: Cache
) => Value;

export type Realm = Record<string, any>;

const { toString: toStringFunction } = Function.prototype;
const {
create,
@@ -20,9 +39,9 @@ const WEAK_MAP = typeof WeakMap === 'function';
*
* @returns the new cache object
*/
export const createCache = (() => {
export const createCache: () => Cache = (() => {
if (WEAK_MAP) {
return (): FastCopy.Cache => new WeakMap();
return () => new WeakMap();
}

class Cache {
@@ -43,7 +62,7 @@ export const createCache = (() => {
}
}

return (): FastCopy.Cache => new Cache();
return (): Cache => new Cache();
})();

/**
@@ -56,7 +75,7 @@ export const createCache = (() => {
* @param realm the realm the object resides in
* @returns the empty cloned object
*/
export const getCleanClone = (object: any, realm: FastCopy.Realm): any => {
export const getCleanClone = (object: any, realm: Realm): any => {
const prototype = object.__proto__ || getPrototypeOf(object);

if (!prototype) {
@@ -90,11 +109,11 @@ export const getCleanClone = (object: any, realm: FastCopy.Realm): any => {
* @param handleCopy the function that handles copying the object
* @returns the copied object
*/
export const getObjectCloneLoose: FastCopy.ObjectCloner = (
export const getObjectCloneLoose: ObjectCloner = (
object: any,
realm: FastCopy.Realm,
handleCopy: FastCopy.Copier,
cache: FastCopy.Cache,
realm: Realm,
handleCopy: InternalCopier,
cache: Cache
): any => {
const clone: any = getCleanClone(object, realm);

@@ -138,11 +157,11 @@ export const getObjectCloneLoose: FastCopy.ObjectCloner = (
* @param handleCopy the function that handles copying the object
* @returns the copied object
*/
export const getObjectCloneStrict: FastCopy.ObjectCloner = (
export const getObjectCloneStrict: ObjectCloner = (
object: any,
realm: FastCopy.Realm,
handleCopy: FastCopy.Copier,
cache: FastCopy.Cache,
realm: Realm,
handleCopy: InternalCopier,
cache: Cache
): any => {
const clone: any = getCleanClone(object, realm);

@@ -151,7 +170,7 @@ export const getObjectCloneStrict: FastCopy.ObjectCloner = (

const properties: (string | symbol)[] = SYMBOL_PROPERTIES
? getOwnPropertyNames(object).concat(
getOwnPropertySymbols(object) as unknown as string[],
getOwnPropertySymbols(object) as unknown as string[]
)
: getOwnPropertyNames(object);

8 changes: 8 additions & 0 deletions tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.declarations.json",
"compilerOptions": {
"declarationDir": "./dist/cjs/types",
"module": "CommonJS",
"outDir": "./dist/cjs"
}
}
8 changes: 8 additions & 0 deletions tsconfig.declarations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"declaration": true,
"emitDeclarationOnly": true
},
"include": ["src/*"]
}
8 changes: 8 additions & 0 deletions tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.declarations.json",
"compilerOptions": {
"declarationDir": "./dist/esm/types",
"module": "ESNext",
"outDir": "./dist/esm"
}
}

0 comments on commit e5e5eb0

Please sign in to comment.