Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

README.md 2.5 KiB

3 år sedan
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # shallow-clone [![NPM version](https://badge.fury.io/js/shallow-clone.svg)](http://badge.fury.io/js/shallow-clone)
  2. > Make a shallow clone of an object, array or primitive.
  3. ## Install
  4. Install with [npm](https://www.npmjs.com/)
  5. ```sh
  6. $ npm i shallow-clone --save
  7. ```
  8. ## Usage
  9. ```js
  10. var clone = require('shallow-clone');
  11. ```
  12. ## shallow clones arrays
  13. The array itself is cloned, but not the elements of the array. So any objects in the array will still not be cloned (e.g. they will be the same object as in the orginal array).
  14. ```js
  15. var arr = [{ 'a': 0 }, { 'b': 1 }]
  16. var foo = clone(arr);
  17. // foo => [{ 'a': 0 }, { 'b': 1 }]
  18. // array is cloned
  19. assert.equal(actual === expected, false);
  20. // array elements are not
  21. assert.deepEqual(actual[0], expected[0]); // true
  22. ```
  23. ## returns primitives as-is
  24. ```js
  25. clone(0)
  26. //=> 0
  27. clone('foo')
  28. //=> 'foo'
  29. ```
  30. ## shallow clone a regex
  31. ```js
  32. clone(/foo/g)
  33. //=> /foo/g
  34. ```
  35. ## shallow clone an object
  36. ```js
  37. clone({a: 1, b: 2, c: 3 })
  38. //=> {a: 1, b: 2, c: 3 }
  39. ```
  40. ## Related projects
  41. * [assign-deep](https://github.com/jonschlinkert/assign-deep): Deeply assign the enumerable properties of source objects to a destination object.
  42. * [clone-deep](https://github.com/jonschlinkert/clone-deep): Recursively (deep) clone JavaScript native types, like Object, Array, RegExp, Date as well as primitives.
  43. * [extend-shallow](https://github.com/jonschlinkert/extend-shallow): Extend an object with the properties of additional objects. node.js/javascript util.
  44. * [is-plain-object](https://github.com/jonschlinkert/is-plain-object): Returns true if an object was created by the `Object` constructor.
  45. * [mixin-object](https://github.com/jonschlinkert/mixin-object): Mixin the own and inherited properties of other objects onto the first object. Pass an… [more](https://github.com/jonschlinkert/mixin-object)
  46. * [mixin-deep](https://github.com/jonschlinkert/mixin-deep): Deeply mix the properties of objects into the first object. Like merge-deep, but doesn't clone.
  47. ## Running tests
  48. Install dev dependencies:
  49. ```sh
  50. $ npm i -d && npm test
  51. ```
  52. ## Contributing
  53. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/shallow-clone/issues/new)
  54. ## Author
  55. **Jon Schlinkert**
  56. + [github/jonschlinkert](https://github.com/jonschlinkert)
  57. + [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
  58. ## License
  59. Copyright © 2015 Jon Schlinkert
  60. Released under the MIT license.
  61. ***
  62. _This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on August 10, 2015._