Skip to content

Commit 0444455

Browse files
committed
add merge to utils
1 parent 94c3c7e commit 0444455

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

src/i18n/i18n.service.ts

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Injectable } from "@angular/core";
22
import { BehaviorSubject } from "rxjs";
33
import { map } from "rxjs/operators";
4+
import { merge } from "./../utils/object";
45

56
const EN = require("./en.json");
67

@@ -34,28 +35,6 @@ export const replace = (subject, variables) => subject.pipe(
3435
})
3536
);
3637

37-
// custom deep object merge
38-
const merge = (target, ...objects) => {
39-
for (const object of objects) {
40-
for (const key in object) {
41-
if (object.hasOwnProperty(key)) {
42-
// since we're dealing just with JSON this simple check should be enough
43-
if (object[key] instanceof Object) {
44-
if (!target[key]) {
45-
target[key] = {};
46-
}
47-
// recursivly merge into the target
48-
// most translations only run 3 or 4 levels deep, so no stack explosions
49-
target[key] = merge(target[key], object[key]);
50-
} else {
51-
target[key] = object[key];
52-
}
53-
}
54-
}
55-
}
56-
return target;
57-
};
58-
5938
/**
6039
* The I18n service is a minimal internal singleton service used to supply our components with translated strings.
6140
*

src/utils/object.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// custom deep object merge
2+
export const merge = (target, ...objects) => {
3+
for (const object of objects) {
4+
for (const key in object) {
5+
if (object.hasOwnProperty(key)) {
6+
// since we're dealing just with JSON this simple check should be enough
7+
if (object[key] instanceof Object) {
8+
if (!target[key]) {
9+
target[key] = {};
10+
}
11+
// recursivly merge into the target
12+
// most translations only run 3 or 4 levels deep, so no stack explosions
13+
target[key] = merge(target[key], object[key]);
14+
} else {
15+
target[key] = object[key];
16+
}
17+
}
18+
}
19+
}
20+
return target;
21+
};

0 commit comments

Comments
 (0)