From 72ae43e1512a6e70d44b74c2bfd372cb6648085e Mon Sep 17 00:00:00 2001 From: Dan Revah Date: Thu, 1 Jun 2017 14:56:30 +0300 Subject: [PATCH] feat(#54): Add precision paramaeters for bytes --- package.json | 2 +- src/app/pipes/helpers/helpers.ts | 9 +++++++++ src/app/pipes/math/bytes.spec.ts | 3 +++ src/app/pipes/math/bytes.ts | 7 ++++--- src/app/pipes/math/round.ts | 8 ++------ 5 files changed, 19 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 800c7375..64e314ea 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ngx-pipes", - "version": "1.6.0", + "version": "1.6.1", "author": "Dan Revah", "description": "Useful angular2 pipes", "license": "MIT", diff --git a/src/app/pipes/helpers/helpers.ts b/src/app/pipes/helpers/helpers.ts index d75e640a..bc2ca97b 100644 --- a/src/app/pipes/helpers/helpers.ts +++ b/src/app/pipes/helpers/helpers.ts @@ -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(); diff --git a/src/app/pipes/math/bytes.spec.ts b/src/app/pipes/math/bytes.spec.ts index 6333d504..884500a7 100644 --- a/src/app/pipes/math/bytes.spec.ts +++ b/src/app/pipes/math/bytes.spec.ts @@ -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'); }); }); diff --git a/src/app/pipes/math/bytes.ts b/src/app/pipes/math/bytes.ts index a247f47f..bdbe3b33 100644 --- a/src/app/pipes/math/bytes.ts +++ b/src/app/pipes/math/bytes.ts @@ -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 { @@ -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}`; } } diff --git a/src/app/pipes/math/round.ts b/src/app/pipes/math/round.ts index 15b78418..6a953b9a 100644 --- a/src/app/pipes/math/round.ts +++ b/src/app/pipes/math/round.ts @@ -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); } }