mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-05-04 13:29:13 -04:00
feat(new tools): Data Storage/Transfer Units Converter
Fix #539 #785 #1160 #848
This commit is contained in:
parent
87984e2081
commit
35ae97d54a
5 changed files with 148 additions and 0 deletions
|
@ -0,0 +1,36 @@
|
||||||
|
import { describe, expect, it } from 'vitest';
|
||||||
|
import { convertStorageAndRateUnits } from './data-storage-unit-converter.service';
|
||||||
|
|
||||||
|
describe('data-storage-unit-converter', () => {
|
||||||
|
describe('convertStorageAndRateUnits', () => {
|
||||||
|
it('convert from same base units', () => {
|
||||||
|
expect(convertStorageAndRateUnits({ value: 1024 * 1024, fromUnit: 'B', toUnit: 'MiB' })).toBe('1');
|
||||||
|
expect(convertStorageAndRateUnits({ value: 1024, fromUnit: 'KiB', toUnit: 'MiB' })).toBe('1');
|
||||||
|
expect(convertStorageAndRateUnits({ value: 1, fromUnit: 'MiB', toUnit: 'KiB' })).toBe('1,024');
|
||||||
|
expect(convertStorageAndRateUnits({ value: 1000, fromUnit: 'MB', toUnit: 'GB' })).toBe('1');
|
||||||
|
expect(convertStorageAndRateUnits({ value: 1024, fromUnit: 'MB', toUnit: 'MB' })).toBe('1,024');
|
||||||
|
expect(convertStorageAndRateUnits({ value: 1, fromUnit: 'MB', toUnit: 'KB' })).toBe('1,000');
|
||||||
|
expect(convertStorageAndRateUnits({ value: 1024, fromUnit: 'MiB', toUnit: 'GiB' })).toBe('1');
|
||||||
|
expect(convertStorageAndRateUnits({ value: 1000, fromUnit: 'MB', toUnit: 'GB' })).toBe('1');
|
||||||
|
expect(convertStorageAndRateUnits({ value: 1000, fromUnit: 'Mb', toUnit: 'Gb' })).toBe('1');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('convert between base units', () => {
|
||||||
|
expect(convertStorageAndRateUnits({ value: 1, fromUnit: 'MB', toUnit: 'MiB' })).toBe('0.954');
|
||||||
|
expect(convertStorageAndRateUnits({ value: 1, fromUnit: 'MiB', toUnit: 'MB' })).toBe('1.049');
|
||||||
|
expect(convertStorageAndRateUnits({ value: 1000 * 1000, fromUnit: 'B', toUnit: 'MiB' })).toBe('0.954');
|
||||||
|
expect(convertStorageAndRateUnits({ value: 1024, fromUnit: 'KB', toUnit: 'MiB' })).toBe('0.977');
|
||||||
|
expect(convertStorageAndRateUnits({ value: 1000, fromUnit: 'MiB', toUnit: 'MB' })).toBe('1,048.576');
|
||||||
|
expect(convertStorageAndRateUnits({ value: 1, fromUnit: 'MB', toUnit: 'Mb' })).toBe('8');
|
||||||
|
expect(convertStorageAndRateUnits({ value: 1000, fromUnit: 'KB', toUnit: 'Kb' })).toBe('8,000');
|
||||||
|
expect(convertStorageAndRateUnits({ value: 1000, fromUnit: 'KiB', toUnit: 'Kb' })).toBe('8,192');
|
||||||
|
expect(convertStorageAndRateUnits({ value: 8, fromUnit: 'Mb', toUnit: 'MB' })).toBe('1');
|
||||||
|
|
||||||
|
expect(convertStorageAndRateUnits({ value: 1, fromUnit: 'Mb', toUnit: 'KB' })).toBe('125');
|
||||||
|
expect(convertStorageAndRateUnits({ value: 125, fromUnit: 'KB', toUnit: 'Mb' })).toBe('1');
|
||||||
|
|
||||||
|
expect(convertStorageAndRateUnits({ value: 1, fromUnit: 'MiB', toUnit: 'Kb' })).toBe('8,388.608');
|
||||||
|
expect(convertStorageAndRateUnits({ value: 8388.608, fromUnit: 'Kb', toUnit: 'MiB' })).toBe('1');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
|
@ -0,0 +1,24 @@
|
||||||
|
export function convertStorageAndRateUnits(
|
||||||
|
{ value, fromUnit, toUnit, precision = 3 }:
|
||||||
|
{ value: number; fromUnit: string; toUnit: string; precision?: number }): string {
|
||||||
|
const units = [
|
||||||
|
'iB', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB',
|
||||||
|
'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB',
|
||||||
|
'b', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb', 'Zb', 'Yb',
|
||||||
|
];
|
||||||
|
|
||||||
|
const fromIndex = units.indexOf(fromUnit);
|
||||||
|
const fromFactor = fromIndex / 9 > 1 ? 1000 : 1024;
|
||||||
|
const fromDivisor = fromIndex / 9 > 2 ? 8 : 1;
|
||||||
|
const toIndex = units.indexOf(toUnit);
|
||||||
|
const toFactor = toIndex / 9 > 1 ? 1000 : 1024;
|
||||||
|
const toDivisor = toIndex / 9 > 2 ? 8 : 1;
|
||||||
|
|
||||||
|
const fromBase = (fromFactor ** (fromIndex % 9)) / fromDivisor;
|
||||||
|
const toBase = (toFactor ** (toIndex % 9)) / toDivisor;
|
||||||
|
|
||||||
|
const result = value * fromBase / toBase;
|
||||||
|
return result.toLocaleString(undefined, {
|
||||||
|
maximumFractionDigits: precision,
|
||||||
|
});
|
||||||
|
}
|
|
@ -0,0 +1,70 @@
|
||||||
|
<script setup lang="ts">
|
||||||
|
import InputCopyable from '../../components/InputCopyable.vue';
|
||||||
|
import { convertBetweenUnits } from './data-storage-unit-converter.service';
|
||||||
|
|
||||||
|
const input = ref('1024');
|
||||||
|
const inputUnit = ref('KB');
|
||||||
|
const outputUnit = ref('MB');
|
||||||
|
const precision = ref(3);
|
||||||
|
|
||||||
|
const allUnits = ['iB', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB',
|
||||||
|
'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB',
|
||||||
|
'b', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb', 'Zb', 'Yb'];
|
||||||
|
|
||||||
|
const output = computed(() => {
|
||||||
|
try {
|
||||||
|
return convertBetweenUnits({
|
||||||
|
value: Number(input.value),
|
||||||
|
fromUnit: inputUnit.value,
|
||||||
|
toUnit: outputUnit.value,
|
||||||
|
precision: precision.value,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (e: any) {
|
||||||
|
return e.toString();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<div>
|
||||||
|
<c-card>
|
||||||
|
<c-input-text
|
||||||
|
v-model:value="input"
|
||||||
|
label="Input number"
|
||||||
|
placeholder="Put your number here (ex: 1024)" label-position="left" label-width="110px" label-align="right"
|
||||||
|
mb-2
|
||||||
|
/>
|
||||||
|
|
||||||
|
<c-select
|
||||||
|
searchable
|
||||||
|
label="Input unit:"
|
||||||
|
:value="inputUnit"
|
||||||
|
:options="allUnits"
|
||||||
|
placeholder="Select input unit"
|
||||||
|
mb-2
|
||||||
|
/>
|
||||||
|
|
||||||
|
<c-select
|
||||||
|
searchable
|
||||||
|
label="Output unit:"
|
||||||
|
:value="outputUnit"
|
||||||
|
:options="allUnits"
|
||||||
|
placeholder="Select output unit"
|
||||||
|
mb-2
|
||||||
|
/>
|
||||||
|
|
||||||
|
<n-form-item label="Precision: " label-placement="left" label-width="120" mb-2>
|
||||||
|
<n-input-number v-model:value="precision" placeholder="Precision..." :max="10" :min="0" />
|
||||||
|
</n-form-item>
|
||||||
|
|
||||||
|
<n-divider />
|
||||||
|
|
||||||
|
<InputCopyable
|
||||||
|
label="Output value"
|
||||||
|
:value="output"
|
||||||
|
placeholder="Output value will be here..."
|
||||||
|
/>
|
||||||
|
</c-card>
|
||||||
|
</div>
|
||||||
|
</template>
|
16
src/tools/data-storage-unit-converter/index.ts
Normal file
16
src/tools/data-storage-unit-converter/index.ts
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
import { ArrowsLeftRight } from '@vicons/tabler';
|
||||||
|
import { defineTool } from '../tool';
|
||||||
|
|
||||||
|
export const tool = defineTool({
|
||||||
|
name: 'Data Storage Unit converter',
|
||||||
|
path: '/data-storage-unit-converter',
|
||||||
|
description: 'Convert data storage or transfer units (bytes, bibytes, bits, kilobytes...)',
|
||||||
|
keywords: ['data', 'storage', 'unit', 'conversion',
|
||||||
|
'bits', 'bytes', 'bibytes', 'binary',
|
||||||
|
'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB',
|
||||||
|
'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB',
|
||||||
|
'b', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb', 'Zb', 'Yb'],
|
||||||
|
component: () => import('./data-storage-unit-converter.vue'),
|
||||||
|
icon: ArrowsLeftRight,
|
||||||
|
createdAt: new Date('2024-08-15'),
|
||||||
|
});
|
|
@ -2,6 +2,8 @@ import { tool as base64FileConverter } from './base64-file-converter';
|
||||||
import { tool as base64StringConverter } from './base64-string-converter';
|
import { tool as base64StringConverter } from './base64-string-converter';
|
||||||
import { tool as basicAuthGenerator } from './basic-auth-generator';
|
import { tool as basicAuthGenerator } from './basic-auth-generator';
|
||||||
import { tool as emailNormalizer } from './email-normalizer';
|
import { tool as emailNormalizer } from './email-normalizer';
|
||||||
|
import { tool as dataTransferRate } from './data-transfer-rate';
|
||||||
|
import { tool as dataStorageUnitConverter } from './data-storage-unit-converter';
|
||||||
|
|
||||||
import { tool as asciiTextDrawer } from './ascii-text-drawer';
|
import { tool as asciiTextDrawer } from './ascii-text-drawer';
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue