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.

126 lines
3.5 KiB

3 months ago
  1. # clone
  2. [![build status](https://secure.travis-ci.org/pvorb/node-clone.png)](http://travis-ci.org/pvorb/node-clone)
  3. [![info badge](https://nodei.co/npm/clone.png?downloads=true&downloadRank=true&stars=true)](http://npm-stat.com/charts.html?package=clone)
  4. offers foolproof _deep cloning_ of objects, arrays, numbers, strings etc. in JavaScript.
  5. ## Installation
  6. npm install clone
  7. (It also works with browserify, ender or standalone.)
  8. ## Example
  9. ~~~ javascript
  10. var clone = require('clone');
  11. var a, b;
  12. a = { foo: { bar: 'baz' } }; // initial value of a
  13. b = clone(a); // clone a -> b
  14. a.foo.bar = 'foo'; // change a
  15. console.log(a); // show a
  16. console.log(b); // show b
  17. ~~~
  18. This will print:
  19. ~~~ javascript
  20. { foo: { bar: 'foo' } }
  21. { foo: { bar: 'baz' } }
  22. ~~~
  23. **clone** masters cloning simple objects (even with custom prototype), arrays,
  24. Date objects, and RegExp objects. Everything is cloned recursively, so that you
  25. can clone dates in arrays in objects, for example.
  26. ## API
  27. `clone(val, circular, depth)`
  28. * `val` -- the value that you want to clone, any type allowed
  29. * `circular` -- boolean
  30. Call `clone` with `circular` set to `false` if you are certain that `obj`
  31. contains no circular references. This will give better performance if needed.
  32. There is no error if `undefined` or `null` is passed as `obj`.
  33. * `depth` -- depth to which the object is to be cloned (optional,
  34. defaults to infinity)
  35. `clone.clonePrototype(obj)`
  36. * `obj` -- the object that you want to clone
  37. Does a prototype clone as
  38. [described by Oran Looney](http://oranlooney.com/functional-javascript/).
  39. ## Circular References
  40. ~~~ javascript
  41. var a, b;
  42. a = { hello: 'world' };
  43. a.myself = a;
  44. b = clone(a);
  45. console.log(b);
  46. ~~~
  47. This will print:
  48. ~~~ javascript
  49. { hello: "world", myself: [Circular] }
  50. ~~~
  51. So, `b.myself` points to `b`, not `a`. Neat!
  52. ## Test
  53. npm test
  54. ## Caveat
  55. Some special objects like a socket or `process.stdout`/`stderr` are known to not
  56. be cloneable. If you find other objects that cannot be cloned, please [open an
  57. issue](https://github.com/pvorb/node-clone/issues/new).
  58. ## Bugs and Issues
  59. If you encounter any bugs or issues, feel free to [open an issue at
  60. github](https://github.com/pvorb/node-clone/issues) or send me an email to
  61. <paul@vorba.ch>. I also always like to hear from you, if you’re using my code.
  62. ## License
  63. Copyright © 2011-2015 [Paul Vorbach](http://paul.vorba.ch/) and
  64. [contributors](https://github.com/pvorb/node-clone/graphs/contributors).
  65. Permission is hereby granted, free of charge, to any person obtaining a copy of
  66. this software and associated documentation files (the “Software”), to deal in
  67. the Software without restriction, including without limitation the rights to
  68. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  69. the Software, and to permit persons to whom the Software is furnished to do so,
  70. subject to the following conditions:
  71. The above copyright notice and this permission notice shall be included in all
  72. copies or substantial portions of the Software.
  73. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  74. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  75. FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  76. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  77. IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, OUT OF OR IN CONNECTION WITH THE
  78. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.