Skip to content

Commit

Permalink
v2.0 - only named exports 🕶
Browse files Browse the repository at this point in the history
  • Loading branch information
mesqueeb committed Mar 14, 2020
1 parent 70acdf7 commit a49875e
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 36 deletions.
43 changes: 20 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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.
Expand All @@ -58,47 +59,43 @@ 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

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

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}`.
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion dist/index.cjs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var isWhat = require('is-what');

/*! *****************************************************************************
Expand Down Expand Up @@ -69,4 +71,4 @@ function copy(target, options) {
}, {});
}

module.exports = copy;
exports.copy = copy;
2 changes: 1 addition & 1 deletion dist/index.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ function copy(target, options) {
}, {});
}

export default copy;
export { copy };
14 changes: 6 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -26,7 +26,9 @@
"clone-objects",
"json-stringify-json-parse",
"deep-clone",
"deep-copy"
"deep-copy",
"typescript",
"ts"
],
"author": "Luca Ban - Mesqueeb",
"license": "MIT",
Expand All @@ -50,11 +52,7 @@
"typescript": "^3.7.5"
},
"ava": {
"extensions": [
"ts"
],
"require": [
"ts-node/register"
]
"extensions": ["ts"],
"require": ["ts-node/register"]
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export type Options = { props?: (string | symbol)[]; nonenumerable?: boolean }
* @returns {T} the target with replaced values
* @export
*/
export default function copy<T extends any> (target: T, options: Options = {}): T {
export function copy<T extends any> (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)
Expand Down
2 changes: 1 addition & 1 deletion test/index.ts
Original file line number Diff line number Diff line change
@@ -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 } }
Expand Down
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ export declare type Options = {
* @returns {T} the target with replaced values
* @export
*/
export default function copy<T extends any>(target: T, options?: Options): T;
export declare function copy<T extends any>(target: T, options?: Options): T;

0 comments on commit a49875e

Please sign in to comment.