Skip to content

Commit 49aa769

Browse files
committed
feat(new tools): Data Storage Units Converter and Data Transfer Rate Converter
New Tool: Data Transfer Rate Converter New Tool: Data Storage Units Converter (with MB, MiB and Mb) Fix CorentinTh#539 CorentinTh#785 CorentinTh#1160 CorentinTh#848 Data Storage Units Converter inspired by CorentinTh#948 by @utf26
1 parent 87984e2 commit 49aa769

9 files changed

+522
-1
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { convertStorageAndRateUnitsDisplay, displayStorageAndRateUnits } from './data-storage-unit-converter.service';
3+
4+
describe('data-storage-unit-converter', () => {
5+
describe('convertStorageAndRateUnitsDisplay', () => {
6+
it('convert from same base units', () => {
7+
expect(convertStorageAndRateUnitsDisplay({ value: 1024 * 1024, fromUnit: 'B', toUnit: 'MiB' })).toBe('1');
8+
expect(convertStorageAndRateUnitsDisplay({ value: 1024, fromUnit: 'KiB', toUnit: 'MiB' })).toBe('1');
9+
expect(convertStorageAndRateUnitsDisplay({ value: 1, fromUnit: 'MiB', toUnit: 'KiB' })).toBe('1,024');
10+
expect(convertStorageAndRateUnitsDisplay({ value: 1000, fromUnit: 'MB', toUnit: 'GB' })).toBe('1');
11+
expect(convertStorageAndRateUnitsDisplay({ value: 1024, fromUnit: 'MB', toUnit: 'MB' })).toBe('1,024');
12+
expect(convertStorageAndRateUnitsDisplay({ value: 1, fromUnit: 'MB', toUnit: 'KB' })).toBe('1,000');
13+
expect(convertStorageAndRateUnitsDisplay({ value: 1024, fromUnit: 'MiB', toUnit: 'GiB' })).toBe('1');
14+
expect(convertStorageAndRateUnitsDisplay({ value: 1000, fromUnit: 'MB', toUnit: 'GB' })).toBe('1');
15+
expect(convertStorageAndRateUnitsDisplay({ value: 1000, fromUnit: 'Mb', toUnit: 'Gb' })).toBe('1');
16+
});
17+
18+
it('convert between base units', () => {
19+
expect(convertStorageAndRateUnitsDisplay({ value: 1, fromUnit: 'MB', toUnit: 'MiB' })).toBe('0.954');
20+
expect(convertStorageAndRateUnitsDisplay({ value: 1, fromUnit: 'MiB', toUnit: 'MB' })).toBe('1.049');
21+
expect(convertStorageAndRateUnitsDisplay({ value: 1000 * 1000, fromUnit: 'B', toUnit: 'MiB' })).toBe('0.954');
22+
expect(convertStorageAndRateUnitsDisplay({ value: 1024, fromUnit: 'KB', toUnit: 'MiB' })).toBe('0.977');
23+
expect(convertStorageAndRateUnitsDisplay({ value: 1000, fromUnit: 'MiB', toUnit: 'MB' })).toBe('1,048.576');
24+
expect(convertStorageAndRateUnitsDisplay({ value: 1, fromUnit: 'MB', toUnit: 'Mb' })).toBe('8');
25+
expect(convertStorageAndRateUnitsDisplay({ value: 1000, fromUnit: 'KB', toUnit: 'Kb' })).toBe('8,000');
26+
expect(convertStorageAndRateUnitsDisplay({ value: 1000, fromUnit: 'KiB', toUnit: 'Kb' })).toBe('8,192');
27+
expect(convertStorageAndRateUnitsDisplay({ value: 8, fromUnit: 'Mb', toUnit: 'MB' })).toBe('1');
28+
29+
expect(convertStorageAndRateUnitsDisplay({ value: 1, fromUnit: 'Mb', toUnit: 'KB' })).toBe('125');
30+
expect(convertStorageAndRateUnitsDisplay({ value: 125, fromUnit: 'KB', toUnit: 'Mb' })).toBe('1');
31+
32+
expect(convertStorageAndRateUnitsDisplay({ value: 1, fromUnit: 'MiB', toUnit: 'Kb' })).toBe('8,388.608');
33+
expect(convertStorageAndRateUnitsDisplay({ value: 8388.608, fromUnit: 'Kb', toUnit: 'MiB' })).toBe('1');
34+
});
35+
it('convert with unit display', () => {
36+
expect(convertStorageAndRateUnitsDisplay({ value: 1024 * 1024, fromUnit: 'B', toUnit: 'MiB', appendUnit: true })).toBe('1MiB');
37+
});
38+
39+
//
40+
});
41+
describe('displayStorageAndRateUnits', () => {
42+
it('convert to correct display value', () => {
43+
expect(displayStorageAndRateUnits({
44+
value: 1.234567, unit: 'MB', appendUnit: false,
45+
})).toBe('1.235');
46+
expect(displayStorageAndRateUnits({
47+
value: 1.234567, unit: 'MB', appendUnit: true,
48+
})).toBe('1.235MB');
49+
expect(displayStorageAndRateUnits({
50+
value: 1.234567, unit: 'MB', appendUnit: true, precision: 5,
51+
})).toBe('1.23457MB');
52+
});
53+
});
54+
});
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
export type BibytesUnits = 'iB' | 'KiB' | 'MiB' | 'GiB' | 'TiB' | 'PiB' | 'EiB' | 'ZiB' | 'YiB';
2+
export type BytesUnits = 'B' | 'KB' | 'MB' | 'GB' | 'TB' | 'PB' | 'EB' | 'ZB' | 'YB';
3+
export type BitsUnits = 'b' | 'Kb' | 'Mb' | 'Gb' | 'Tb' | 'Pb' | 'Eb' | 'Zb' | 'Yb';
4+
export type AllSupportedUnits = BibytesUnits | BytesUnits | BitsUnits;
5+
6+
export function displayStorageAndRateUnits(
7+
{ value, unit, precision = 3, appendUnit = false }:
8+
{ value: number; unit: AllSupportedUnits; precision?: number ; appendUnit?: boolean }): string {
9+
return value.toLocaleString(undefined, {
10+
maximumFractionDigits: precision,
11+
}) + (appendUnit ? unit : '');
12+
}
13+
14+
export function convertStorageAndRateUnitsDisplay(
15+
{ value, fromUnit, toUnit, precision = 3, appendUnit = false }:
16+
{ value: number; fromUnit: AllSupportedUnits; toUnit: AllSupportedUnits; precision?: number; appendUnit?: boolean }): string {
17+
return displayStorageAndRateUnits({
18+
precision,
19+
unit: toUnit,
20+
appendUnit,
21+
value: convertStorageAndRateUnits({
22+
value, fromUnit, toUnit,
23+
}),
24+
});
25+
}
26+
27+
export function convertStorageAndRateUnits(
28+
{ value, fromUnit, toUnit }:
29+
{ value: number; fromUnit: AllSupportedUnits; toUnit: AllSupportedUnits }): number {
30+
const units = [
31+
'iB', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB',
32+
'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB',
33+
'b', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb', 'Zb', 'Yb',
34+
];
35+
36+
const fromIndex = units.indexOf(fromUnit);
37+
const fromFactor = fromIndex / 9 > 1 ? 1000 : 1024;
38+
const fromDivisor = fromIndex / 9 > 2 ? 8 : 1;
39+
const toIndex = units.indexOf(toUnit);
40+
const toFactor = toIndex / 9 > 1 ? 1000 : 1024;
41+
const toDivisor = toIndex / 9 > 2 ? 8 : 1;
42+
43+
const fromBase = (fromFactor ** (fromIndex % 9)) / fromDivisor;
44+
const toBase = (toFactor ** (toIndex % 9)) / toDivisor;
45+
46+
return value * fromBase / toBase;
47+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<script setup lang="ts">
2+
import InputCopyable from '../../components/InputCopyable.vue';
3+
import { type AllSupportedUnits, convertStorageAndRateUnitsDisplay } from './data-storage-unit-converter.service';
4+
5+
const input = ref<{ size: string; unit: string }>({ size: '0', unit: 'KB' });
6+
const output = ref<{ unit: string; precision: number; appendUnit: boolean }>({ unit: 'MB', precision: 3, appendUnit: false });
7+
8+
const allUnits = [
9+
'iB', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB',
10+
'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB',
11+
'b', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb', 'Zb', 'Yb'];
12+
13+
const convertedValue = computed(() => {
14+
try {
15+
return convertStorageAndRateUnitsDisplay({
16+
value: Number(input.value.size),
17+
fromUnit: input.value.unit as AllSupportedUnits,
18+
toUnit: output.value.unit as AllSupportedUnits,
19+
precision: output.value.precision,
20+
appendUnit: output.value.appendUnit,
21+
});
22+
}
23+
catch (e: any) {
24+
return e.toString();
25+
}
26+
});
27+
</script>
28+
29+
<template>
30+
<div>
31+
<c-card>
32+
<n-form-item label="Input Size:" label-placement="left" mb-1>
33+
<c-input-text
34+
v-model:value="input.size"
35+
placeholder="Put your number here (ex: 1024)"
36+
mr-2
37+
/>
38+
<c-select
39+
v-model:value="input.unit"
40+
:options="allUnits"
41+
placeholder="Select input unit"
42+
/>
43+
</n-form-item>
44+
45+
<div flex items-baseline gap-2>
46+
<c-select
47+
v-model:value="output.unit"
48+
label="Output:" label-position="left"
49+
:options="allUnits"
50+
placeholder="Select output unit"
51+
/>
52+
53+
<n-form-item label="Precision:" label-placement="left">
54+
<n-input-number v-model:value="output.precision" placeholder="Precision..." :max="10" :min="0" />
55+
</n-form-item>
56+
57+
<n-checkbox v-model:checked="output.appendUnit">
58+
Show unit?
59+
</n-checkbox>
60+
</div>
61+
62+
<n-divider />
63+
64+
<InputCopyable
65+
label="Output value"
66+
:value="convertedValue"
67+
placeholder="Output value will be here..."
68+
/>
69+
</c-card>
70+
</div>
71+
</template>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { ArrowsLeftRight } from '@vicons/tabler';
2+
import { defineTool } from '../tool';
3+
4+
export const tool = defineTool({
5+
name: 'Data Storage Unit converter',
6+
path: '/data-storage-unit-converter',
7+
description: 'Convert data storage or transfer units (bytes, bibytes, bits, kilobytes...)',
8+
keywords: ['data', 'storage', 'unit', 'conversion',
9+
'bits', 'bytes', 'bibytes', 'binary',
10+
'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB',
11+
'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB',
12+
'b', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb', 'Zb', 'Yb'],
13+
component: () => import('./data-storage-unit-converter.vue'),
14+
icon: ArrowsLeftRight,
15+
createdAt: new Date('2024-08-15'),
16+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { amountTransferable, neededRate, transferTimeSeconds } from './data-transfer-rate-converter.service';
3+
4+
describe('data-transfer-converter', () => {
5+
describe('transferTimeSeconds', () => {
6+
it('compute transfer time in seconds', () => {
7+
expect(transferTimeSeconds({
8+
dataSize: 100,
9+
dataSizeUnit: 'MB',
10+
bitRate: 10,
11+
bitRateUnit: 'Mb',
12+
})).toBe(80);
13+
});
14+
});
15+
describe('neededRate', () => {
16+
it('compute neededRate', () => {
17+
expect(neededRate({
18+
dataSize: 100,
19+
dataSizeUnit: 'MB',
20+
hours: 0,
21+
minutes: 1,
22+
seconds: 20,
23+
bitRateUnit: 'Mb',
24+
})).toBe(10);
25+
});
26+
});
27+
describe('amountTransferable', () => {
28+
it('compute amount transfered', () => {
29+
expect(amountTransferable({
30+
bitRate: 10,
31+
bitRateUnit: 'Mb',
32+
hours: 1,
33+
minutes: 0,
34+
seconds: 0,
35+
dataSizeUnit: 'MB',
36+
})).toBe(4500);
37+
});
38+
});
39+
});
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { type AllSupportedUnits, type BitsUnits, convertStorageAndRateUnits } from '../data-storage-unit-converter/data-storage-unit-converter.service';
2+
3+
export function transferTimeSeconds({
4+
dataSize,
5+
dataSizeUnit,
6+
bitRate,
7+
bitRateUnit,
8+
}: {
9+
dataSize: number
10+
dataSizeUnit: AllSupportedUnits
11+
bitRate: number
12+
bitRateUnit: BitsUnits
13+
}): number {
14+
const dataSizeInBytes = convertStorageAndRateUnits({ value: dataSize, fromUnit: dataSizeUnit, toUnit: 'B' });
15+
const bitRateInBytes = convertStorageAndRateUnits({ value: bitRate, fromUnit: bitRateUnit, toUnit: 'B' });
16+
return bitRateInBytes > 0 ? dataSizeInBytes / bitRateInBytes : 0;
17+
}
18+
19+
export function neededRate({
20+
dataSize,
21+
dataSizeUnit,
22+
hours,
23+
minutes,
24+
seconds,
25+
bitRateUnit,
26+
}: {
27+
dataSize: number
28+
dataSizeUnit: AllSupportedUnits
29+
hours: number
30+
minutes: number
31+
seconds: number
32+
bitRateUnit: BitsUnits
33+
}): number {
34+
const dataSizeInBits = convertStorageAndRateUnits({ value: dataSize, fromUnit: dataSizeUnit, toUnit: 'b' });
35+
const timeInSeconds = hours * 3600 + minutes * 60 + seconds;
36+
return convertStorageAndRateUnits({ value: timeInSeconds > 0 ? dataSizeInBits / timeInSeconds : 0, fromUnit: 'b', toUnit: bitRateUnit });
37+
}
38+
39+
export function amountTransferable({
40+
bitRate,
41+
bitRateUnit,
42+
hours,
43+
minutes,
44+
seconds,
45+
dataSizeUnit,
46+
}: {
47+
bitRate: number
48+
bitRateUnit: BitsUnits
49+
hours: number
50+
minutes: number
51+
seconds: number
52+
dataSizeUnit: AllSupportedUnits
53+
}): number {
54+
const bitRateInBytes = convertStorageAndRateUnits({ value: bitRate, fromUnit: bitRateUnit, toUnit: 'B' });
55+
const timeInSeconds = hours * 3600 + minutes * 60 + seconds;
56+
return convertStorageAndRateUnits({ value: bitRateInBytes * timeInSeconds, fromUnit: 'B', toUnit: dataSizeUnit });
57+
}

0 commit comments

Comments
 (0)