clone.js 524 B

1234567891011121314151617181920212223242526272829
  1. /**
  2. * The MIT License (MIT)
  3. * Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
  4. */
  5. 'use strict';
  6. /**
  7. * Performs a deep copy of an simple object.
  8. * Only handles scalar values, arrays and objects.
  9. *
  10. * @param obj Object
  11. */
  12. module.exports = function clone(obj) {
  13. if (obj === null || typeof obj !== 'object') {
  14. return obj;
  15. }
  16. var res = void 0;
  17. if (Array.isArray(obj)) {
  18. res = [];
  19. } else {
  20. res = {};
  21. }
  22. for (var i in obj) {
  23. res[i] = clone(obj[i]);
  24. }
  25. return res;
  26. };