deep-assign.mjs 478 B

12345678910111213141516171819202122
  1. import { isDef, isObject } from "./basic.mjs";
  2. const { hasOwnProperty } = Object.prototype;
  3. function assignKey(to, from, key) {
  4. const val = from[key];
  5. if (!isDef(val)) {
  6. return;
  7. }
  8. if (!hasOwnProperty.call(to, key) || !isObject(val)) {
  9. to[key] = val;
  10. } else {
  11. to[key] = deepAssign(Object(to[key]), val);
  12. }
  13. }
  14. function deepAssign(to, from) {
  15. Object.keys(from).forEach((key) => {
  16. assignKey(to, from, key);
  17. });
  18. return to;
  19. }
  20. export {
  21. deepAssign
  22. };