From b060c789bf6543ba552d19738d83294491e6e394 Mon Sep 17 00:00:00 2001 From: Ed Morales Date: Mon, 27 Feb 2017 22:22:27 -0800 Subject: [PATCH] feat(digits): l10n support for TdDigitsPipe (#378) * feat(digits): i10n support for TdDigitsPipe leverage LOCALE_ID and DecimalPipe to support l10n within TdDigitsPipe and update unit tests * fix(): return argument if not a number * fix(): fix comment --- .../common/pipes/digits/digits.pipe.spec.ts | 28 +++++++++++++++---- .../core/common/pipes/digits/digits.pipe.ts | 18 ++++++++---- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/platform/core/common/pipes/digits/digits.pipe.spec.ts b/src/platform/core/common/pipes/digits/digits.pipe.spec.ts index 7ae99ae383..f869ae2099 100644 --- a/src/platform/core/common/pipes/digits/digits.pipe.spec.ts +++ b/src/platform/core/common/pipes/digits/digits.pipe.spec.ts @@ -2,20 +2,23 @@ import { TdDigitsPipe } from './digits.pipe'; describe('TdDigitsPipe', () => { let pipe: TdDigitsPipe; + let l10nPipe: TdDigitsPipe; beforeEach(() => { pipe = new TdDigitsPipe(); + l10nPipe = new TdDigitsPipe('es'); }); it('should return with an empty or invalid input', () => { - expect(pipe.transform('notanumber', undefined)).toEqual('Invalid Number'); - expect(pipe.transform(NaN, undefined)).toEqual('Invalid Number'); - expect(pipe.transform(undefined, undefined)).toEqual('Invalid Number'); + expect(pipe.transform('notanumber', undefined)).toEqual('notanumber'); + expect(pipe.transform(NaN, undefined)).toEqual(NaN); + expect(pipe.transform(undefined, undefined)).toEqual(undefined); }); it('should return formatted digits', () => { + /* transformations in 'en'*/ expect(pipe.transform('34', undefined)).toEqual('34'); - expect(pipe.transform(0.45, undefined)).toEqual(0.45); - expect(pipe.transform(0.724, undefined)).toEqual(0.724); + expect(pipe.transform(0.45, 1)).toEqual('0.5'); + expect(pipe.transform(0.724, 2)).toEqual('0.72'); expect(pipe.transform(535, undefined)).toEqual('535'); expect(pipe.transform(138540, undefined)).toEqual('138.5 K'); expect(pipe.transform(138540, 2)).toEqual('138.54 K'); @@ -26,5 +29,20 @@ describe('TdDigitsPipe', () => { expect(pipe.transform(3.81861e+10, undefined)).toEqual('38.2 B'); expect(pipe.transform(1.890381861e+14, undefined)).toEqual('189 T'); expect(pipe.transform(5.35765e+16, undefined)).toEqual('53.6 Q'); + + /* transformations in 'es'*/ + expect(l10nPipe.transform('34', undefined)).toEqual('34'); + expect(l10nPipe.transform(0.45, 1)).toEqual('0,5'); + expect(l10nPipe.transform(0.724, 2)).toEqual('0,72'); + expect(l10nPipe.transform(535, undefined)).toEqual('535'); + expect(l10nPipe.transform(138540, undefined)).toEqual('138,5 K'); + expect(l10nPipe.transform(138540, 2)).toEqual('138,54 K'); + expect(l10nPipe.transform(1571800, undefined)).toEqual('1,6 M'); + expect(l10nPipe.transform(1571800, 3)).toEqual('1,572 M'); + expect(l10nPipe.transform(10000000, undefined)).toEqual('10 M'); + expect(l10nPipe.transform(10200000, undefined)).toEqual('10,2 M'); + expect(l10nPipe.transform(3.81861e+10, undefined)).toEqual('38,2 B'); + expect(l10nPipe.transform(1.890381861e+14, undefined)).toEqual('189 T'); + expect(l10nPipe.transform(5.35765e+16, undefined)).toEqual('53,6 Q'); }); }); diff --git a/src/platform/core/common/pipes/digits/digits.pipe.ts b/src/platform/core/common/pipes/digits/digits.pipe.ts index f2d0183bfc..d644bf648e 100644 --- a/src/platform/core/common/pipes/digits/digits.pipe.ts +++ b/src/platform/core/common/pipes/digits/digits.pipe.ts @@ -1,24 +1,32 @@ -import { Pipe, PipeTransform } from '@angular/core'; +import { Pipe, PipeTransform, Inject, LOCALE_ID } from '@angular/core'; +import { DecimalPipe } from '@angular/common'; @Pipe({ name: 'digits', }) export class TdDigitsPipe implements PipeTransform { + + private _decimalPipe: DecimalPipe; + + constructor(@Inject(LOCALE_ID) private _locale: string = 'en') { + this._decimalPipe = new DecimalPipe(this._locale); + } + /* `digits` needs to be type `digits: any` or TypeScript complains */ transform(digits: any, precision: number = 1): string { if (digits === 0) { return '0'; } else if (isNaN(parseInt(digits, 10))) { - /* If not a valid number, return 'Invalid Number' */ - return 'Invalid Number'; - } else if (digits < 1) { + /* If not a valid number, return the value */ return digits; + } else if (digits < 1) { + return this._decimalPipe.transform(digits.toFixed(precision)); } let k: number = 1000; let sizes: string[] = ['', 'K', 'M', 'B', 'T', 'Q']; let i: number = Math.floor(Math.log(digits) / Math.log(k)); let size: string = sizes[i]; - return parseFloat((digits / Math.pow(k, i)).toFixed(precision)) + (size ? ' ' + size : ''); + return this._decimalPipe.transform(parseFloat((digits / Math.pow(k, i)).toFixed(precision))) + (size ? ' ' + size : ''); } }