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.

152 lines
5.1 KiB

4 years ago
  1. # shallow-clone [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=W8YFZ425KND68) [![NPM version](https://img.shields.io/npm/v/shallow-clone.svg?style=flat)](https://www.npmjs.com/package/shallow-clone) [![NPM monthly downloads](https://img.shields.io/npm/dm/shallow-clone.svg?style=flat)](https://npmjs.org/package/shallow-clone) [![NPM total downloads](https://img.shields.io/npm/dt/shallow-clone.svg?style=flat)](https://npmjs.org/package/shallow-clone) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/shallow-clone.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/shallow-clone)
  2. > Creates a shallow clone of any JavaScript value.
  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 shallow-clone
  8. ```
  9. ## Usage
  10. ```js
  11. const clone = require('shallow-clone');
  12. ```
  13. **Supports**
  14. * array buffers
  15. * arrays
  16. * buffers
  17. * dates
  18. * errors
  19. * float32 arrays
  20. * float64 arrays
  21. * int16 arrays
  22. * int32 arrays
  23. * int8 arrays
  24. * maps
  25. * objects
  26. * primitives
  27. * regular expressions
  28. * sets
  29. * symbols
  30. * uint16 arrays
  31. * uint32 arrays
  32. * uint8 arrays
  33. * uint8clamped arrays
  34. ## Arrays
  35. By default, only the array itself is cloned (shallow), use [clone-deep](https://github.com/jonschlinkert/clone-deep) if you also need the elements in the array to be cloned.
  36. ```js
  37. const arr = [{ a: 0 }, { b: 1 }];
  38. const foo = clone(arr);
  39. // foo => [{ 'a': 0 }, { 'b': 1 }]
  40. // array is cloned
  41. assert(actual === expected); // false
  42. // array elements are not
  43. assert.deepEqual(actual[0], expected[0]); // true
  44. ```
  45. ## Objects
  46. Only the object is shallow cloned, use [clone-deep](https://github.com/jonschlinkert/clone-deep) if you also need the values in the object to be cloned.
  47. ```js
  48. console.log(clone({ a: 1, b: 2, c: 3 }));
  49. //=> {a: 1, b: 2, c: 3 }
  50. ```
  51. ## RegExp
  52. Clones regular expressions and flags, and preserves the `.lastIndex`.
  53. ```js
  54. const regex = clone(/foo/g); //=> /foo/g
  55. // you can manually reset lastIndex if necessary
  56. regex.lastIndex = 0;
  57. ```
  58. ## Primitives
  59. Simply returns primitives unchanged.
  60. ```js
  61. clone(0); //=> 0
  62. clone('foo'); //=> 'foo'
  63. ```
  64. ## About
  65. <details>
  66. <summary><strong>Contributing</strong></summary>
  67. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
  68. </details>
  69. <details>
  70. <summary><strong>Running Tests</strong></summary>
  71. 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:
  72. ```sh
  73. $ npm install && npm test
  74. ```
  75. </details>
  76. <details>
  77. <summary><strong>Building docs</strong></summary>
  78. _(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.)_
  79. To generate the readme, run the following command:
  80. ```sh
  81. $ npm install -g verbose/verb#dev verb-generate-readme && verb
  82. ```
  83. </details>
  84. ### Related projects
  85. You might also be interested in these projects:
  86. * [assign-deep](https://www.npmjs.com/package/assign-deep): Deeply assign the values of all enumerable-own-properties and symbols from one or more source objects… [more](https://github.com/jonschlinkert/assign-deep) | [homepage](https://github.com/jonschlinkert/assign-deep "Deeply assign the values of all enumerable-own-properties and symbols from one or more source objects to a target object. Returns the target object.")
  87. * [clone-deep](https://www.npmjs.com/package/clone-deep): Recursively (deep) clone JavaScript native types, like Object, Array, RegExp, Date as well as primitives. | [homepage](https://github.com/jonschlinkert/clone-deep "Recursively (deep) clone JavaScript native types, like Object, Array, RegExp, Date as well as primitives.")
  88. * [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.")
  89. * [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.")
  90. ### Contributors
  91. | **Commits** | **Contributor** |
  92. | --- | --- |
  93. | 20 | [jonschlinkert](https://github.com/jonschlinkert) |
  94. | 2 | [doowb](https://github.com/doowb) |
  95. | 1 | [jakub-g](https://github.com/jakub-g) |
  96. ### Author
  97. **Jon Schlinkert**
  98. * [GitHub Profile](https://github.com/jonschlinkert)
  99. * [Twitter Profile](https://twitter.com/jonschlinkert)
  100. * [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)
  101. ### License
  102. Copyright © 2019, [Jon Schlinkert](https://github.com/jonschlinkert).
  103. Released under the [MIT License](LICENSE).
  104. ***
  105. _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on April 15, 2019._