source: trip-planner-front/node_modules/klona/dist/index.js@ fa375fe

Last change on this file since fa375fe was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.5 KB
Line 
1function klona(x) {
2 if (typeof x !== 'object') return x;
3
4 var k, tmp, str=Object.prototype.toString.call(x);
5
6 if (str === '[object Object]') {
7 if (x.constructor !== Object && typeof x.constructor === 'function') {
8 tmp = new x.constructor();
9 for (k in x) {
10 if (tmp.hasOwnProperty(k) && tmp[k] !== x[k]) {
11 tmp[k] = klona(x[k]);
12 }
13 }
14 } else {
15 tmp = {}; // null
16 for (k in x) {
17 if (k === '__proto__') {
18 Object.defineProperty(tmp, k, {
19 value: klona(x[k]),
20 configurable: true,
21 enumerable: true,
22 writable: true,
23 });
24 } else {
25 tmp[k] = klona(x[k]);
26 }
27 }
28 }
29 return tmp;
30 }
31
32 if (str === '[object Array]') {
33 k = x.length;
34 for (tmp=Array(k); k--;) {
35 tmp[k] = klona(x[k]);
36 }
37 return tmp;
38 }
39
40 if (str === '[object Set]') {
41 tmp = new Set;
42 x.forEach(function (val) {
43 tmp.add(klona(val));
44 });
45 return tmp;
46 }
47
48 if (str === '[object Map]') {
49 tmp = new Map;
50 x.forEach(function (val, key) {
51 tmp.set(klona(key), klona(val));
52 });
53 return tmp;
54 }
55
56 if (str === '[object Date]') {
57 return new Date(+x);
58 }
59
60 if (str === '[object RegExp]') {
61 tmp = new RegExp(x.source, x.flags);
62 tmp.lastIndex = x.lastIndex;
63 return tmp;
64 }
65
66 if (str === '[object DataView]') {
67 return new x.constructor( klona(x.buffer) );
68 }
69
70 if (str === '[object ArrayBuffer]') {
71 return x.slice(0);
72 }
73
74 // ArrayBuffer.isView(x)
75 // ~> `new` bcuz `Buffer.slice` => ref
76 if (str.slice(-6) === 'Array]') {
77 return new x.constructor(x);
78 }
79
80 return x;
81}
82
83exports.klona = klona;
Note: See TracBrowser for help on using the repository browser.