deep-clone.mjs 379 B

1234567891011121314151617181920
  1. import { isDef, isObject } from "./basic.mjs";
  2. function deepClone(obj) {
  3. if (!isDef(obj)) {
  4. return obj;
  5. }
  6. if (Array.isArray(obj)) {
  7. return obj.map((item) => deepClone(item));
  8. }
  9. if (isObject(obj)) {
  10. const to = {};
  11. Object.keys(obj).forEach((key) => {
  12. to[key] = deepClone(obj[key]);
  13. });
  14. return to;
  15. }
  16. return obj;
  17. }
  18. export {
  19. deepClone
  20. };