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.

README.md 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. # clone
  2. [![build status](https://secure.travis-ci.org/pvorb/clone.svg)](http://travis-ci.org/pvorb/clone) [![downloads](https://img.shields.io/npm/dt/clone.svg)](http://npm-stat.com/charts.html?package=clone)
  3. offers foolproof _deep cloning_ of objects, arrays, numbers, strings, maps,
  4. sets, promises, etc. in JavaScript.
  5. **XSS vulnerability detected**
  6. ## Installation
  7. npm install clone
  8. (It also works with browserify, ender or standalone. You may want to use the
  9. option `noParse` in browserify to reduce the resulting file size, since usually
  10. `Buffer`s are not needed in browsers.)
  11. ## Example
  12. ~~~ javascript
  13. var clone = require('clone');
  14. var a, b;
  15. a = { foo: { bar: 'baz' } }; // initial value of a
  16. b = clone(a); // clone a -> b
  17. a.foo.bar = 'foo'; // change a
  18. console.log(a); // show a
  19. console.log(b); // show b
  20. ~~~
  21. This will print:
  22. ~~~ javascript
  23. { foo: { bar: 'foo' } }
  24. { foo: { bar: 'baz' } }
  25. ~~~
  26. **clone** masters cloning simple objects (even with custom prototype), arrays,
  27. Date objects, and RegExp objects. Everything is cloned recursively, so that you
  28. can clone dates in arrays in objects, for example.
  29. ## API
  30. `clone(val, circular, depth)`
  31. * `val` -- the value that you want to clone, any type allowed
  32. * `circular` -- boolean
  33. Call `clone` with `circular` set to `false` if you are certain that `obj`
  34. contains no circular references. This will give better performance if
  35. needed. There is no error if `undefined` or `null` is passed as `obj`.
  36. * `depth` -- depth to which the object is to be cloned (optional,
  37. defaults to infinity)
  38. * `prototype` -- sets the prototype to be used when cloning an object.
  39. (optional, defaults to parent prototype).
  40. * `includeNonEnumerable` -- set to `true` if the non-enumerable properties
  41. should be cloned as well. Non-enumerable properties on the prototype chain
  42. will be ignored. (optional, defaults to `false`)
  43. `clone.clonePrototype(obj)`
  44. * `obj` -- the object that you want to clone
  45. Does a prototype clone as
  46. [described by Oran Looney](http://oranlooney.com/functional-javascript/).
  47. ## Circular References
  48. ~~~ javascript
  49. var a, b;
  50. a = { hello: 'world' };
  51. a.myself = a;
  52. b = clone(a);
  53. console.log(b);
  54. ~~~
  55. This will print:
  56. ~~~ javascript
  57. { hello: "world", myself: [Circular] }
  58. ~~~
  59. So, `b.myself` points to `b`, not `a`. Neat!
  60. ## Test
  61. npm test
  62. ## Changelog
  63. ### v2.1.2
  64. #### 2018-03-21
  65. - Use `Buffer.allocUnsafe()` on Node >= 4.5.0 (contributed by @ChALkeR)
  66. ### v2.1.1
  67. #### 2017-03-09
  68. - Fix build badge in README
  69. - Add support for cloning Maps and Sets on Internet Explorer
  70. ### v2.1.0
  71. #### 2016-11-22
  72. - Add support for cloning Errors
  73. - Exclude non-enumerable symbol-named object properties from cloning
  74. - Add option to include non-enumerable own properties of objects
  75. ### v2.0.0
  76. #### 2016-09-28
  77. - Add support for cloning ES6 Maps, Sets, Promises, and Symbols
  78. ### v1.0.3
  79. #### 2017-11-08
  80. - Close XSS vulnerability in the NPM package, which included the file
  81. `test-apart-ctx.html`. This vulnerability was disclosed by Juho Nurminen of
  82. 2NS - Second Nature Security.
  83. ### v1.0.2 (deprecated)
  84. #### 2015-03-25
  85. - Fix call on getRegExpFlags
  86. - Refactor utilities
  87. - Refactor test suite
  88. ### v1.0.1 (deprecated)
  89. #### 2015-03-04
  90. - Fix nodeunit version
  91. - Directly call getRegExpFlags
  92. ### v1.0.0 (deprecated)
  93. #### 2015-02-10
  94. - Improve browser support
  95. - Improve browser testability
  96. - Move helper methods to private namespace
  97. ## Caveat
  98. Some special objects like a socket or `process.stdout`/`stderr` are known to not
  99. be cloneable. If you find other objects that cannot be cloned, please [open an
  100. issue](https://github.com/pvorb/clone/issues/new).
  101. ## Bugs and Issues
  102. If you encounter any bugs or issues, feel free to [open an issue at
  103. github](https://github.com/pvorb/clone/issues) or send me an email to
  104. <paul@vorba.ch>. I also always like to hear from you, if you’re using my code.
  105. ## License
  106. Copyright © 2011-2016 [Paul Vorbach](https://paul.vorba.ch/) and
  107. [contributors](https://github.com/pvorb/clone/graphs/contributors).
  108. Permission is hereby granted, free of charge, to any person obtaining a copy of
  109. this software and associated documentation files (the “Software”), to deal in
  110. the Software without restriction, including without limitation the rights to
  111. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  112. the Software, and to permit persons to whom the Software is furnished to do so,
  113. subject to the following conditions:
  114. The above copyright notice and this permission notice shall be included in all
  115. copies or substantial portions of the Software.
  116. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  117. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  118. FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  119. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  120. IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, OUT OF OR IN CONNECTION WITH THE
  121. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.