forked from CorentinTh/it-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(new tools): Data Storage/Transfer Units Converter
- Loading branch information
Showing
5 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
src/tools/data-storage-unit-converter/data-storage-unit-converter.service.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
}); | ||
}); | ||
}); |
24 changes: 24 additions & 0 deletions
24
src/tools/data-storage-unit-converter/data-storage-unit-converter.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}); | ||
} |
70 changes: 70 additions & 0 deletions
70
src/tools/data-storage-unit-converter/data-storage-unit-converter.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters