# clones

> should deep clone everything even global objects, functions, circularities, ...

Latest version **1.2.0** (published 2019-01-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install clones
pnpm add clones
yarn add clones
bun add clones
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2019-01-01 |
| First published | 2017-02-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=4.0.0 |
| Dependencies | 0 |
| Unpacked size | 17.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | commenthol |
| Maintainers | commenthol |
| Keywords | circular, clone, deep-clone, global |

## Links

- npm: https://www.npmjs.com/package/clones
- Repository: https://github.com/commenthol/clones
- Homepage: https://github.com/commenthol/clones#readme
- Issues: https://github.com/commenthol/clones/issues
- npm.io page: https://npm.io/package/clones

## Alternatives

- [jsforce](https://npm.io/package/jsforce.md) — 851.2K weekly downloads
- [react-native-qrcode-svg](https://npm.io/package/react-native-qrcode-svg.md) — 693.5K weekly downloads
- [@salesforce/plugin-data](https://npm.io/package/@salesforce/plugin-data.md) — 394.9K weekly downloads
- [@backstage/plugin-search-common](https://npm.io/package/@backstage/plugin-search-common.md) — 308.5K weekly downloads
- [@chain-registry/types](https://npm.io/package/@chain-registry/types.md) — 38.4K weekly downloads

## Recent versions

- 1.2.0 (latest) — 2019-01-01
- 1.1.0 — 2017-03-04
- 1.0.1 — 2017-02-19
- 1.0.0 — 2017-02-18

## README

# clones

[![NPM version](https://badge.fury.io/js/clones.svg)](https://www.npmjs.com/package/clones/)

> should deep clone everything even global objects, functions, circularities, ...

Companion for [safer-eval](https://github.com/commenthol/safer-eval).

Runs on node and in modern browsers:

|                | Versions |
| ---            | ---      |
| **node**       | ~~0.12~~, 4, 6, 8, 10, 11  |
| **Chrome**     | 55, 56, 71 |
| **Firefox**    | 45, 51, 64 |
| **Edge**       | 14, 16   |
| **IE**         | ~~11~~   |
| **Safari**     | 10       |
| **iOS Safari** | 10       |

## Installation

```
npm i -S clones
```

## Usage

```js
const clones = require('clones')
const dest = clones(source, [bind])
```

**Parameters**

**Parameters**

**source**: `Object`, clone source

**bind**: `Object`, bind functions to this context

**Returns**: `Any`, deep clone of `source`

**Example**:
```js
const clones = require('clones')

var source = {
  obj: {a: {b: 1}},
  arr: [true, 1, {c: 'dee'}],
  fn: function () { return this.number + 12 }
}
// adding circularity
source.obj.a.e = source.obj.a

// do the cloning (with binding a context)
var dest = clones(source, {number: 30})
// => { obj: { a: { b: 1, e: [Circular] }, d: 2017-02-17T21:57:44.576Z },
//      arr: [ true, 1, { c: 'dee' } ],
//      fn: [Function: fn] }

// checks
assert.ok(dest !== source)                      // has different reference
assert.ok(dest.obj !== source.obj)              // has different reference
assert.ok(dest.obj.a !== source.obj.a)          // has different reference
assert.ok(dest.obj.a.e !== source.obj.a.e)      // different references for circularities
assert.equal(dest.obj.d.toISOString(),
  source.obj.d.toISOString())                   // has same content
assert.ok(dest.fn !== source.fn)                // has different function reference
source.fn = source.fn.bind({number: 29})        // bind `this` for `source`
assert.equal(dest.fn(), source.fn() + 1)        // returning the same result
```

### Clone prototypes or classes

```js
const clones = require('clones')
// clone built in `Array`
const C = clones.classes(Array)

let c = new C(1,2,3)
// => [1, 2, 3]
c.reverse()
// => [3, 2, 1]
```

## License

[MIT](./LICENSE)

---
_Source: https://npm.io/package/clones · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
