Skip to content

Commit

Permalink
fix: review
Browse files Browse the repository at this point in the history
  • Loading branch information
Raubzeug committed Oct 21, 2024
1 parent f42551f commit 7eb74ad
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 35 deletions.
46 changes: 23 additions & 23 deletions src/utils/bytesParsers/__test__/formatBytes.test.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
import {unbreakableGap} from '../../utils';
import {UNBREAKABLE_GAP} from '../../utils';
import {formatBytes} from '../formatBytes';

describe('formatBytes', () => {
it('should work with only value', () => {
expect(formatBytes({value: 100})).toBe(`100${unbreakableGap}B`);
expect(formatBytes({value: 100_000})).toBe(`100${unbreakableGap}KB`);
expect(formatBytes({value: 100_000_000})).toBe(`100${unbreakableGap}MB`);
expect(formatBytes({value: 100_000_000_000})).toBe(`100${unbreakableGap}GB`);
expect(formatBytes({value: 100_000_000_000_000})).toBe(`100${unbreakableGap}TB`);
expect(formatBytes({value: 100})).toBe(`100${UNBREAKABLE_GAP}B`);
expect(formatBytes({value: 100_000})).toBe(`100${UNBREAKABLE_GAP}KB`);
expect(formatBytes({value: 100_000_000})).toBe(`100${UNBREAKABLE_GAP}MB`);
expect(formatBytes({value: 100_000_000_000})).toBe(`100${UNBREAKABLE_GAP}GB`);
expect(formatBytes({value: 100_000_000_000_000})).toBe(`100${UNBREAKABLE_GAP}TB`);
});
it('should convert to size', () => {
expect(formatBytes({value: 100_000, size: 'b'})).toBe(
`100${unbreakableGap}000${unbreakableGap}B`,
`100${UNBREAKABLE_GAP}000${UNBREAKABLE_GAP}B`,
);
expect(formatBytes({value: 100_000_000_000_000, size: 'gb'})).toBe(
`100${unbreakableGap}000${unbreakableGap}GB`,
`100${UNBREAKABLE_GAP}000${UNBREAKABLE_GAP}GB`,
);
});
it('should convert without labels', () => {
expect(formatBytes({value: 100_000, size: 'b', withSizeLabel: false})).toBe(
`100${unbreakableGap}000`,
`100${UNBREAKABLE_GAP}000`,
);
expect(formatBytes({value: 100_000_000_000_000, size: 'gb', withSizeLabel: false})).toBe(
`100${unbreakableGap}000`,
`100${UNBREAKABLE_GAP}000`,
);
});
it('should convert to speed', () => {
expect(formatBytes({value: 100_000, withSpeedLabel: true})).toBe(
`100${unbreakableGap}KB/s`,
`100${UNBREAKABLE_GAP}KB/s`,
);
expect(formatBytes({value: 100_000, size: 'b', withSpeedLabel: true})).toBe(
`100${unbreakableGap}000${unbreakableGap}B/s`,
`100${UNBREAKABLE_GAP}000${UNBREAKABLE_GAP}B/s`,
);
});
it('should return fixed amount of significant digits', () => {
expect(formatBytes({value: 99_000, significantDigits: 2})).toEqual(
`99${unbreakableGap}000${unbreakableGap}B`,
`99${UNBREAKABLE_GAP}000${UNBREAKABLE_GAP}B`,
);
expect(formatBytes({value: 100_000, significantDigits: 2})).toEqual(
`100${unbreakableGap}KB`,
`100${UNBREAKABLE_GAP}KB`,
);
expect(formatBytes({value: 99_000_000_000_000, significantDigits: 2})).toEqual(
`99${unbreakableGap}000${unbreakableGap}GB`,
`99${UNBREAKABLE_GAP}000${UNBREAKABLE_GAP}GB`,
);
expect(formatBytes({value: 100_000_000_000_000, significantDigits: 2})).toEqual(
`100${unbreakableGap}TB`,
`100${UNBREAKABLE_GAP}TB`,
);
});
it('should return empty string on invalid data', () => {
Expand All @@ -55,12 +55,12 @@ describe('formatBytes', () => {
expect(formatBytes({value: '123qwe'})).toEqual('');
});
it('should work with precision', () => {
expect(formatBytes({value: 123.123, precision: 2})).toBe(`123${unbreakableGap}B`);
expect(formatBytes({value: 12.123, precision: 2})).toBe(`12${unbreakableGap}B`);
expect(formatBytes({value: 1.123, precision: 2})).toBe(`1.1${unbreakableGap}B`);
expect(formatBytes({value: 0.123, precision: 2})).toBe(`0.12${unbreakableGap}B`);
expect(formatBytes({value: 0.012, precision: 2})).toBe(`0.01${unbreakableGap}B`);
expect(formatBytes({value: 0.001, precision: 2})).toBe(`0${unbreakableGap}B`);
expect(formatBytes({value: 0, precision: 2})).toBe(`0${unbreakableGap}B`);
expect(formatBytes({value: 123.123, precision: 2})).toBe(`123${UNBREAKABLE_GAP}B`);
expect(formatBytes({value: 12.123, precision: 2})).toBe(`12${UNBREAKABLE_GAP}B`);
expect(formatBytes({value: 1.123, precision: 2})).toBe(`1.1${UNBREAKABLE_GAP}B`);
expect(formatBytes({value: 0.123, precision: 2})).toBe(`0.12${UNBREAKABLE_GAP}B`);
expect(formatBytes({value: 0.012, precision: 2})).toBe(`0.01${UNBREAKABLE_GAP}B`);
expect(formatBytes({value: 0.001, precision: 2})).toBe(`0${UNBREAKABLE_GAP}B`);
expect(formatBytes({value: 0, precision: 2})).toBe(`0${UNBREAKABLE_GAP}B`);
});
});
4 changes: 2 additions & 2 deletions src/utils/bytesParsers/formatBytes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {GIGABYTE, KILOBYTE, MEGABYTE, TERABYTE} from '../constants';
import {formatNumber, roundToPrecision} from '../dataFormatters/dataFormatters';
import {isNumeric, unbreakableGap} from '../utils';
import {UNBREAKABLE_GAP, isNumeric} from '../utils';

import i18n from './i18n';

Expand Down Expand Up @@ -84,7 +84,7 @@ const formatToSize = ({value, size = 'mb', precision = 0}: FormatToSizeArgs) =>
return formatNumber(result);
};

const addSizeLabel = (result: string, size: BytesSizes, delimiter = unbreakableGap) => {
const addSizeLabel = (result: string, size: BytesSizes, delimiter = UNBREAKABLE_GAP) => {
return result + delimiter + sizes[size].label;
};

Expand Down
14 changes: 7 additions & 7 deletions src/utils/dataFormatters/__test__/formatStorageValues.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {unbreakableGap} from '../../utils';
import {UNBREAKABLE_GAP} from '../../utils';
import {formatStorageValues} from '../dataFormatters';

describe('formatStorageValues', () => {
Expand All @@ -9,27 +9,27 @@ describe('formatStorageValues', () => {

it('should format value correctly when total is undefined', () => {
const result = formatStorageValues(1024);
expect(result).toEqual([`1${unbreakableGap}KB`, '']);
expect(result).toEqual([`1${UNBREAKABLE_GAP}KB`, '']);
});

it('should format total correctly when value is undefined', () => {
const result = formatStorageValues(undefined, 2048);
expect(result).toEqual(['', `2${unbreakableGap}KB`]);
expect(result).toEqual(['', `2${UNBREAKABLE_GAP}KB`]);
});

it('should format both value and total correctly', () => {
const result = formatStorageValues(1024, 2048);
expect(result).toEqual(['1', `2${unbreakableGap}KB`]);
expect(result).toEqual(['1', `2${UNBREAKABLE_GAP}KB`]);
});

it('should handle small value compared to total and increase precision', () => {
const result = formatStorageValues(1, 1024);
expect(result).toEqual(['0.001', `1${unbreakableGap}KB`]);
expect(result).toEqual(['0.001', `1${UNBREAKABLE_GAP}KB`]);
});

it('should return ["0", formattedTotal] when value is 0', () => {
const result = formatStorageValues(0, 2048);
expect(result).toEqual(['0', `2${unbreakableGap}KB`]);
expect(result).toEqual(['0', `2${UNBREAKABLE_GAP}KB`]);
});

it('should use provided size and delimiter', () => {
Expand All @@ -39,6 +39,6 @@ describe('formatStorageValues', () => {

it('should handle non-numeric total gracefully', () => {
const result = formatStorageValues(2048, 'Not a number' as any);
expect(result).toEqual([`2${unbreakableGap}KB`, '']);
expect(result).toEqual([`2${UNBREAKABLE_GAP}KB`, '']);
});
});
4 changes: 2 additions & 2 deletions src/utils/numeral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import numeral from 'numeral';
import 'numeral/locales'; // Without this numeral will throw an error when using not 'en' locale

import {Lang, i18n} from './i18n';
import {unbreakableGap} from './utils';
import {UNBREAKABLE_GAP} from './utils';

// Set space delimiter for all locales possible in project
Object.values(Lang).forEach((value) => {
if (numeral.locales[value]) {
numeral.locales[value].delimiters.thousands = unbreakableGap;
numeral.locales[value].delimiters.thousands = UNBREAKABLE_GAP;
}
});

Expand Down
2 changes: 1 addition & 1 deletion src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,4 @@ export function toExponential(value: number, precision?: number) {
return Number(value).toExponential(precision);
}

export const unbreakableGap = '\xa0';
export const UNBREAKABLE_GAP = '\xa0';

0 comments on commit 7eb74ad

Please sign in to comment.