From 28cbd865b7c0d423430c086d83a5e2340d064b9d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Stephan=20M=C3=BCller?= Date: Thu, 26 Apr 2018 12:20:01 +0200 Subject: [PATCH] mgr/dashboard: Convert floating values to bytes MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Now the 'formatter' service can convert floating values to bytes. Signed-off-by: Stephan Müller --- .../shared/services/formatter.service.spec.ts | 65 ++++++++++++------- .../app/shared/services/formatter.service.ts | 10 +-- 2 files changed, 45 insertions(+), 30 deletions(-) diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/formatter.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/formatter.service.spec.ts index c79c8b22ce76e..f32f92889ce68 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/formatter.service.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/formatter.service.spec.ts @@ -1,36 +1,51 @@ -import { inject, TestBed } from '@angular/core/testing'; +import { TestBed } from '@angular/core/testing'; import { FormatterService } from './formatter.service'; describe('FormatterService', () => { + let service: FormatterService; beforeEach(() => { TestBed.configureTestingModule({ providers: [FormatterService] }); + service = new FormatterService(); }); - it('should be created', - inject([FormatterService], (service: FormatterService) => { - expect(service).toBeTruthy(); - })); - - it('should not convert 10xyz to bytes (failure)', - inject([FormatterService], (service: FormatterService) => { - const bytes = service.toBytes('10xyz'); - expect(bytes).toBeNull(); - })); - - it('should convert 4815162342 to bytes', - inject([FormatterService], (service: FormatterService) => { - const bytes = service.toBytes('4815162342'); - expect(bytes).toEqual(jasmine.any(Number)); - expect(bytes).toBe(4815162342); - })); - - it('should convert 100M to bytes', - inject([FormatterService], (service: FormatterService) => { - const bytes = service.toBytes('100M'); - expect(bytes).toEqual(jasmine.any(Number)); - expect(bytes).toBe(104857600); - })); + it('should be created', () => { + expect(service).toBeTruthy(); + }); + + it('should not convert 10xyz to bytes (failure)', () => { + const bytes = service.toBytes('10xyz'); + expect(bytes).toBeNull(); + }); + + it('should not convert 1.1.1KiB to bytes (failure)', () => { + const bytes = service.toBytes('1.1.1KiB'); + expect(bytes).toBeNull(); + }); + + it('should convert 4815162342 to bytes', () => { + const bytes = service.toBytes('4815162342'); + expect(bytes).toEqual(jasmine.any(Number)); + expect(bytes).toBe(4815162342); + }); + + it('should convert 100M to bytes', () => { + const bytes = service.toBytes('100M'); + expect(bytes).toEqual(jasmine.any(Number)); + expect(bytes).toBe(104857600); + }); + + it('should convert 1.532KiB to bytes', () => { + const bytes = service.toBytes('1.532KiB'); + expect(bytes).toEqual(jasmine.any(Number)); + expect(bytes).toBe(Math.floor(1.532 * 1024)); + }); + + it('should convert 0.000000000001TiB to bytes', () => { + const bytes = service.toBytes('0.000000000001TiB'); + expect(bytes).toEqual(jasmine.any(Number)); + expect(bytes).toBe(1); + }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/formatter.service.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/formatter.service.ts index 82b53db337f71..7c6a95d3048a8 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/formatter.service.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/formatter.service.ts @@ -86,15 +86,15 @@ export class FormatterService { 'yb': Math.pow(base, 8), 'yib': Math.pow(base, 8) }; - const m = RegExp('^(\\d+)\\s*(B|K(B|iB)?|M(B|iB)?|G(B|iB)?|T(B|iB)?|P(B|iB)?|' + + const m = RegExp('^(\\d+(\.\\d+)?)\\s*(B|K(B|iB)?|M(B|iB)?|G(B|iB)?|T(B|iB)?|P(B|iB)?|' + 'E(B|iB)?|Z(B|iB)?|Y(B|iB)?)?$', 'i').exec(value); if (m === null) { return null; } - let bytes = parseInt(m[1], 10); - if (_.isString(m[2])) { - bytes = bytes * units[m[2].toLowerCase()]; + let bytes = parseFloat(m[1]); + if (_.isString(m[3])) { + bytes = bytes * units[m[3].toLowerCase()]; } - return bytes; + return Math.floor(bytes); } } -- 2.39.5