Skip to content

Commit

Permalink
feat(digits): l10n support for TdDigitsPipe (#378)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
emoralesb05 authored and richavyas committed Feb 28, 2017
1 parent ec215c9 commit b060c78
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
28 changes: 23 additions & 5 deletions src/platform/core/common/pipes/digits/digits.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
});
});
18 changes: 13 additions & 5 deletions src/platform/core/common/pipes/digits/digits.pipe.ts
Original file line number Diff line number Diff line change
@@ -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 : '');
}
}

0 comments on commit b060c78

Please sign in to comment.