All Projects → mesqueeb → copy-anything

mesqueeb / copy-anything

Licence: MIT license
An optimised way to copy'ing (cloning) an Object or Array. A small and simple integration

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to copy-anything

Clone Deep
Recursively (deep) clone JavaScript native types, like Object, Array, RegExp, Date as well as primitives. Used by superstruct, merge-deep, and many others!
Stars: ✭ 229 (+1105.26%)
Mutual labels:  clone, copy, object
Discord Guild Copy
A script to copy a discord guild/server
Stars: ✭ 127 (+568.42%)
Mutual labels:  clone, copy
Shallow Clone
Make a shallow clone of an object, array or primitive.
Stars: ✭ 23 (+21.05%)
Mutual labels:  clone, object
cloner
A deep copy algorithm for haxe
Stars: ✭ 24 (+26.32%)
Mutual labels:  clone, copy
python-pyfields
Define fields in python classes. Easily.
Stars: ✭ 39 (+105.26%)
Mutual labels:  object
wp-site-cloner
Create sites with content from other sites
Stars: ✭ 22 (+15.79%)
Mutual labels:  clone
SmartReplace
Unity plug-in for replacing scene objects while keeping their references.
Stars: ✭ 50 (+163.16%)
Mutual labels:  object
AmazonPrime-ReactJS-Clone
Amazon Prime Clone in ReactJS. I have made a Amazon Prime Clone web version in ReactSJ. All the data is dynamic and even has the facility of Watching Trailers. Play around with the app. Link given in ReadMe
Stars: ✭ 45 (+136.84%)
Mutual labels:  clone
ExplorerGenie
ExplorerGenie is an extended context menu for the Windows explorer.
Stars: ✭ 25 (+31.58%)
Mutual labels:  copy
mpc-qt
Media Player Classic Qute Theater
Stars: ✭ 125 (+557.89%)
Mutual labels:  clone
is-extendable
Answers the question: "can this value have keys?". Returns true if a value is any of the object types: array, regexp, plain object, function or date. Useful for determining if a value is an object that can be extended.
Stars: ✭ 19 (+0%)
Mutual labels:  object
cppcraft
a Minecraft clone written in C++ and OpenGL that includes Minecraft textures, chunks, building, terrain, trees, water, inventories, and more!
Stars: ✭ 75 (+294.74%)
Mutual labels:  clone
obj-to-table
Create a table from an array of objects
Stars: ✭ 15 (-21.05%)
Mutual labels:  object
DataTypes
Built-in data types
Stars: ✭ 34 (+78.95%)
Mutual labels:  object
qverse
Traverse any data with DPML commands.
Stars: ✭ 25 (+31.58%)
Mutual labels:  object
github-org-clone
Clone repositories managed by a github organisation or team
Stars: ✭ 34 (+78.95%)
Mutual labels:  clone
timeline
Timeline - A photo organizer
Stars: ✭ 39 (+105.26%)
Mutual labels:  object
ByteCopy
Simple C99 program and API for copying files.
Stars: ✭ 16 (-15.79%)
Mutual labels:  clone
Differentia.js
No longer being supported or maintained. A Graph Theory & Data Structure Library for JavaScript.
Stars: ✭ 13 (-31.58%)
Mutual labels:  clone
openwar
Classic fantasy RTS game in the spirit of Warcraft: Orcs & Humans.
Stars: ✭ 48 (+152.63%)
Mutual labels:  clone

Copy anything 🎭

Total Downloads Latest Stable Version

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

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
    }, {})
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].