Skip to content

Commit

Permalink
perf(deepmerge-ts): use local method instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphaël Balet committed Sep 24, 2024
1 parent 951175a commit 370e008
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
10 changes: 1 addition & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngx-translate-multi-http-loader",
"version": "18.3.0",
"version": "18.2.0",
"license": "MIT",
"author": "Raphael Balet",
"maintainers": [
Expand All @@ -16,7 +16,6 @@
"@angular/common": "^18.2.3",
"@angular/core": "^18.2.3",
"@ngx-translate/core": "^15.0.0",
"deepmerge-ts": "^7.1.0",
"rxjs": "^7.8.1",
"tslib": "2.6.3"
},
Expand Down
3 changes: 1 addition & 2 deletions projects/multi-http-loader/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngx-translate-multi-http-loader",
"version": "18.3.0",
"version": "18.2.0",
"license": "MIT",
"author": {
"name": "Raphaël Balet",
Expand Down Expand Up @@ -29,7 +29,6 @@
"@angular/common": ">=13.0.0",
"@angular/core": ">=13.0.0",
"@ngx-translate/core": ">=15.0.0",
"deepmerge-ts": "^7.1.0",
"rxjs": "^7.8.1"
}
}
33 changes: 31 additions & 2 deletions projects/multi-http-loader/src/lib/multi-http-loader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { HttpBackend, HttpClient } from '@angular/common/http'
import { TranslateLoader } from '@ngx-translate/core'
import { deepmerge } from 'deepmerge-ts'
import { forkJoin, Observable, of } from 'rxjs'
import { catchError, map } from 'rxjs/operators'

Expand Down Expand Up @@ -36,6 +35,36 @@ export class MultiTranslateHttpLoader implements TranslateLoader {
)
})

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

// @ToDo: Use it from ngx-translate once it gets exported: https://github.com/rbalet/ngx-translate-multi-http-loader/issues/35
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
}
}

0 comments on commit 370e008

Please sign in to comment.