rbalet / ngx-translate-multi-http-loader

A loader for ngx-translate that loads translations with http calls
MIT License
77 stars 15 forks source link

perf(mergeDeep): use from ngx-translate #35

Closed rbalet closed 4 weeks ago

rbalet commented 2 months ago

Description

Once the ngx-translate library have exported mergeDeep, remove the local created one for them.

From

...
    return forkJoin(requests).pipe(
      map((response) => response.reduce((acc, curr) => this.mergeDeep(acc, curr), {})),
    )
  }

  // @ToDo: Use it from ngx-translate once it gets exported: 
  isObject(item: any): boolean {
    return item && typeof item === 'object' && !Array.isArray(item)
  }

  mergeDeep(target: any, source: any): any {
    const output = Object.assign({}, target)

    if (!this.isObject(target)) {
      return this.mergeDeep({}, source)
    }

    if (this.isObject(target) && this.isObject(source)) {
      Object.keys(source).forEach((key: any) => {
        if (this.isObject(source[key])) {
          if (!(key in target)) {
            Object.assign(output, { [key]: source[key] })
          } else {
            output[key] = this.mergeDeep(target[key], source[key])
          }
        } else {
          Object.assign(output, { [key]: source[key] })
        }
      })
    }
    return output
  }

To

import { mergeDeep } from '@ngx-translate/core'

...
    return forkJoin(requests).pipe(
      map((response) => response.reduce((acc, curr) => this.mergeDeep(acc, curr), {})),
    )
  }