Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sasaplus1/deepcopy.js
deep copy data
https://github.com/sasaplus1/deepcopy.js
clone deepcopy javascript library
Last synced: 3 months ago
JSON representation
deep copy data
- Host: GitHub
- URL: https://github.com/sasaplus1/deepcopy.js
- Owner: sasaplus1
- License: mit
- Created: 2013-01-25T12:03:47.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2024-03-12T11:19:16.000Z (4 months ago)
- Last Synced: 2024-03-18T17:17:26.318Z (3 months ago)
- Topics: clone, deepcopy, javascript, library
- Language: TypeScript
- Homepage: https://sasaplus1.github.io/deepcopy.js/benchmark
- Size: 1.48 MB
- Stars: 130
- Watchers: 5
- Forks: 19
- Open Issues: 41
-
Metadata Files:
- Readme: README.md
- Changelog: HISTORY.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Lists
- awesome-github-star - deepcopy.js
README
# deepcopy.js
[![test](https://github.com/sasaplus1/deepcopy.js/workflows/test/badge.svg)](https://github.com/sasaplus1/deepcopy.js/actions?query=workflow%3Atest)
[![npm version](https://badge.fury.io/js/deepcopy.svg)](http://badge.fury.io/js/deepcopy)
[![Try deepcopy on RunKit](https://badge.runkitcdn.com/deepcopy.svg)](https://npm.runkit.com/deepcopy)
[![renovate](https://badges.renovateapi.com/github/sasaplus1/deepcopy.js)](https://renovatebot.com)deep copy data
## Installation
### npm
```console
$ npm install deepcopy
```## Usage
### node.js
#### JavaScript
```js
const deepcopy = require('deepcopy');
```#### TypeScript
```typescript
import * as deepcopy from 'deepcopy';
```### browser
```html
```
### Example
basic usage:
```js
const src = {
desserts: [
{ name: 'cake' },
{ name: 'ice cream' },
{ name: 'pudding' }
]
};const dist = deepcopy(src);
src.desserts = null;
console.log(src); // { desserts: null }
console.log(dist); // { desserts: [ { name: 'cake' }, { name: 'ice cream' }, { name: 'pudding' } ] }
```customize deepcopy:
```js
function MyClass(id) {
this._id = id;
}const src = {
myClasses: [
new MyClass(1),
new MyClass(2),
new MyClass(3)
]
};const dest = deepcopy(base, {
customizer(value) {
if (target.constructor === MyClass) {
return new MyClass(target._id);
}
}
});src.myClasses = null;
console.log(src); // { myClasses: null }
console.log(dest); // { myClasses: [ MyClass { _id: 1 }, MyClass { _id: 2 }, MyClass { _id: 3 } ] }
```## Functions
### deepcopy(value[, options])
- `value`
- `*`
- target value
- `options`
- `Object|Function`
- `Object` - pass options
- `Function` - use as customize function
- `return`
- `*` - copied value### Supported types and copy operation
|type |operation | |
|:-----------------|:-----------|:-------------------------|
|ArrayBuffer |deep copy | |
|Boolean |deep copy | |
|Buffer |deep copy |node.js only |
|DataView |deep copy | |
|Date |deep copy | |
|Number |deep copy | |
|RegExp |deep copy | |
|String |deep copy | |
|Float32Array |deep copy | |
|Float64Array |deep copy | |
|Int16Array |deep copy | |
|Int32Array |deep copy | |
|Int8Array |deep copy | |
|Uint16Array |deep copy | |
|Uint32Array |deep copy | |
|Uint8Array |deep copy | |
|Uint8ClampedArray |deep copy | |
|boolean |deep copy | |
|null |deep copy | |
|number |deep copy | |
|string |deep copy | |
|symbol |deep copy | |
|undefined |deep copy | |
|Arguments |deep copy |recursively, copy as Array|
|Array |deep copy |recursively |
|Map |deep copy |recursively |
|Object |deep copy |recursively |
|Set |deep copy |recursively |
|Array Iterator |shallow copy| |
|Map Iterator |shallow copy| |
|Promise |shallow copy| |
|Set Iterator |shallow copy| |
|String Iterator |shallow copy| |
|function |shallow copy| |
|global |shallow copy|window, global, self, etc.|
|WeakMap |shallow copy| |
|WeakSet |shallow copy| |## Contributors
- [kjirou](https://github.com/kjirou)
- [Pr0methean](https://github.com/Pr0methean)## License
The MIT license.