Skip to content

Commit

Permalink
feat: add "json" mode;
Browse files Browse the repository at this point in the history
- for JSON values only!
- 240B // 330-340k op/s
  • Loading branch information
lukeed committed Aug 14, 2020
1 parent 1762c15 commit 8cb217a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ node_modules
*.log

/dist
/json
/lite
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"dist"
],
"modes": {
"json": "src/json.js",
"default": "src/index.js",
"lite": "src/lite.js"
},
Expand Down
28 changes: 28 additions & 0 deletions src/json.js
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 8cb217a

Please sign in to comment.