finki-main
main
|
Last change
on this file since 81bc7da was 81bc7da, checked in by Klimentina Efremova <klimentina08642@…>, 3 months ago |
|
Initial commit
|
-
Property mode
set to
100644
|
|
File size:
829 bytes
|
| Line | |
|---|
| 1 | module.exports = clone;
|
|---|
| 2 |
|
|---|
| 3 | // via http://stackoverflow.com/a/728694/22617
|
|---|
| 4 | function clone(obj) {
|
|---|
| 5 | // Handle the 3 simple types, and null or undefined
|
|---|
| 6 | if (null === obj || 'object' !== typeof obj) {
|
|---|
| 7 | return obj;
|
|---|
| 8 | }
|
|---|
| 9 |
|
|---|
| 10 | var copy;
|
|---|
| 11 |
|
|---|
| 12 | // Handle Date
|
|---|
| 13 | if (obj instanceof Date) {
|
|---|
| 14 | copy = new Date();
|
|---|
| 15 | copy.setTime(obj.getTime());
|
|---|
| 16 | return copy;
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | // Handle Array
|
|---|
| 20 | if (obj instanceof Array) {
|
|---|
| 21 | copy = [];
|
|---|
| 22 | for (var i = 0, len = obj.length; i < len; i++) {
|
|---|
| 23 | copy[i] = clone(obj[i]);
|
|---|
| 24 | }
|
|---|
| 25 | return copy;
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | // Handle Object
|
|---|
| 29 | if (obj instanceof Object) {
|
|---|
| 30 | copy = {};
|
|---|
| 31 | for (var attr in obj) {
|
|---|
| 32 | if (obj.hasOwnProperty && obj.hasOwnProperty(attr)) {
|
|---|
| 33 | copy[attr] = clone(obj[attr]);
|
|---|
| 34 | }
|
|---|
| 35 | }
|
|---|
| 36 | return copy;
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | throw new Error('Unable to copy obj! Its type isn\'t supported.');
|
|---|
| 40 | } |
|---|
Note:
See
TracBrowser
for help on using the repository browser.