4.0.2 • Published 7 months ago

copy-anything v4.0.2

Weekly downloads
864,821
License
MIT
Repository
github
Last release
7 months ago

Copy anything 🎭

npm i copy-anything

An optimised way to copy'ing (cloning) an object or array. A small and simple integration.

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 was looking for:

  • a simple copy/clone function
  • has to be fast!
  • props must lose any reference to original object
  • works with arrays and objects in arrays!
  • supports symbols
  • can copy non-enumerable props as well
  • does not break special class instances ‼️

This last one is crucial! So many libraries use custom classes that create objects with special prototypes, and such objects all break when trying to copy them inproperly. So we gotta be careful!

copy-anything will copy objects and nested properties, but only as long as they're "plain objects". As soon as a sub-prop is not a "plain object" and has a special prototype, it will copy that instance over "as is". ♻️

Meet the family (more tiny utils with TS support)

Usage

import { copy } from 'copy-anything'

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
// or add a new nested prop
copy.type.fire = true

// then the original object will still be the same:
(original.type.water === true) // true
(original.type.fire === undefined) // true

Please note, by default copy-anything does not copy non-enumerable props. If you need to copy those, see the instructions further down below.

Works with arrays

It will also clone arrays, as well as objects inside arrays! 😉

const original = [{ name: 'Squirtle' }]
const copy = copy(original)

// now if we change a prop in the array
copy[0].name = 'Wartortle'
// or add a new item to the array
copy.push({ name: 'Charmander' })

// then the original array will still be the same:
(original[0].name === 'Squirtle') // true
(original[1] === undefined) // true

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.

const original = { name: 'Bulbasaur' }
// bulbasaur's ID is non-enumerable
Object.defineProperty(original, 'id', {
  value: '001',
  writable: true,
  enumerable: false,
  configurable: true,
})
const copy1 = copy(original)
(copy1.id === undefined) // true

const copy2 = copy(original, { nonenumerable: true })
(copy2.id === '001') // true

Limit to specific props

You can limit to specific props.

const original = { name: 'Flareon', type: ['fire'], id: '136' }
const copy = copy(original, { props: ['name'] })

(copy) // will look like: `{ name: 'Flareon' }`

Please note, if the props you have specified are non-enumerable, you will also need to pass {nonenumerable: true}.

Source code

The source code is literally just these lines. Most of the magic comes from the isPlainObject function from the is-what library.

import { isPlainObject } from 'is-what'

export function copy (target) {
  if (isArray(target)) return target.map(i => copy(i))
  if (!isPlainObject(target)) return target
  return Object.keys(target)
    .reduce((carry, key) => {
      const val = target[key]
      carry[key] = copy(val)
      return carry
    }, {})
}
lessst-store@olivervorasai/sliderasterjs@everything-registry/sub-chunk-1380@belopash/squid-toolsshacl-form-reactteapackage-tatespoorman297superjsonsuperjson-cjssuperjson-oldstored-mapvs-tree-plusanci-reactless-4.2.0-4261jsonresume-theme-orbit-remasterized-frless_browser_fixiiif-manifest-editorjcd-devtoolshello_test_my_webpackmynancemathquillcustommathquillcustomlibopenapi-axios-clientpretty-state-machinenext10-clipeopledatalabsreact-china-divisionreact-easy-formcorereact-easy-formrenderreact-dragger-sortquasar-ui-component-info-cardquasar-ui-easy-tablesquasar-ui-easy-formsreact-addresssort-anythingtohsaka888-use-breakpoint@bithighlander/openapi-client-axios@blitzar/components@blitzar/form@cdxoo/pojo-utils128981semzub@codelic/chat-components@codelic/codelic-cli@codelic/component@codelic/console@codepen/less@codelic/ac-com@codelic/web-scraperzubtesttodayvuex-easy-firestorevue-test-shh-ui@grund/tools@hoseki-inc/superjson@ipinyou/dashboard@magnetarjs/plugin-simple-store@magnetarjs/plugin-vue2@magnetarjs/plugin-vue3@nodesuite/options@enirisdev/angular-google-charts@openfeature/go-feature-flag-provider@slimr/util@soyjak/utils@solidpixels/less@simpleform/editor@simpleform/form@simpleform/inula-editor@simpleform/inula-form@simpleform/inula-render@simpleform/render@stlmpp/store@dinert/component@pbbbl/clean-deep@pbbbl/stringifyef-h5emily-webpack@vue-sync/simple-store@zhangchongzhi/webpackakita-ui@yicode/yite-cli@yicode/yivite-cli@zalastax/nolb-copalisselisss-lab3bitboss-uiappxgencrossnotecase-gg-editorcssesc-wbgdist-packagedocument-model-libsdocs-vuepress
4.0.1

7 months ago

4.0.2

7 months ago

4.0.0

7 months ago

3.0.5

2 years ago

3.0.4

2 years ago

3.0.3

2 years ago

2.0.5

3 years ago

2.0.4

3 years ago

2.0.6

3 years ago

3.0.2

3 years ago

3.0.1

3 years ago

3.0.0

3 years ago

2.0.3

4 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.6.0

5 years ago

1.5.4

5 years ago

1.5.3

5 years ago

1.5.2

5 years ago

1.5.1

5 years ago

1.5.0

5 years ago

1.4.0

5 years ago

1.3.0

5 years ago

1.2.4

5 years ago

1.2.3

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago