1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- function klona(x) {
- if (typeof x !== 'object') return x;
- var k, tmp, str=Object.prototype.toString.call(x);
- if (str === '[object Object]') {
- if (x.constructor !== Object && typeof x.constructor === 'function') {
- tmp = new x.constructor();
- for (k in x) {
- if (tmp.hasOwnProperty(k) && tmp[k] !== x[k]) {
- tmp[k] = klona(x[k]);
- }
- }
- } else {
- tmp = {}; // null
- for (k in x) {
- if (k === '__proto__') {
- Object.defineProperty(tmp, k, {
- value: klona(x[k]),
- configurable: true,
- enumerable: true,
- writable: true,
- });
- } else {
- tmp[k] = klona(x[k]);
- }
- }
- }
- return tmp;
- }
- if (str === '[object Array]') {
- k = x.length;
- for (tmp=Array(k); k--;) {
- tmp[k] = klona(x[k]);
- }
- return tmp;
- }
- if (str === '[object Set]') {
- tmp = new Set;
- x.forEach(function (val) {
- tmp.add(klona(val));
- });
- return tmp;
- }
- if (str === '[object Map]') {
- tmp = new Map;
- x.forEach(function (val, key) {
- tmp.set(klona(key), klona(val));
- });
- return tmp;
- }
- if (str === '[object Date]') {
- return new Date(+x);
- }
- if (str === '[object RegExp]') {
- tmp = new RegExp(x.source, x.flags);
- tmp.lastIndex = x.lastIndex;
- return tmp;
- }
- if (str === '[object DataView]') {
- return new x.constructor( klona(x.buffer) );
- }
- if (str === '[object ArrayBuffer]') {
- return x.slice(0);
- }
- // ArrayBuffer.isView(x)
- // ~> `new` bcuz `Buffer.slice` => ref
- if (str.slice(-6) === 'Array]') {
- return new x.constructor(x);
- }
- return x;
- }
- exports.klona = klona;
|