Upstream lukeed/klona state of the touched files before fb0cab4de3
This commit is contained in:
commit
2460d7b6ef
9 files changed
+322
No files matched your search
+53
@@ -0,0 +1,53 @@
|
||||
function set(obj, key, val) {
|
||||
if (typeof val.value === 'object') val.value = klona(val.value);
|
||||
if (!val.enumerable || val.get || val.set || !val.configurable || !val.writable || key === '__proto__') {
|
||||
Object.defineProperty(obj, key, val);
|
||||
} else obj[key] = val.value;
|
||||
}
|
||||
|
||||
export function klona(x) {
|
||||
if (typeof x !== 'object') return x;
|
||||
|
||||
var i=0, k, list, tmp, str=Object.prototype.toString.call(x);
|
||||
|
||||
if (str === '[object Object]') {
|
||||
tmp = Object.create(x.__proto__ || null);
|
||||
} else if (str === '[object Array]') {
|
||||
tmp = Array(x.length);
|
||||
} else if (str === '[object Set]') {
|
||||
tmp = new Set;
|
||||
x.forEach(function (val) {
|
||||
tmp.add(klona(val));
|
||||
});
|
||||
} else if (str === '[object Map]') {
|
||||
tmp = new Map;
|
||||
x.forEach(function (val, key) {
|
||||
tmp.set(klona(key), klona(val));
|
||||
});
|
||||
} else if (str === '[object Date]') {
|
||||
tmp = new Date(+x);
|
||||
} else if (str === '[object RegExp]') {
|
||||
tmp = new RegExp(x.source, x.flags);
|
||||
} else if (str === '[object DataView]') {
|
||||
tmp = new x.constructor( klona(x.buffer) );
|
||||
} else if (str === '[object ArrayBuffer]') {
|
||||
tmp = x.slice(0);
|
||||
} else if (str.slice(-6) === 'Array]') {
|
||||
// ArrayBuffer.isView(x)
|
||||
// ~> `new` bcuz `Buffer.slice` => ref
|
||||
tmp = new x.constructor(x);
|
||||
}
|
||||
|
||||
if (tmp) {
|
||||
for (list=Object.getOwnPropertySymbols(x); i < list.length; i++) {
|
||||
set(tmp, list[i], Object.getOwnPropertyDescriptor(x, list[i]));
|
||||
}
|
||||
|
||||
for (i=0, list=Object.getOwnPropertyNames(x); i < list.length; i++) {
|
||||
if (Object.hasOwnProperty.call(tmp, k=list[i]) && tmp[k] === x[k]) continue;
|
||||
set(tmp, k, Object.getOwnPropertyDescriptor(x, k));
|
||||
}
|
||||
}
|
||||
|
||||
return tmp || x;
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
export 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 (x.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;
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
export function klona(val) {
|
||||
var k, out, tmp;
|
||||
|
||||
if (Array.isArray(val)) {
|
||||
out = Array(k=val.length);
|
||||
while (k--) out[k] = (tmp=val[k]) && typeof tmp === 'object' ? klona(tmp) : tmp;
|
||||
return out;
|
||||
}
|
||||
|
||||
if (Object.prototype.toString.call(val) === '[object Object]') {
|
||||
out = {}; // null
|
||||
for (k in val) {
|
||||
if (k === '__proto__') {
|
||||
Object.defineProperty(out, k, {
|
||||
value: klona(val[k]),
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
writable: true,
|
||||
});
|
||||
} else {
|
||||
out[k] = (tmp=val[k]) && typeof tmp === 'object' ? klona(tmp) : tmp;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
return val;
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
export 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 (x.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 Date]') {
|
||||
return new Date(+x);
|
||||
}
|
||||
|
||||
if (str === '[object RegExp]') {
|
||||
tmp = new RegExp(x.source, x.flags);
|
||||
tmp.lastIndex = x.lastIndex;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import * as suites from './suites';
|
||||
import { klona } from '../src/full';
|
||||
|
||||
suites.API(klona);
|
||||
|
||||
suites.Strings(klona);
|
||||
suites.Booleans(klona);
|
||||
suites.Numbers(klona);
|
||||
suites.Nully(klona);
|
||||
|
||||
suites.Dates(klona);
|
||||
suites.RegExps(klona);
|
||||
|
||||
suites.Objects(klona);
|
||||
suites.Arrays(klona);
|
||||
|
||||
suites.Functions(klona);
|
||||
suites.Pollutions(klona);
|
||||
suites.Classes(klona);
|
||||
|
||||
suites.Maps(klona);
|
||||
suites.Sets(klona);
|
||||
|
||||
suites.TypedArrays(klona);
|
||||
|
||||
suites.Symbols(klona);
|
||||
suites.Descriptors(klona);
|
||||
suites.Dicts(klona);
|
||||
@@ -0,0 +1,24 @@
|
||||
import * as suites from './suites';
|
||||
import { klona } from '../src/index';
|
||||
|
||||
suites.API(klona);
|
||||
|
||||
suites.Strings(klona);
|
||||
suites.Booleans(klona);
|
||||
suites.Numbers(klona);
|
||||
suites.Nully(klona);
|
||||
|
||||
suites.Dates(klona);
|
||||
suites.RegExps(klona);
|
||||
|
||||
suites.Objects(klona);
|
||||
suites.Arrays(klona);
|
||||
|
||||
suites.Functions(klona);
|
||||
suites.Pollutions(klona);
|
||||
suites.Classes(klona);
|
||||
|
||||
suites.Maps(klona);
|
||||
suites.Sets(klona);
|
||||
|
||||
suites.TypedArrays(klona);
|
||||
@@ -0,0 +1,14 @@
|
||||
import * as suites from './suites';
|
||||
import { klona } from '../src/json';
|
||||
|
||||
suites.API(klona);
|
||||
|
||||
suites.Strings(klona);
|
||||
suites.Booleans(klona);
|
||||
suites.Numbers(klona);
|
||||
suites.Nully(klona);
|
||||
|
||||
suites.Objects(klona);
|
||||
suites.Arrays(klona);
|
||||
|
||||
suites.Pollutions(klona);
|
||||
@@ -0,0 +1,19 @@
|
||||
import * as suites from './suites';
|
||||
import { klona } from '../src/lite';
|
||||
|
||||
suites.API(klona);
|
||||
|
||||
suites.Strings(klona);
|
||||
suites.Booleans(klona);
|
||||
suites.Numbers(klona);
|
||||
suites.Nully(klona);
|
||||
|
||||
suites.Dates(klona);
|
||||
suites.RegExps(klona);
|
||||
|
||||
suites.Objects(klona);
|
||||
suites.Arrays(klona);
|
||||
|
||||
suites.Functions(klona);
|
||||
suites.Pollutions(klona);
|
||||
suites.Classes(klona);
|
||||
@@ -0,0 +1,24 @@
|
||||
export { default as API } from './api';
|
||||
|
||||
export { default as Numbers } from './number';
|
||||
export { default as Booleans } from './boolean';
|
||||
export { default as Strings } from './string';
|
||||
export { default as Nully } from './nullish';
|
||||
|
||||
export { default as Maps } from './map';
|
||||
export { default as Sets } from './set';
|
||||
|
||||
export { default as TypedArrays } from './typedarray';
|
||||
export { default as Descriptors } from './descriptor';
|
||||
export { default as Symbols } from './symbol';
|
||||
|
||||
export { default as Dates } from './date';
|
||||
export { default as RegExps } from './regexp';
|
||||
|
||||
export { default as Dicts } from './dictionary';
|
||||
export { default as Objects } from './object';
|
||||
export { default as Arrays } from './array';
|
||||
|
||||
export { default as Functions } from './function';
|
||||
export { default as Pollutions } from './pollution';
|
||||
export { default as Classes } from './class';
|
||||
Reference in new issue
Block a user