index.js 621 B

123456789101112131415161718192021222324252627282930
  1. function klona(val) {
  2. var k, out, tmp;
  3. if (Array.isArray(val)) {
  4. out = Array(k=val.length);
  5. while (k--) out[k] = (tmp=val[k]) && typeof tmp === 'object' ? klona(tmp) : tmp;
  6. return out;
  7. }
  8. if (Object.prototype.toString.call(val) === '[object Object]') {
  9. out = {}; // null
  10. for (k in val) {
  11. if (k === '__proto__') {
  12. Object.defineProperty(out, k, {
  13. value: klona(val[k]),
  14. configurable: true,
  15. enumerable: true,
  16. writable: true,
  17. });
  18. } else {
  19. out[k] = (tmp=val[k]) && typeof tmp === 'object' ? klona(tmp) : tmp;
  20. }
  21. }
  22. return out;
  23. }
  24. return val;
  25. }
  26. exports.klona = klona;