From a49875e8088f19659f8b4e2202c916d3ac0f616e Mon Sep 17 00:00:00 2001 From: Mesqueeb Date: Sun, 15 Mar 2020 08:53:41 +0900 Subject: [PATCH] =?UTF-8?q?v2.0=20-=20only=20named=20exports=20?= =?UTF-8?q?=F0=9F=95=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 43 ++++++++++++++++++++----------------------- dist/index.cjs.js | 4 +++- dist/index.esm.js | 2 +- package.json | 14 ++++++-------- src/index.ts | 2 +- test/index.ts | 2 +- types/index.d.ts | 2 +- 7 files changed, 33 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 5c62d60..22eb218 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ An optimised way to copy'ing (cloning) an object or array. A small and simple in ## Motivation -I created this package because I tried a lot of similar packages that do copy'ing/cloning. But all had its quirks, and *all of them break things they are not supposed to break*... 😞 +I created this package because I tried a lot of similar packages that do copy'ing/cloning. But all had its quirks, and _all of them break things they are not supposed to break_... 😞 I was looking for: @@ -37,18 +37,19 @@ copy-anything will copy objects and nested properties, but only as long as they' ## Usage ```js -import copy from 'copy-anything' +import { copy } from 'copy-anything' -const original = {name: 'Ditto', type: {water: true}} +const original = { name: 'Ditto', type: { water: true } } const copy = copy(original) // now if we change a nested prop like the type: copy.type.water = false -copy.type.fire = true // new prop +copy.type.fire = true( + // new prop -// then the original object will still be the same: -(original.type.water === true) -(original.type.fire === undefined) + // then the original object will still be the same: + original.type.water === true +)(original.type.fire === undefined) ``` > Please note, by default copy-anything does not copy non-enumerable props. If you need to copy those, see the instructions further down below. @@ -58,16 +59,17 @@ copy.type.fire = true // new prop It will also clone arrays, **as well as objects inside arrays!** 😉 ```js -const original = [{name: 'Squirtle'}] +const original = [{ name: 'Squirtle' }] const copy = copy(original) // now if we change a prop in the array like so: copy[0].name = 'Wartortle' -copy.push({name: 'Charmander'}) // new item +copy.push({ name: 'Charmander' })( + // new item -// then the original array will still be the same: -(original[0].name === 'Squirtle') -(original[1] === undefined) + // then the original array will still be the same: + original[0].name === 'Squirtle' +)(original[1] === undefined) ``` ## Non-enumerable @@ -75,19 +77,16 @@ copy.push({name: 'Charmander'}) // new item By default, copy-anything only copies enumerable properties. If you also want to copy non-enumerable properties you can do so by passing that as an option. ```js -const original = {name: 'Bulbasaur'} +const original = { name: 'Bulbasaur' } // bulbasaur's ID is non-enumerable Object.defineProperty(original, 'id', { value: '001', writable: true, enumerable: false, - configurable: true + configurable: true, }) const copy1 = copy(original) -const copy2 = copy(original, {nonenumerable: true}) - -(copy1.id === undefined) -(copy2.id === '001') +const copy2 = copy(original, { nonenumerable: true })(copy1.id === undefined)(copy2.id === '001') ``` ## Limit to specific props @@ -95,10 +94,8 @@ const copy2 = copy(original, {nonenumerable: true}) You can limit to specific props. ```js -const original = {name: 'Flareon', type: ['fire'], id: '136'} -const copy = copy(original, {props: ['name']}) - -(copy === {name: 'Flareon'}) +const original = { name: 'Flareon', type: ['fire'], id: '136' } +const copy = copy(original, { props: ['name'] })(copy === { name: 'Flareon' }) ``` > Please note, if the props you have specified are non-enumerable, you will also need to pass `{nonenumerable: true}`. @@ -110,7 +107,7 @@ The source code is literally just these lines. Most of the magic comes from the ```JavaScript import { isPlainObject } from 'is-what' -export default function copy (target) { +export function copy (target) { if (isArray(target)) return target.map(i => copy(i)) if (!isPlainObject(target)) return target return Object.keys(target) diff --git a/dist/index.cjs.js b/dist/index.cjs.js index ed3e8aa..c218819 100644 --- a/dist/index.cjs.js +++ b/dist/index.cjs.js @@ -1,5 +1,7 @@ 'use strict'; +Object.defineProperty(exports, '__esModule', { value: true }); + var isWhat = require('is-what'); /*! ***************************************************************************** @@ -69,4 +71,4 @@ function copy(target, options) { }, {}); } -module.exports = copy; +exports.copy = copy; diff --git a/dist/index.esm.js b/dist/index.esm.js index a421e6b..bc3978a 100644 --- a/dist/index.esm.js +++ b/dist/index.esm.js @@ -67,4 +67,4 @@ function copy(target, options) { }, {}); } -export default copy; +export { copy }; diff --git a/package.json b/package.json index 4e91a39..93a45e6 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "copy-anything", "sideEffects": false, - "version": "1.6.0", + "version": "2.0.0", "description": "An optimised way to copy'ing an object. A small and simple integration", "main": "dist/index.cjs.js", "module": "dist/index.esm.js", @@ -26,7 +26,9 @@ "clone-objects", "json-stringify-json-parse", "deep-clone", - "deep-copy" + "deep-copy", + "typescript", + "ts" ], "author": "Luca Ban - Mesqueeb", "license": "MIT", @@ -50,11 +52,7 @@ "typescript": "^3.7.5" }, "ava": { - "extensions": [ - "ts" - ], - "require": [ - "ts-node/register" - ] + "extensions": ["ts"], + "require": ["ts-node/register"] } } diff --git a/src/index.ts b/src/index.ts index 7b04896..e4dc816 100644 --- a/src/index.ts +++ b/src/index.ts @@ -36,7 +36,7 @@ export type Options = { props?: (string | symbol)[]; nonenumerable?: boolean } * @returns {T} the target with replaced values * @export */ -export default function copy (target: T, options: Options = {}): T { +export function copy (target: T, options: Options = {}): T { if (isArray(target)) return target.map((i: any) => copy(i, options)) if (!isPlainObject(target)) return target const props = Object.getOwnPropertyNames(target) diff --git a/test/index.ts b/test/index.ts index 16eeba1..b768a62 100644 --- a/test/index.ts +++ b/test/index.ts @@ -1,5 +1,5 @@ import test from 'ava' -import copy from '../src/index' +import { copy } from '../src/index' test('copy - change original', t => { const original = { a: 0, b: 0, c: { d: 0 } } diff --git a/types/index.d.ts b/types/index.d.ts index 4ef32dc..62e12f6 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -12,4 +12,4 @@ export declare type Options = { * @returns {T} the target with replaced values * @export */ -export default function copy(target: T, options?: Options): T; +export declare function copy(target: T, options?: Options): T;