diff --git a/.gitignore b/.gitignore index 9c94371..1506892 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,6 @@ node_modules *-lock.* *.lock *.log -dist + +/dist +/lite diff --git a/package.json b/package.json index e64236d..b5f79c7 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,10 @@ "*.d.ts", "dist" ], + "modes": { + "default": "src/index.js", + "lite": "src/lite.js" + }, "engines": { "node": ">= 8" }, diff --git a/src/lite.js b/src/lite.js new file mode 100644 index 0000000..d0ee09d --- /dev/null +++ b/src/lite.js @@ -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 (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 Date]') { + return new Date(+x); + } + + if (str === '[object RegExp]') { + tmp = new RegExp(x.source, x.flags); + tmp.lastIndex = x.lastIndex; + return tmp; + } + + return x; +}