diff --git a/.gitignore b/.gitignore index 1506892..f39bc43 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ node_modules *.log /dist +/json /lite diff --git a/package.json b/package.json index b5f79c7..0f336ab 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "dist" ], "modes": { + "json": "src/json.js", "default": "src/index.js", "lite": "src/lite.js" }, diff --git a/src/json.js b/src/json.js new file mode 100644 index 0000000..eae543d --- /dev/null +++ b/src/json.js @@ -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; +}