Skip to content

Commit

Permalink
feat: add "lite" mode;
Browse files Browse the repository at this point in the history
- copied from `default` mode
- removes Set, Map, TypedArray support
- has `dequal/lite` support parity
  • Loading branch information
lukeed committed Aug 14, 2020
1 parent 0d99163 commit 1762c15
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ node_modules
*-lock.*
*.lock
*.log
dist

/dist
/lite
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
"*.d.ts",
"dist"
],
"modes": {
"default": "src/index.js",
"lite": "src/lite.js"
},
"engines": {
"node": ">= 8"
},
Expand Down
51 changes: 51 additions & 0 deletions src/lite.js
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 1762c15

Please sign in to comment.