deep-assign.ts 577 B

123456789101112131415161718192021222324252627282930
  1. /* eslint-disable no-use-before-define */
  2. import { isDef, isObj } from '.';
  3. const { hasOwnProperty } = Object.prototype;
  4. type Object = {
  5. [key: string]: any;
  6. }
  7. function assignKey(to: Object, from: Object, key: string) {
  8. const val = from[key];
  9. if (!isDef(val)) {
  10. return;
  11. }
  12. if (!hasOwnProperty.call(to, key) || !isObj(val)) {
  13. to[key] = val;
  14. } else {
  15. to[key] = deepAssign(Object(to[key]), from[key]);
  16. }
  17. }
  18. export function deepAssign(to: Object, from: Object) {
  19. Object.keys(from).forEach(key => {
  20. assignKey(to, from, key);
  21. });
  22. return to;
  23. }