Skip to content

Commit

Permalink
feat(#54): Add precision paramaeters for bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Revah committed Jun 1, 2017
1 parent 5016dc5 commit 72ae43e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngx-pipes",
"version": "1.6.0",
"version": "1.6.1",
"author": "Dan Revah",
"description": "Useful angular2 pipes",
"license": "MIT",
Expand Down
9 changes: 9 additions & 0 deletions src/app/pipes/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ export function isNumberFinite(value: any) {
return isNumber(value) && isFinite(value);
}

export function applyPrecision(num: number, precision: number) {
if (precision <= 0) {
return Math.round(num);
}

const tho = 10 ** precision;
return Math.round(num * tho) / tho;
}

export function extractDeepPropertyByMapKey(obj: any, map: string): any {
const keys = map.split('.');
const key = keys.shift();
Expand Down
3 changes: 3 additions & 0 deletions src/app/pipes/math/bytes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ describe('BytesPipe', () => {
expect(pipe.transform(1e9 - 1)).toEqual('999.999999 MB');
expect(pipe.transform(1e9)).toEqual('1 GB');
expect(pipe.transform(1e12)).toEqual('1000 GB');
expect(pipe.transform(12345678)).toEqual('12.345678 MB');
expect(pipe.transform(12345678, 1)).toEqual('12.3 MB');
expect(pipe.transform(12345678, 2)).toEqual('12.35 MB');
});
});
7 changes: 4 additions & 3 deletions src/app/pipes/math/bytes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {PipeTransform, Pipe} from '@angular/core';
import {isNumberFinite} from '../helpers/helpers';
import {isNumberFinite, isUndefined, applyPrecision} from '../helpers/helpers';

@Pipe({name: 'bytes'})
export class BytesPipe implements PipeTransform {
Expand All @@ -10,13 +10,14 @@ export class BytesPipe implements PipeTransform {
{ max: 1e12, type: 'GB' }
];

transform(value: number): string | number {
transform(value: number, precision?: number): string | number {
if (!isNumberFinite(value)) {
return NaN;
}

const format = this.dictionary.find(d => value < d.max) || this.dictionary[this.dictionary.length - 1];
const num = value / (format.max / 1e3);
const calc = value / (format.max / 1e3);
const num = isUndefined(precision) ? calc : applyPrecision(calc, precision);
return `${num} ${format.type}`;
}
}
8 changes: 2 additions & 6 deletions src/app/pipes/math/round.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import {PipeTransform, Pipe} from '@angular/core';
import {applyPrecision} from '../helpers/helpers';

@Pipe({name: 'round'})
export class RoundPipe implements PipeTransform {

transform(num: number, precision: number = 0): number {
if (precision <= 0) {
return Math.round(num);
}

const tho = 10 ** precision;
return Math.round(num * tho) / tho;
return applyPrecision(num, precision);
}
}

0 comments on commit 72ae43e

Please sign in to comment.