Skip to content

Commit 6f6fdd5

Browse files
committed
Improve performance
1 parent 2ef8d55 commit 6f6fdd5

File tree

5 files changed

+101
-60
lines changed

5 files changed

+101
-60
lines changed

package-lock.json

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"@angular/router": "~14.0.0",
4444
"@ngx-translate/core": "^14.0.0",
4545
"@types/jest": "^28.1.1",
46-
"@types/node": "^12.11.1",
46+
"@types/node": "^12.20.55",
4747
"@typescript-eslint/eslint-plugin": "5.27.1",
4848
"@typescript-eslint/parser": "5.27.1",
4949
"eslint": "^8.17.0",

src/lib/translate-format-js-compiler.spec.ts

Lines changed: 86 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ class TestVVTranslateParserComponent {
2525

2626
describe('VVTranslateParser', () => {
2727
let fixture: ComponentFixture<TestVVTranslateParserComponent>;
28+
let compiler: TranslateFormatJsCompiler;
29+
const SELECT_TEXT = 'Ich wohne in einem {text, select, A {Haus} B {Hotel} C {Keller} other {Garten}}.';
30+
const PLURAL_TEXT = 'Gib mir {num, plural, =0 {Kein} =1 {Ein} =2 {Zwei} other {mehr}} Bier';
31+
const HTML_TEXT = "'<h1>Headline</h1>'";
2832
beforeEach(waitForAsync(() => {
2933
TestBed.configureTestingModule({
3034
declarations: [TestVVTranslateParserComponent],
@@ -34,14 +38,14 @@ describe('VVTranslateParser', () => {
3438
provide: TranslateLoader,
3539
useValue: {
3640
getTranslation: () => of({
37-
TEST_SELECT:
38-
'Ich wohne in einem {text, select, A {Haus} B {Hotel} C {Keller} other {Garten}}.',
39-
TEST_PLURAL:
40-
'Gib mir {num, plural, =0 {Kein} =1 {Ein} =2 {Zwei} other {mehr}} Bier',
41-
DEEP: {
42-
TEST: 'deep test',
41+
...{
42+
TEST_SELECT: SELECT_TEXT,
43+
TEST_PLURAL: PLURAL_TEXT,
44+
DEEP: {
45+
TEST: 'deep test',
46+
},
47+
HTML: HTML_TEXT,
4348
},
44-
HTML: "'<h1>Headline</h1>'",
4549
}),
4650
},
4751
},
@@ -53,53 +57,91 @@ describe('VVTranslateParser', () => {
5357
},
5458
}),
5559
],
60+
providers: [TranslateFormatJsCompiler],
5661
}).compileComponents();
5762
fixture = TestBed.createComponent(TestVVTranslateParserComponent);
63+
compiler = TestBed.inject(TranslateFormatJsCompiler);
5864
fixture.detectChanges();
5965
}));
66+
describe('should compileTranslations', () => {
67+
it.each`
68+
text | translation
69+
${'A'} | ${'Ich wohne in einem Haus.'}
70+
${'B'} | ${'Ich wohne in einem Hotel.'}
71+
${'C'} | ${'Ich wohne in einem Keller.'}
72+
${'D'} | ${'Ich wohne in einem Garten.'}
73+
${'E'} | ${'Ich wohne in einem Garten.'}
74+
`(
75+
'should translate select correctly with $text',
76+
({ text, translation }) => {
77+
fixture.componentInstance.text = text;
78+
fixture.detectChanges();
6079

61-
it.each`
62-
text | translation
63-
${'A'} | ${'Ich wohne in einem Haus.'}
64-
${'B'} | ${'Ich wohne in einem Hotel.'}
65-
${'C'} | ${'Ich wohne in einem Keller.'}
66-
${'D'} | ${'Ich wohne in einem Garten.'}
67-
${'E'} | ${'Ich wohne in einem Garten.'}
68-
`('should translate select correctly with $text', ({ text, translation }) => {
69-
fixture.componentInstance.text = text;
70-
fixture.detectChanges();
80+
expect(
81+
fixture.debugElement.nativeElement.querySelector('#select')
82+
.textContent,
83+
).toBe(translation);
84+
},
85+
);
7186

72-
expect(
73-
fixture.debugElement.nativeElement.querySelector('#select').textContent,
74-
).toBe(translation);
75-
});
87+
it.each`
88+
num | translation
89+
${0} | ${'Gib mir Kein Bier'}
90+
${1} | ${'Gib mir Ein Bier'}
91+
${2} | ${'Gib mir Zwei Bier'}
92+
${3} | ${'Gib mir mehr Bier'}
93+
${4} | ${'Gib mir mehr Bier'}
94+
`('should translate plural correctly with $num', ({ num, translation }) => {
95+
fixture.componentInstance.num = num;
96+
fixture.detectChanges();
7697

77-
it.each`
78-
num | translation
79-
${0} | ${'Gib mir Kein Bier'}
80-
${1} | ${'Gib mir Ein Bier'}
81-
${2} | ${'Gib mir Zwei Bier'}
82-
${3} | ${'Gib mir mehr Bier'}
83-
${4} | ${'Gib mir mehr Bier'}
84-
`('should translate plural correctly with $num', ({ num, translation }) => {
85-
fixture.componentInstance.num = num;
86-
fixture.detectChanges();
98+
expect(
99+
fixture.debugElement.nativeElement.querySelector('#plural').textContent,
100+
).toBe(translation);
101+
});
87102

88-
expect(
89-
fixture.debugElement.nativeElement.querySelector('#plural').textContent,
90-
).toBe(translation);
91-
});
103+
it('should support deep', () => {
104+
expect(
105+
fixture.debugElement.nativeElement.querySelector('#deep').textContent,
106+
).toBe('deep test');
107+
});
92108

93-
it('should support deep', () => {
94-
expect(
95-
fixture.debugElement.nativeElement.querySelector('#deep').textContent,
96-
).toBe('deep test');
109+
it('should support html', () => {
110+
expect(
111+
fixture.debugElement.nativeElement.querySelector('#html > h1')
112+
.textContent,
113+
).toBe('Headline');
114+
});
97115
});
98116

99-
it('should support html', () => {
100-
expect(
101-
fixture.debugElement.nativeElement.querySelector('#html > h1')
102-
.textContent,
103-
).toBe('Headline');
117+
describe('should compile', () => {
118+
it.each`
119+
text | translation
120+
${'A'} | ${'Ich wohne in einem Haus.'}
121+
${'B'} | ${'Ich wohne in einem Hotel.'}
122+
${'C'} | ${'Ich wohne in einem Keller.'}
123+
${'D'} | ${'Ich wohne in einem Garten.'}
124+
${'E'} | ${'Ich wohne in einem Garten.'}
125+
`(
126+
'should translate select correctly with $text',
127+
({ text, translation }) => {
128+
expect((compiler.compile(SELECT_TEXT, 'de') as any)({ text })).toBe(
129+
translation,
130+
);
131+
},
132+
);
133+
134+
it.each`
135+
num | translation
136+
${0} | ${'Gib mir Kein Bier'}
137+
${1} | ${'Gib mir Ein Bier'}
138+
${2} | ${'Gib mir Zwei Bier'}
139+
${3} | ${'Gib mir mehr Bier'}
140+
${4} | ${'Gib mir mehr Bier'}
141+
`('should translate plural correctly with $num', ({ num, translation }) => {
142+
expect((compiler.compile(PLURAL_TEXT, 'de') as any)({ num })).toBe(
143+
translation,
144+
);
145+
});
104146
});
105147
});

src/lib/translate-format-js-compiler.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ export class TranslateFormatJsCompiler extends TranslateCompiler {
2222

2323
/** Compile translations object */
2424
compileTranslations(translations: any, lang: string): any {
25-
return this.compileRecursively(translations, lang);
25+
this.compileRecursively(translations, lang);
26+
return translations;
2627
}
2728

2829
/**
@@ -31,16 +32,16 @@ export class TranslateFormatJsCompiler extends TranslateCompiler {
3132
* @param lang from @ngx-translate
3233
* @returns translation object with compile functions.
3334
*/
34-
private compileRecursively(obj: any, lang: string): any {
35-
return Object.keys(obj).reduce((acc: any, key: string) => {
35+
private compileRecursively(obj: any, lang: string): void {
36+
Object.keys(obj).forEach((key: string) => {
3637
const value = obj[key];
3738

38-
return typeof value === 'string'
39-
? { ...acc, [key]: this.compile(value, lang) }
40-
: {
41-
...acc,
42-
[key]: this.compileRecursively(value, lang),
43-
};
44-
}, {});
39+
if (typeof value === 'string') {
40+
// eslint-disable-next-line no-param-reassign
41+
obj[key] = this.compile(value, lang);
42+
} else {
43+
this.compileRecursively(value, lang);
44+
}
45+
});
4546
}
4647
}

tsconfig.spec.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"extends": "./tsconfig.lib.json",
33
"compilerOptions": {
44
"outDir": "./out-tsc/spec",
5-
"types": ["jest"],
5+
"types": ["jest", "node"],
66
"esModuleInterop": true,
77
"emitDecoratorMetadata": true
88
},

0 commit comments

Comments
 (0)