From 8cb217a6f8da579b2d420da6991beb4df59ac65d Mon Sep 17 00:00:00 2001 From: Luke Edwards Date: Thu, 13 Aug 2020 13:02:18 -0700 Subject: [PATCH] feat: add "json" mode; - for JSON values only! - 240B // 330-340k op/s --- .gitignore | 1 + package.json | 1 + src/json.js | 28 ++++++++++++++++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 src/json.js 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; +}