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