✅ Add tests for some tools functions (#1377)
* ✅ Add tests for some tools functions * 🐛 Build issue with language definition
This commit is contained in:
73
src/tools/_tests/bytesHelper.spec.ts
Normal file
73
src/tools/_tests/bytesHelper.spec.ts
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
import { describe, expect, it } from 'vitest';
|
||||||
|
|
||||||
|
import { bytes } from '../bytesHelper';
|
||||||
|
|
||||||
|
describe('bytes.toPerSecondString', () => {
|
||||||
|
it('should format 999 bytes as 999 b/s', () => {
|
||||||
|
// Arrange
|
||||||
|
const byteCount = 999;
|
||||||
|
// Act
|
||||||
|
const result = bytes.toPerSecondString(byteCount);
|
||||||
|
// Assert
|
||||||
|
expect(result).toBe('999.0 b/s');
|
||||||
|
});
|
||||||
|
it.each([
|
||||||
|
[1, 'b/s', 'Kb/s'],
|
||||||
|
[2, 'Kb/s', 'Mb/s'],
|
||||||
|
[3, 'Mb/s', 'Gb/s'],
|
||||||
|
])('should format 1000^%s bytes or 1000 %s as 1.0 %s', (power, _unit, nextUnit) => {
|
||||||
|
// Arrange
|
||||||
|
const byteCount = Math.pow(1000, power);
|
||||||
|
// Act
|
||||||
|
const result = bytes.toPerSecondString(byteCount);
|
||||||
|
// Assert
|
||||||
|
expect(result).toBe(`1.0 ${nextUnit}`);
|
||||||
|
});
|
||||||
|
it('should format 1000^4 bytes or 1 Tb/s with as 1000.0 Gb/s', () => {
|
||||||
|
// Arrange
|
||||||
|
const byteCount = Math.pow(1000, 4);
|
||||||
|
// Act
|
||||||
|
const result = bytes.toPerSecondString(byteCount);
|
||||||
|
// Assert
|
||||||
|
expect(result).toBe('1000.0 Gb/s');
|
||||||
|
});
|
||||||
|
it('should format undefined as -', () => {
|
||||||
|
// Arrange
|
||||||
|
const byteCount = undefined;
|
||||||
|
// Act
|
||||||
|
const result = bytes.toPerSecondString(byteCount);
|
||||||
|
// Assert
|
||||||
|
expect(result).toBe('-');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('bytes.toString', () => {
|
||||||
|
it('should format 999 bytes as 999 B', () => {
|
||||||
|
// Arrange
|
||||||
|
const byteCount = 999;
|
||||||
|
// Act
|
||||||
|
const result = bytes.toString(byteCount);
|
||||||
|
// Assert
|
||||||
|
expect(result).toBe('999.0 B');
|
||||||
|
});
|
||||||
|
it.each([
|
||||||
|
[1, 'B', 'KiB'],
|
||||||
|
[2, 'KiB', 'MiB'],
|
||||||
|
[3, 'MiB', 'GiB'],
|
||||||
|
])('should format 1024^%s bytes or 1024 %s as 1.0 %s', (power, _unit, nextUnit) => {
|
||||||
|
// Arrange
|
||||||
|
const byteCount = Math.pow(1024, power);
|
||||||
|
// Act
|
||||||
|
const result = bytes.toString(byteCount);
|
||||||
|
// Assert
|
||||||
|
expect(result).toBe(`1.0 ${nextUnit}`);
|
||||||
|
});
|
||||||
|
it('should format 1024^4 bytes or 1 TiB with as 1024.0 GiB', () => {
|
||||||
|
// Arrange
|
||||||
|
const byteCount = Math.pow(1024, 4);
|
||||||
|
// Act
|
||||||
|
const result = bytes.toString(byteCount);
|
||||||
|
// Assert
|
||||||
|
expect(result).toBe('1024.0 GiB');
|
||||||
|
});
|
||||||
|
});
|
||||||
110
src/tools/_tests/humanFileSize.spec.ts
Normal file
110
src/tools/_tests/humanFileSize.spec.ts
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
import { describe, expect, it, vi } from 'vitest';
|
||||||
|
|
||||||
|
import { humanFileSize } from '../humanFileSize';
|
||||||
|
|
||||||
|
describe('humanFileSize', () => {
|
||||||
|
it('should format 1023 bytes as 1023 B', () => {
|
||||||
|
// Arrange
|
||||||
|
const bytes = 1023;
|
||||||
|
|
||||||
|
// Act
|
||||||
|
const result = humanFileSize(bytes, false);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
expect(result).toBe('1023 B');
|
||||||
|
});
|
||||||
|
it.each([
|
||||||
|
[1, 'B', 'KiB'],
|
||||||
|
[2, 'KiB', 'MiB'],
|
||||||
|
[3, 'MiB', 'GiB'],
|
||||||
|
[4, 'GiB', 'TiB'],
|
||||||
|
[5, 'TiB', 'PiB'],
|
||||||
|
[6, 'PiB', 'EiB'],
|
||||||
|
[7, 'EiB', 'ZiB'],
|
||||||
|
[8, 'ZiB', 'YiB'],
|
||||||
|
])(
|
||||||
|
'should format 1024^%s B or 1024 %s with 1024 threshhold as 1.0 %s',
|
||||||
|
(power, _unit, nextUnit) => {
|
||||||
|
// Arrange
|
||||||
|
const bytes = Math.pow(1024, power);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
const result = humanFileSize(bytes, false);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
expect(result).toBe(`1.0 ${nextUnit}`);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
it('should format 1024^9 B or 1024 YiB with 1024 threshhold as 1024.0 YiB', () => {
|
||||||
|
// Arrange
|
||||||
|
const bytes = Math.pow(1024, 9);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
const result = humanFileSize(bytes, false);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
expect(result).toBe('1024.0 YiB');
|
||||||
|
});
|
||||||
|
it('should format 999 bytes as 999 B', () => {
|
||||||
|
// Arrange
|
||||||
|
const bytes = 999;
|
||||||
|
|
||||||
|
// Act
|
||||||
|
const result = humanFileSize(bytes);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
expect(result).toBe('999 B');
|
||||||
|
});
|
||||||
|
it.each([
|
||||||
|
[1, 'B', 'KB'],
|
||||||
|
[2, 'KB', 'MB'],
|
||||||
|
[3, 'MB', 'GB'],
|
||||||
|
[4, 'GB', 'TB'],
|
||||||
|
[5, 'TB', 'PB'],
|
||||||
|
[6, 'PB', 'EB'],
|
||||||
|
[7, 'EB', 'ZB'],
|
||||||
|
[8, 'ZB', 'YB'],
|
||||||
|
])(
|
||||||
|
'should format 1000^%s B or 1000 %s with 1000 threshhold as 1.0 %s',
|
||||||
|
(power, _unit, nextUnit) => {
|
||||||
|
// Arrange
|
||||||
|
const bytes = Math.pow(1000, power);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
const result = humanFileSize(bytes);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
expect(result).toBe(`1.0 ${nextUnit}`);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
it('should format 1000^9 B or 1000 YB with 1000 threshhold as 1000.0 YB', () => {
|
||||||
|
// Arrange
|
||||||
|
const bytes = Math.pow(1000, 9);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
const result = humanFileSize(bytes);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
expect(result).toBe('1000.0 YB');
|
||||||
|
});
|
||||||
|
it('should format 1000 B with 1000 threshhold and 0 decimal places as 1 KB', () => {
|
||||||
|
// Arrange
|
||||||
|
const bytes = 1000;
|
||||||
|
|
||||||
|
// Act
|
||||||
|
const result = humanFileSize(bytes, true, 0);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
expect(result).toBe('1 KB');
|
||||||
|
});
|
||||||
|
it('should format 1000 B with 1000 threshhold and 4 decimal places as 1.0000 KB', () => {
|
||||||
|
// Arrange
|
||||||
|
const bytes = 1000;
|
||||||
|
|
||||||
|
// Act
|
||||||
|
const result = humanFileSize(bytes, true, 4);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
expect(result).toBe('1.0000 KB');
|
||||||
|
});
|
||||||
|
});
|
||||||
22
src/tools/_tests/language.spec.ts
Normal file
22
src/tools/_tests/language.spec.ts
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import { describe, expect, it } from 'vitest';
|
||||||
|
|
||||||
|
import { getLanguageByCode } from '../language';
|
||||||
|
|
||||||
|
describe('getLanguageByCode', () => {
|
||||||
|
it('should return Vietnamese for code vi', () => {
|
||||||
|
// Arrange
|
||||||
|
const code = 'vi';
|
||||||
|
// Act
|
||||||
|
const result = getLanguageByCode(code);
|
||||||
|
// Assert
|
||||||
|
expect(result.translatedName).toBe('Vietnamese');
|
||||||
|
});
|
||||||
|
it('should return English as fallback for code null', () => {
|
||||||
|
// Arrange
|
||||||
|
const code = null;
|
||||||
|
// Act
|
||||||
|
const result = getLanguageByCode(code);
|
||||||
|
// Assert
|
||||||
|
expect(result.translatedName).toBe('English');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -2,30 +2,30 @@
|
|||||||
* Format bytes as human-readable text.
|
* Format bytes as human-readable text.
|
||||||
*
|
*
|
||||||
* @param bytes Number of bytes.
|
* @param bytes Number of bytes.
|
||||||
* @param si True to use metric (SI) units, aka powers of 1000. False to use
|
* @param use1024Threshhold True to use metric (SI) units, aka powers of 1000. False to use
|
||||||
* binary (IEC), aka powers of 1024.
|
* binary (IEC), aka powers of 1024.
|
||||||
* @param dp Number of decimal places to display.
|
* @param decimalPlaces Number of decimal places to display.
|
||||||
*
|
*
|
||||||
* @return Formatted string.
|
* @return Formatted string.
|
||||||
*/
|
*/
|
||||||
export function humanFileSize(initialBytes: number, si = true, dp = 1) {
|
export function humanFileSize(initialBytes: number, use1024Threshhold = true, decimalPlaces = 1) {
|
||||||
const thresh = si ? 1000 : 1024;
|
const thresh = use1024Threshhold ? 1000 : 1024;
|
||||||
let bytes = initialBytes;
|
let bytes = initialBytes;
|
||||||
|
|
||||||
if (Math.abs(bytes) < thresh) {
|
if (Math.abs(bytes) < thresh) {
|
||||||
return `${bytes} B`;
|
return `${bytes} B`;
|
||||||
}
|
}
|
||||||
|
|
||||||
const units = si
|
const units = use1024Threshhold
|
||||||
? ['kb', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
|
? ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
|
||||||
: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
|
: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
|
||||||
let u = -1;
|
let u = -1;
|
||||||
const r = 10 ** dp;
|
const r = 10 ** decimalPlaces;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
bytes /= thresh;
|
bytes /= thresh;
|
||||||
u += 1;
|
u += 1;
|
||||||
} while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1);
|
} while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1);
|
||||||
|
|
||||||
return `${bytes.toFixed(dp)} ${units[u]}`;
|
return `${bytes.toFixed(decimalPlaces)} ${units[u]}`;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ export type Language = {
|
|||||||
locale: string;
|
locale: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const languages: Language[] = [
|
export const languages = [
|
||||||
{
|
{
|
||||||
shortName: 'de',
|
shortName: 'de',
|
||||||
originalName: 'Deutsch',
|
originalName: 'Deutsch',
|
||||||
@@ -93,6 +93,7 @@ export const languages: Language[] = [
|
|||||||
originalName: 'LOLCAT',
|
originalName: 'LOLCAT',
|
||||||
translatedName: 'LOLCAT',
|
translatedName: 'LOLCAT',
|
||||||
emoji: '🐱',
|
emoji: '🐱',
|
||||||
|
country: 'LOL',
|
||||||
locale: 'en-gb',
|
locale: 'en-gb',
|
||||||
},
|
},
|
||||||
// Norwegian
|
// Norwegian
|
||||||
@@ -224,9 +225,11 @@ export const languages: Language[] = [
|
|||||||
originalName: 'Magyar',
|
originalName: 'Magyar',
|
||||||
translatedName: 'Hungarian',
|
translatedName: 'Hungarian',
|
||||||
emoji: '🇭🇺',
|
emoji: '🇭🇺',
|
||||||
|
country: 'HU',
|
||||||
locale: 'hu',
|
locale: 'hu',
|
||||||
},
|
},
|
||||||
];
|
] as const satisfies Readonly<Language[]>;
|
||||||
|
|
||||||
export const getLanguageByCode = (code: string | null) =>
|
export const getLanguageByCode = (code: string | null) =>
|
||||||
languages.find((language) => language.shortName === code) ?? languages[languages.length - 1];
|
languages.find((language) => language.shortName === code) ??
|
||||||
|
languages.find((x) => x.locale === 'en-gb')!;
|
||||||
|
|||||||
Reference in New Issue
Block a user