Skip to content

Commit 8907ff0

Browse files
committed
Merge branch 'feat/storage-transfer-units' into chore/all-my-stuffs
# Conflicts: # components.d.ts # src/tools/index.ts
2 parents c4e8b15 + 7856df3 commit 8907ff0

10 files changed

+567
-1
lines changed

pnpm-lock.yaml

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
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('1024');
10+
expect(convertStorageAndRateUnitsDisplay({ value: 1000, fromUnit: 'MB', toUnit: 'GB' })).toBe('1');
11+
expect(convertStorageAndRateUnitsDisplay({ value: 1024, fromUnit: 'MB', toUnit: 'MB' })).toBe('1024');
12+
expect(convertStorageAndRateUnitsDisplay({ value: 1, fromUnit: 'MB', toUnit: 'KB' })).toBe('1000');
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('1048.576');
24+
expect(convertStorageAndRateUnitsDisplay({ value: 1, fromUnit: 'MB', toUnit: 'Mb' })).toBe('8');
25+
expect(convertStorageAndRateUnitsDisplay({ value: 1000, fromUnit: 'KB', toUnit: 'Kb' })).toBe('8000');
26+
expect(convertStorageAndRateUnitsDisplay({ value: 1000, fromUnit: 'KiB', toUnit: 'Kb' })).toBe('8192');
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('8388.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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.toFixed(precision).replace(/0+$/, '').replace(/\.$/, '') + (appendUnit ? unit : ''); // NOSONAR
10+
}
11+
12+
export function convertStorageAndRateUnitsDisplay(
13+
{ value, fromUnit, toUnit, precision = 3, appendUnit = false }:
14+
{ value: number; fromUnit: AllSupportedUnits; toUnit: AllSupportedUnits; precision?: number; appendUnit?: boolean }): string {
15+
return displayStorageAndRateUnits({
16+
precision,
17+
unit: toUnit,
18+
appendUnit,
19+
value: convertStorageAndRateUnits({
20+
value, fromUnit, toUnit,
21+
}),
22+
});
23+
}
24+
25+
export function convertStorageAndRateUnits(
26+
{ value, fromUnit, toUnit }:
27+
{ value: number; fromUnit: AllSupportedUnits; toUnit: AllSupportedUnits }): number {
28+
const units = [
29+
'iB', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB',
30+
'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB',
31+
'b', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb', 'Zb', 'Yb',
32+
];
33+
34+
const fromIndex = units.indexOf(fromUnit);
35+
const fromFactor = fromIndex / 9 > 1 ? 1000 : 1024;
36+
const fromDivisor = fromIndex / 9 > 2 ? 8 : 1;
37+
const toIndex = units.indexOf(toUnit);
38+
const toFactor = toIndex / 9 > 1 ? 1000 : 1024;
39+
const toDivisor = toIndex / 9 > 2 ? 8 : 1;
40+
41+
const fromBase = (fromFactor ** (fromIndex % 9)) / fromDivisor;
42+
const toBase = (toFactor ** (toIndex % 9)) / toDivisor;
43+
44+
return value * fromBase / toBase;
45+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
{ value: 'B', label: 'Bytes (B)' },
10+
{ value: 'b', label: 'Bits (bit)' },
11+
{ value: 'iB', label: 'Bibytes (iB)' },
12+
{ value: 'KB', label: 'Kilobytes (KB)' },
13+
{ value: 'Kb', label: 'Kilobits (Kbit)' },
14+
{ value: 'KiB', label: 'Kibibytes (KiB)' },
15+
{ value: 'MB', label: 'Megabytes (MB)' },
16+
{ value: 'Mb', label: 'Megabits (Mbit)' },
17+
{ value: 'MiB', label: 'Mebibytes (MiB)' },
18+
{ value: 'GB', label: 'Gigabytes (GB)' },
19+
{ value: 'Gb', label: 'Gigabits (Gbit)' },
20+
{ value: 'GiB', label: 'Gibibytes (GiB)' },
21+
{ value: 'TB', label: 'Terabytes (TB)' },
22+
{ value: 'Tb', label: 'Terabits (Tbit)' },
23+
{ value: 'TiB', label: 'Tebibytes (TiB)' },
24+
{ value: 'PB', label: 'Petabytes (PB)' },
25+
{ value: 'Pb', label: 'Petabits (Pbit)' },
26+
{ value: 'PiB', label: 'Pebibytes (PiB)' },
27+
{ value: 'EB', label: 'Exabytes (EB)' },
28+
{ value: 'Eb', label: 'Exabits (Ebit)' },
29+
{ value: 'EiB', label: 'Exbibytes (EiB)' },
30+
{ value: 'ZB', label: 'Zettabytes (ZB)' },
31+
{ value: 'Zb', label: 'Zettabits (Zbit)' },
32+
{ value: 'ZiB', label: 'Zebibytes (ZiB)' },
33+
{ value: 'YB', label: 'Yottabytes (YB)' },
34+
{ value: 'Yb', label: 'Yottabits (Ybit)' },
35+
{ value: 'YiB', label: 'Yobibytes (YiB)' },
36+
];
37+
38+
const convertedValue = computed(() => {
39+
try {
40+
return convertStorageAndRateUnitsDisplay({
41+
value: Number(input.value.size),
42+
fromUnit: input.value.unit as AllSupportedUnits,
43+
toUnit: output.value.unit as AllSupportedUnits,
44+
precision: output.value.precision,
45+
appendUnit: output.value.appendUnit,
46+
});
47+
}
48+
catch (e: any) {
49+
return e.toString();
50+
}
51+
});
52+
</script>
53+
54+
<template>
55+
<div>
56+
<c-card>
57+
<n-form-item label="Input Size:" label-placement="left" mb-1>
58+
<c-input-text
59+
v-model:value="input.size"
60+
placeholder="Put your number here (ex: 1024)"
61+
mr-2
62+
/>
63+
<c-select
64+
v-model:value="input.unit"
65+
:options="allUnits"
66+
placeholder="Select input unit"
67+
/>
68+
</n-form-item>
69+
70+
<div flex items-baseline gap-2>
71+
<c-select
72+
v-model:value="output.unit"
73+
label="Output:" label-position="left"
74+
:options="allUnits"
75+
placeholder="Select output unit"
76+
/>
77+
78+
<n-form-item label="Precision:" label-placement="left">
79+
<n-input-number v-model:value="output.precision" style="width:100px" placeholder="Precision..." :max="10" :min="0" />
80+
</n-form-item>
81+
82+
<n-checkbox v-model:checked="output.appendUnit">
83+
Show unit?
84+
</n-checkbox>
85+
</div>
86+
87+
<n-divider />
88+
89+
<InputCopyable
90+
label="Output value"
91+
:value="convertedValue"
92+
placeholder="Output value will be here..."
93+
/>
94+
</c-card>
95+
</div>
96+
</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)