Skip to content

Commit 0c7f383

Browse files
committed
fix(#635) Locale pipes optimizations
1 parent a363009 commit 0c7f383

File tree

5 files changed

+54
-10
lines changed

5 files changed

+54
-10
lines changed

libs/transloco-locale/src/lib/pipes/transloco-currency.pipe.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ import { TranslocoLocalePipe } from './transloco-locale.pipe';
2222
pure: false,
2323
})
2424
export class TranslocoCurrencyPipe
25-
extends TranslocoLocalePipe
25+
extends TranslocoLocalePipe<number | string, [
26+
display?: 'code' | 'symbol' | 'name',
27+
numberFormatOptions?: NumberFormatOptions,
28+
currencyCode?: Currency,
29+
locale?: Locale]>
2630
implements PipeTransform, OnDestroy
2731
{
2832
constructor(
@@ -43,7 +47,7 @@ export class TranslocoCurrencyPipe
4347
* 1000000 | translocoCurrency: 'symbol' : {minimumFractionDigits: 0 } : USD // $1,000,000
4448
*
4549
*/
46-
transform(
50+
protected override doTransform(
4751
value: number | string,
4852
display: 'code' | 'symbol' | 'name' = 'symbol',
4953
numberFormatOptions: NumberFormatOptions = {},

libs/transloco-locale/src/lib/pipes/transloco-date.pipe.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { TranslocoLocalePipe } from './transloco-locale.pipe';
2222
pure: false,
2323
})
2424
export class TranslocoDatePipe
25-
extends TranslocoLocalePipe
25+
extends TranslocoLocalePipe<ValidDate, [options?: DateFormatOptions, locale?: Locale]>
2626
implements PipeTransform, OnDestroy
2727
{
2828
constructor(
@@ -47,14 +47,22 @@ export class TranslocoDatePipe
4747
* 1 | translocoDate: { dateStyle: 'medium' } // Jan 1, 1970
4848
* '2019-02-08' | translocoDate: { dateStyle: 'medium' } // Feb 8, 2019
4949
*/
50-
transform(date: ValidDate, options: DateFormatOptions = {}, locale?: Locale) {
50+
protected override doTransform(date: ValidDate, options: DateFormatOptions = {}, locale?: Locale) {
5151
if (isNil(date)) return '';
5252
locale = this.getLocale(locale);
5353

5454
return this.translocoLocaleService.localizeDate(date, locale, {
5555
...getDefaultOptions(locale, 'date', this.localeConfig),
5656
...options,
5757
});
58+
}
59+
60+
protected override isSameValue(value?: ValidDate) {
61+
return this.getComparableDate(this.lastValue) === this.getComparableDate(value);
62+
}
63+
64+
private getComparableDate(value?: any) {
65+
return value?.getTime ? value.getTime() : value;
5866
}
5967

6068
ngOnDestroy(): void {

libs/transloco-locale/src/lib/pipes/transloco-decimal.pipe.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { TranslocoLocalePipe } from './transloco-locale.pipe';
2121
pure: false,
2222
})
2323
export class TranslocoDecimalPipe
24-
extends TranslocoLocalePipe
24+
extends TranslocoLocalePipe<string | number, [numberFormatOptions?: NumberFormatOptions, locale?: Locale]>
2525
implements PipeTransform, OnDestroy
2626
{
2727
constructor(
@@ -41,7 +41,7 @@ export class TranslocoDecimalPipe
4141
* 1234567890 | translocoDecimal: {useGrouping: false}: en-US // 1234567890
4242
*
4343
*/
44-
transform(
44+
protected override doTransform(
4545
value: string | number,
4646
numberFormatOptions: NumberFormatOptions = {},
4747
locale?: Locale

libs/transloco-locale/src/lib/pipes/transloco-locale.pipe.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@ import { Subscription } from 'rxjs';
33
import { Locale } from '../../lib/transloco-locale.types';
44
import { TranslocoLocaleService } from '../transloco-locale.service';
55

6-
export class TranslocoLocalePipe {
6+
export abstract class TranslocoLocalePipe<VALUE = unknown, ARGS extends unknown[] = []> {
77
private subscription: Subscription | null =
88
this.translocoLocaleService.localeChanges$.subscribe({
9-
next: () => this.cdr.markForCheck(),
9+
next: () => this.invalidate(),
1010
});
1111

12+
protected lastValue?: VALUE;
13+
protected lastArgs?: string;
14+
15+
protected lastResult = '';
16+
1217
constructor(
1318
protected translocoLocaleService: TranslocoLocaleService,
1419
protected cdr: ChangeDetectorRef
@@ -19,6 +24,33 @@ export class TranslocoLocalePipe {
1924
return locale || this.translocoLocaleService.getLocale()!;
2025
}
2126

27+
transform(value: VALUE, ...args: ARGS): string {
28+
if (this.isSameValue(value) && this.isSameArgs(...args)) {
29+
return this.lastResult;
30+
}
31+
this.lastResult = this.doTransform(value, ...args);
32+
this.lastValue = value;
33+
this.lastArgs = JSON.stringify(args);
34+
return this.lastResult;
35+
}
36+
37+
protected abstract doTransform(value: VALUE, ...args: ARGS): string;
38+
39+
protected isSameValue(value: VALUE): boolean {
40+
return this.lastValue === value;
41+
}
42+
43+
protected isSameArgs(...args: ARGS): boolean {
44+
return JSON.stringify(args) === this.lastArgs;
45+
}
46+
47+
invalidate() {
48+
this.lastValue = undefined;
49+
this.lastArgs = undefined;
50+
this.lastResult = '';
51+
this.cdr.markForCheck();
52+
}
53+
2254
onDestroy(): void {
2355
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2456
this.subscription!.unsubscribe();

libs/transloco-locale/src/lib/pipes/transloco-percent.pipe.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { TranslocoLocalePipe } from './transloco-locale.pipe';
2121
pure: false,
2222
})
2323
export class TranslocoPercentPipe
24-
extends TranslocoLocalePipe
24+
extends TranslocoLocalePipe<number | string, [numberFormatOptions?: NumberFormatOptions, locale?: Locale]>
2525
implements PipeTransform, OnDestroy
2626
{
2727
constructor(
@@ -41,7 +41,7 @@ export class TranslocoPercentPipe
4141
* "1" | translocoPercent : {} : en-US // 100%
4242
*
4343
*/
44-
transform(
44+
protected override doTransform(
4545
value: number | string,
4646
numberFormatOptions: NumberFormatOptions = {},
4747
locale?: Locale

0 commit comments

Comments
 (0)