You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
4.2 KiB

4 years ago
  1. # clone-deep [![NPM version](https://img.shields.io/npm/v/clone-deep.svg?style=flat)](https://www.npmjs.com/package/clone-deep) [![NPM monthly downloads](https://img.shields.io/npm/dm/clone-deep.svg?style=flat)](https://npmjs.org/package/clone-deep) [![NPM total downloads](https://img.shields.io/npm/dt/clone-deep.svg?style=flat)](https://npmjs.org/package/clone-deep) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/clone-deep.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/clone-deep)
  2. > Recursively (deep) clone JavaScript native types, like Object, Array, RegExp, Date as well as primitives.
  3. Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
  4. ## Install
  5. Install with [npm](https://www.npmjs.com/):
  6. ```sh
  7. $ npm install --save clone-deep
  8. ```
  9. ## Usage
  10. ```js
  11. const cloneDeep = require('clone-deep');
  12. let obj = { a: 'b' };
  13. let arr = [obj];
  14. let copy = cloneDeep(arr);
  15. obj.c = 'd';
  16. console.log(copy);
  17. //=> [{ a: 'b' }]
  18. console.log(arr);
  19. //=> [{ a: 'b', c: 'd' }]
  20. ```
  21. ## Heads up!
  22. The last argument specifies whether or not to clone instances (objects that are from a custom class or are not created by the `Object` constructor. This value may be `true` or the function use for cloning instances.
  23. When an `instanceClone` function is provided, it will be invoked to clone objects that are not "plain" objects (as defined by [isPlainObject](#isPlainObject)`isPlainObject`). If `instanceClone` is not specified, this library will not attempt to clone non-plain objects, and will simply copy the object reference.
  24. ## Attribution
  25. Initially based on [mout's](https://github.com/mout/mout) implementation of deepClone.
  26. ## About
  27. <details>
  28. <summary><strong>Contributing</strong></summary>
  29. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
  30. </details>
  31. <details>
  32. <summary><strong>Running Tests</strong></summary>
  33. Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
  34. ```sh
  35. $ npm install && npm test
  36. ```
  37. </details>
  38. <details>
  39. <summary><strong>Building docs</strong></summary>
  40. _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
  41. To generate the readme, run the following command:
  42. ```sh
  43. $ npm install -g verbose/verb#dev verb-generate-readme && verb
  44. ```
  45. </details>
  46. ### Related projects
  47. You might also be interested in these projects:
  48. * [is-plain-object](https://www.npmjs.com/package/is-plain-object): Returns true if an object was created by the `Object` constructor. | [homepage](https://github.com/jonschlinkert/is-plain-object "Returns true if an object was created by the `Object` constructor.")
  49. * [isobject](https://www.npmjs.com/package/isobject): Returns true if the value is an object and not an array or null. | [homepage](https://github.com/jonschlinkert/isobject "Returns true if the value is an object and not an array or null.")
  50. * [kind-of](https://www.npmjs.com/package/kind-of): Get the native type of a value. | [homepage](https://github.com/jonschlinkert/kind-of "Get the native type of a value.")
  51. * [shallow-clone](https://www.npmjs.com/package/shallow-clone): Creates a shallow clone of any JavaScript value. | [homepage](https://github.com/jonschlinkert/shallow-clone "Creates a shallow clone of any JavaScript value.")
  52. ### Contributors
  53. | **Commits** | **Contributor** |
  54. | --- | --- |
  55. | 46 | [jonschlinkert](https://github.com/jonschlinkert) |
  56. | 2 | [yujunlong2000](https://github.com/yujunlong2000) |
  57. ### Author
  58. **Jon Schlinkert**
  59. * [GitHub Profile](https://github.com/jonschlinkert)
  60. * [Twitter Profile](https://twitter.com/jonschlinkert)
  61. * [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)
  62. ### License
  63. Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
  64. Released under the [MIT License](LICENSE).
  65. ***
  66. _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on November 21, 2018._