mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-21 21:44:40 +03:00
fix(Data Storage Converter): make precision even for 1.00
This commit is contained in:
1 parent
c1b0694097
commit
c8f0d53821
2 files changed
+18
-18
No files matched your search
@@ -4,15 +4,15 @@ import { convertStorageAndRateUnitsDisplay, displayStorageAndRateUnits } from '.
|
|||||||
describe('data-storage-unit-converter', () => {
|
describe('data-storage-unit-converter', () => {
|
||||||
describe('convertStorageAndRateUnitsDisplay', () => {
|
describe('convertStorageAndRateUnitsDisplay', () => {
|
||||||
it('convert from same base units', () => {
|
it('convert from same base units', () => {
|
||||||
expect(convertStorageAndRateUnitsDisplay({ value: 1024 * 1024, fromUnit: 'B', toUnit: 'MiB' })).toBe('1');
|
expect(convertStorageAndRateUnitsDisplay({ value: 1024 * 1024, fromUnit: 'B', toUnit: 'MiB', precision: 0 })).toBe('1');
|
||||||
expect(convertStorageAndRateUnitsDisplay({ value: 1024, fromUnit: 'KiB', toUnit: 'MiB' })).toBe('1');
|
expect(convertStorageAndRateUnitsDisplay({ value: 1024, fromUnit: 'KiB', toUnit: 'MiB', precision: 0 })).toBe('1');
|
||||||
expect(convertStorageAndRateUnitsDisplay({ value: 1, fromUnit: 'MiB', toUnit: 'KiB' })).toBe('1024');
|
expect(convertStorageAndRateUnitsDisplay({ value: 1, fromUnit: 'MiB', toUnit: 'KiB', precision: 0 })).toBe('1024');
|
||||||
expect(convertStorageAndRateUnitsDisplay({ value: 1000, fromUnit: 'MB', toUnit: 'GB' })).toBe('1');
|
expect(convertStorageAndRateUnitsDisplay({ value: 1000, fromUnit: 'MB', toUnit: 'GB', precision: 0 })).toBe('1');
|
||||||
expect(convertStorageAndRateUnitsDisplay({ value: 1024, fromUnit: 'MB', toUnit: 'MB' })).toBe('1024');
|
expect(convertStorageAndRateUnitsDisplay({ value: 1024, fromUnit: 'MB', toUnit: 'MB', precision: 0 })).toBe('1024');
|
||||||
expect(convertStorageAndRateUnitsDisplay({ value: 1, fromUnit: 'MB', toUnit: 'KB' })).toBe('1000');
|
expect(convertStorageAndRateUnitsDisplay({ value: 1, fromUnit: 'MB', toUnit: 'KB', precision: 0 })).toBe('1000');
|
||||||
expect(convertStorageAndRateUnitsDisplay({ value: 1024, fromUnit: 'MiB', toUnit: 'GiB' })).toBe('1');
|
expect(convertStorageAndRateUnitsDisplay({ value: 1024, fromUnit: 'MiB', toUnit: 'GiB', precision: 0 })).toBe('1');
|
||||||
expect(convertStorageAndRateUnitsDisplay({ value: 1000, fromUnit: 'MB', toUnit: 'GB' })).toBe('1');
|
expect(convertStorageAndRateUnitsDisplay({ value: 1000, fromUnit: 'MB', toUnit: 'GB', precision: 0 })).toBe('1');
|
||||||
expect(convertStorageAndRateUnitsDisplay({ value: 1000, fromUnit: 'Mb', toUnit: 'Gb' })).toBe('1');
|
expect(convertStorageAndRateUnitsDisplay({ value: 1000, fromUnit: 'Mb', toUnit: 'Gb', precision: 0 })).toBe('1');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('convert between base units', () => {
|
it('convert between base units', () => {
|
||||||
@@ -21,19 +21,19 @@ describe('data-storage-unit-converter', () => {
|
|||||||
expect(convertStorageAndRateUnitsDisplay({ value: 1000 * 1000, fromUnit: 'B', toUnit: 'MiB' })).toBe('0.954');
|
expect(convertStorageAndRateUnitsDisplay({ value: 1000 * 1000, fromUnit: 'B', toUnit: 'MiB' })).toBe('0.954');
|
||||||
expect(convertStorageAndRateUnitsDisplay({ value: 1024, fromUnit: 'KB', toUnit: 'MiB' })).toBe('0.977');
|
expect(convertStorageAndRateUnitsDisplay({ value: 1024, fromUnit: 'KB', toUnit: 'MiB' })).toBe('0.977');
|
||||||
expect(convertStorageAndRateUnitsDisplay({ value: 1000, fromUnit: 'MiB', toUnit: 'MB' })).toBe('1048.576');
|
expect(convertStorageAndRateUnitsDisplay({ value: 1000, fromUnit: 'MiB', toUnit: 'MB' })).toBe('1048.576');
|
||||||
expect(convertStorageAndRateUnitsDisplay({ value: 1, fromUnit: 'MB', toUnit: 'Mb' })).toBe('8');
|
expect(convertStorageAndRateUnitsDisplay({ value: 1, fromUnit: 'MB', toUnit: 'Mb', precision: 0 })).toBe('8');
|
||||||
expect(convertStorageAndRateUnitsDisplay({ value: 1000, fromUnit: 'KB', toUnit: 'Kb' })).toBe('8000');
|
expect(convertStorageAndRateUnitsDisplay({ value: 1000, fromUnit: 'KB', toUnit: 'Kb', precision: 0 })).toBe('8000');
|
||||||
expect(convertStorageAndRateUnitsDisplay({ value: 1000, fromUnit: 'KiB', toUnit: 'Kb' })).toBe('8192');
|
expect(convertStorageAndRateUnitsDisplay({ value: 1000, fromUnit: 'KiB', toUnit: 'Kb', precision: 0 })).toBe('8192');
|
||||||
expect(convertStorageAndRateUnitsDisplay({ value: 8, fromUnit: 'Mb', toUnit: 'MB' })).toBe('1');
|
expect(convertStorageAndRateUnitsDisplay({ value: 8, fromUnit: 'Mb', toUnit: 'MB', precision: 0 })).toBe('1');
|
||||||
|
|
||||||
expect(convertStorageAndRateUnitsDisplay({ value: 1, fromUnit: 'Mb', toUnit: 'KB' })).toBe('125');
|
expect(convertStorageAndRateUnitsDisplay({ value: 1, fromUnit: 'Mb', toUnit: 'KB', precision: 0 })).toBe('125');
|
||||||
expect(convertStorageAndRateUnitsDisplay({ value: 125, fromUnit: 'KB', toUnit: 'Mb' })).toBe('1');
|
expect(convertStorageAndRateUnitsDisplay({ value: 125, fromUnit: 'KB', toUnit: 'Mb', precision: 0 })).toBe('1');
|
||||||
|
|
||||||
expect(convertStorageAndRateUnitsDisplay({ value: 1, fromUnit: 'MiB', toUnit: 'Kb' })).toBe('8388.608');
|
expect(convertStorageAndRateUnitsDisplay({ value: 1, fromUnit: 'MiB', toUnit: 'Kb' })).toBe('8388.608');
|
||||||
expect(convertStorageAndRateUnitsDisplay({ value: 8388.608, fromUnit: 'Kb', toUnit: 'MiB' })).toBe('1');
|
expect(convertStorageAndRateUnitsDisplay({ value: 8388.608, fromUnit: 'Kb', toUnit: 'MiB', precision: 0 })).toBe('1');
|
||||||
});
|
});
|
||||||
it('convert with unit display', () => {
|
it('convert with unit display', () => {
|
||||||
expect(convertStorageAndRateUnitsDisplay({ value: 1024 * 1024, fromUnit: 'B', toUnit: 'MiB', appendUnit: true })).toBe('1MiB');
|
expect(convertStorageAndRateUnitsDisplay({ value: 1024 * 1024, fromUnit: 'B', toUnit: 'MiB', appendUnit: true, precision: 2 })).toBe('1.00MiB');
|
||||||
});
|
});
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ export type AllSupportedUnits = BibytesUnits | BytesUnits | BitsUnits;
|
|||||||
export function displayStorageAndRateUnits(
|
export function displayStorageAndRateUnits(
|
||||||
{ value, unit, precision = 3, appendUnit = false }:
|
{ value, unit, precision = 3, appendUnit = false }:
|
||||||
{ value: number; unit: AllSupportedUnits; precision?: number ; appendUnit?: boolean }): string {
|
{ value: number; unit: AllSupportedUnits; precision?: number ; appendUnit?: boolean }): string {
|
||||||
return value.toFixed(precision).replace(/0+$/, '').replace(/\.$/, '') + (appendUnit ? unit : ''); // NOSONAR
|
return value.toFixed(precision) + (appendUnit ? unit : ''); // NOSONAR
|
||||||
}
|
}
|
||||||
|
|
||||||
export function convertStorageAndRateUnitsDisplay(
|
export function convertStorageAndRateUnitsDisplay(
|
||||||
|
|||||||
Reference in new issue
Block a user