Skip to content

Commit 35ae97d

Browse files
committed
feat(new tools): Data Storage/Transfer Units Converter
Fix CorentinTh#539 CorentinTh#785 CorentinTh#1160 CorentinTh#848
1 parent 87984e2 commit 35ae97d

File tree

5 files changed

+148
-0
lines changed

5 files changed

+148
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { convertStorageAndRateUnits } from './data-storage-unit-converter.service';
3+
4+
describe('data-storage-unit-converter', () => {
5+
describe('convertStorageAndRateUnits', () => {
6+
it('convert from same base units', () => {
7+
expect(convertStorageAndRateUnits({ value: 1024 * 1024, fromUnit: 'B', toUnit: 'MiB' })).toBe('1');
8+
expect(convertStorageAndRateUnits({ value: 1024, fromUnit: 'KiB', toUnit: 'MiB' })).toBe('1');
9+
expect(convertStorageAndRateUnits({ value: 1, fromUnit: 'MiB', toUnit: 'KiB' })).toBe('1,024');
10+
expect(convertStorageAndRateUnits({ value: 1000, fromUnit: 'MB', toUnit: 'GB' })).toBe('1');
11+
expect(convertStorageAndRateUnits({ value: 1024, fromUnit: 'MB', toUnit: 'MB' })).toBe('1,024');
12+
expect(convertStorageAndRateUnits({ value: 1, fromUnit: 'MB', toUnit: 'KB' })).toBe('1,000');
13+
expect(convertStorageAndRateUnits({ value: 1024, fromUnit: 'MiB', toUnit: 'GiB' })).toBe('1');
14+
expect(convertStorageAndRateUnits({ value: 1000, fromUnit: 'MB', toUnit: 'GB' })).toBe('1');
15+
expect(convertStorageAndRateUnits({ value: 1000, fromUnit: 'Mb', toUnit: 'Gb' })).toBe('1');
16+
});
17+
18+
it('convert between base units', () => {
19+
expect(convertStorageAndRateUnits({ value: 1, fromUnit: 'MB', toUnit: 'MiB' })).toBe('0.954');
20+
expect(convertStorageAndRateUnits({ value: 1, fromUnit: 'MiB', toUnit: 'MB' })).toBe('1.049');
21+
expect(convertStorageAndRateUnits({ value: 1000 * 1000, fromUnit: 'B', toUnit: 'MiB' })).toBe('0.954');
22+
expect(convertStorageAndRateUnits({ value: 1024, fromUnit: 'KB', toUnit: 'MiB' })).toBe('0.977');
23+
expect(convertStorageAndRateUnits({ value: 1000, fromUnit: 'MiB', toUnit: 'MB' })).toBe('1,048.576');
24+
expect(convertStorageAndRateUnits({ value: 1, fromUnit: 'MB', toUnit: 'Mb' })).toBe('8');
25+
expect(convertStorageAndRateUnits({ value: 1000, fromUnit: 'KB', toUnit: 'Kb' })).toBe('8,000');
26+
expect(convertStorageAndRateUnits({ value: 1000, fromUnit: 'KiB', toUnit: 'Kb' })).toBe('8,192');
27+
expect(convertStorageAndRateUnits({ value: 8, fromUnit: 'Mb', toUnit: 'MB' })).toBe('1');
28+
29+
expect(convertStorageAndRateUnits({ value: 1, fromUnit: 'Mb', toUnit: 'KB' })).toBe('125');
30+
expect(convertStorageAndRateUnits({ value: 125, fromUnit: 'KB', toUnit: 'Mb' })).toBe('1');
31+
32+
expect(convertStorageAndRateUnits({ value: 1, fromUnit: 'MiB', toUnit: 'Kb' })).toBe('8,388.608');
33+
expect(convertStorageAndRateUnits({ value: 8388.608, fromUnit: 'Kb', toUnit: 'MiB' })).toBe('1');
34+
});
35+
});
36+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export function convertStorageAndRateUnits(
2+
{ value, fromUnit, toUnit, precision = 3 }:
3+
{ value: number; fromUnit: string; toUnit: string; precision?: number }): string {
4+
const units = [
5+
'iB', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB',
6+
'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB',
7+
'b', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb', 'Zb', 'Yb',
8+
];
9+
10+
const fromIndex = units.indexOf(fromUnit);
11+
const fromFactor = fromIndex / 9 > 1 ? 1000 : 1024;
12+
const fromDivisor = fromIndex / 9 > 2 ? 8 : 1;
13+
const toIndex = units.indexOf(toUnit);
14+
const toFactor = toIndex / 9 > 1 ? 1000 : 1024;
15+
const toDivisor = toIndex / 9 > 2 ? 8 : 1;
16+
17+
const fromBase = (fromFactor ** (fromIndex % 9)) / fromDivisor;
18+
const toBase = (toFactor ** (toIndex % 9)) / toDivisor;
19+
20+
const result = value * fromBase / toBase;
21+
return result.toLocaleString(undefined, {
22+
maximumFractionDigits: precision,
23+
});
24+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<script setup lang="ts">
2+
import InputCopyable from '../../components/InputCopyable.vue';
3+
import { convertBetweenUnits } from './data-storage-unit-converter.service';
4+
5+
const input = ref('1024');
6+
const inputUnit = ref('KB');
7+
const outputUnit = ref('MB');
8+
const precision = ref(3);
9+
10+
const allUnits = ['iB', '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+
14+
const output = computed(() => {
15+
try {
16+
return convertBetweenUnits({
17+
value: Number(input.value),
18+
fromUnit: inputUnit.value,
19+
toUnit: outputUnit.value,
20+
precision: precision.value,
21+
});
22+
}
23+
catch (e: any) {
24+
return e.toString();
25+
}
26+
});
27+
</script>
28+
29+
<template>
30+
<div>
31+
<c-card>
32+
<c-input-text
33+
v-model:value="input"
34+
label="Input number"
35+
placeholder="Put your number here (ex: 1024)" label-position="left" label-width="110px" label-align="right"
36+
mb-2
37+
/>
38+
39+
<c-select
40+
searchable
41+
label="Input unit:"
42+
:value="inputUnit"
43+
:options="allUnits"
44+
placeholder="Select input unit"
45+
mb-2
46+
/>
47+
48+
<c-select
49+
searchable
50+
label="Output unit:"
51+
:value="outputUnit"
52+
:options="allUnits"
53+
placeholder="Select output unit"
54+
mb-2
55+
/>
56+
57+
<n-form-item label="Precision: " label-placement="left" label-width="120" mb-2>
58+
<n-input-number v-model:value="precision" placeholder="Precision..." :max="10" :min="0" />
59+
</n-form-item>
60+
61+
<n-divider />
62+
63+
<InputCopyable
64+
label="Output value"
65+
:value="output"
66+
placeholder="Output value will be here..."
67+
/>
68+
</c-card>
69+
</div>
70+
</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+
});

src/tools/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { tool as base64FileConverter } from './base64-file-converter';
22
import { tool as base64StringConverter } from './base64-string-converter';
33
import { tool as basicAuthGenerator } from './basic-auth-generator';
44
import { tool as emailNormalizer } from './email-normalizer';
5+
import { tool as dataTransferRate } from './data-transfer-rate';
6+
import { tool as dataStorageUnitConverter } from './data-storage-unit-converter';
57

68
import { tool as asciiTextDrawer } from './ascii-text-drawer';
79

0 commit comments

Comments
 (0)