From e46a5ec4d378eac7903761993e89a83befb2d9ec Mon Sep 17 00:00:00 2001 From: sharevb Date: Sun, 28 Apr 2024 12:30:03 +0200 Subject: [PATCH] feat(new tool): HDD Size Calculator Fix #974 --- components.d.ts | 8 +--- .../hdd-calculator.service.test.ts | 12 ++++++ .../hdd-calculator/hdd-calculator.service.ts | 14 +++++++ src/tools/hdd-calculator/hdd-calculator.vue | 38 +++++++++++++++++++ src/tools/hdd-calculator/index.ts | 14 +++++++ src/tools/index.ts | 2 + 6 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 src/tools/hdd-calculator/hdd-calculator.service.test.ts create mode 100644 src/tools/hdd-calculator/hdd-calculator.service.ts create mode 100644 src/tools/hdd-calculator/hdd-calculator.vue create mode 100644 src/tools/hdd-calculator/index.ts diff --git a/components.d.ts b/components.d.ts index e31119b36..67e1f6c24 100644 --- a/components.d.ts +++ b/components.d.ts @@ -82,6 +82,7 @@ declare module '@vue/runtime-core' { GitMemo: typeof import('./src/tools/git-memo/git-memo.vue')['default'] 'GitMemo.content': typeof import('./src/tools/git-memo/git-memo.content.md')['default'] HashText: typeof import('./src/tools/hash-text/hash-text.vue')['default'] + HddCalculator: typeof import('./src/tools/hdd-calculator/hdd-calculator.vue')['default'] HmacGenerator: typeof import('./src/tools/hmac-generator/hmac-generator.vue')['default'] 'Home.page': typeof import('./src/pages/Home.page.vue')['default'] HtmlEntities: typeof import('./src/tools/html-entities/html-entities.vue')['default'] @@ -127,24 +128,18 @@ declare module '@vue/runtime-core' { MetaTagGenerator: typeof import('./src/tools/meta-tag-generator/meta-tag-generator.vue')['default'] MimeTypes: typeof import('./src/tools/mime-types/mime-types.vue')['default'] NavbarButtons: typeof import('./src/components/NavbarButtons.vue')['default'] - NCode: typeof import('naive-ui')['NCode'] NCollapseTransition: typeof import('naive-ui')['NCollapseTransition'] NConfigProvider: typeof import('naive-ui')['NConfigProvider'] NDivider: typeof import('naive-ui')['NDivider'] NEllipsis: typeof import('naive-ui')['NEllipsis'] NFormItem: typeof import('naive-ui')['NFormItem'] - NGi: typeof import('naive-ui')['NGi'] - NGrid: typeof import('naive-ui')['NGrid'] NH1: typeof import('naive-ui')['NH1'] NH3: typeof import('naive-ui')['NH3'] NIcon: typeof import('naive-ui')['NIcon'] NInputNumber: typeof import('naive-ui')['NInputNumber'] - NLabel: typeof import('naive-ui')['NLabel'] NLayout: typeof import('naive-ui')['NLayout'] NLayoutSider: typeof import('naive-ui')['NLayoutSider'] NMenu: typeof import('naive-ui')['NMenu'] - NScrollbar: typeof import('naive-ui')['NScrollbar'] - NSpin: typeof import('naive-ui')['NSpin'] NumeronymGenerator: typeof import('./src/tools/numeronym-generator/numeronym-generator.vue')['default'] OtpCodeGeneratorAndValidator: typeof import('./src/tools/otp-code-generator-and-validator/otp-code-generator-and-validator.vue')['default'] PasswordStrengthAnalyser: typeof import('./src/tools/password-strength-analyser/password-strength-analyser.vue')['default'] @@ -159,6 +154,7 @@ declare module '@vue/runtime-core' { RouterLink: typeof import('vue-router')['RouterLink'] RouterView: typeof import('vue-router')['RouterView'] RsaKeyPairGenerator: typeof import('./src/tools/rsa-key-pair-generator/rsa-key-pair-generator.vue')['default'] + SafelinkDecoder: typeof import('./src/tools/safelink-decoder/safelink-decoder.vue')['default'] SlugifyString: typeof import('./src/tools/slugify-string/slugify-string.vue')['default'] SpanCopyable: typeof import('./src/components/SpanCopyable.vue')['default'] SqlPrettify: typeof import('./src/tools/sql-prettify/sql-prettify.vue')['default'] diff --git a/src/tools/hdd-calculator/hdd-calculator.service.test.ts b/src/tools/hdd-calculator/hdd-calculator.service.test.ts new file mode 100644 index 000000000..8c6d27f29 --- /dev/null +++ b/src/tools/hdd-calculator/hdd-calculator.service.test.ts @@ -0,0 +1,12 @@ +import { describe, expect, it } from 'vitest'; +import { getRealSize } from './hdd-calculator.service'; + +describe('hdd-calculator', () => { + it('Convert Claimed Size to Real Size', async () => { + expect(getRealSize(10, 'gb', 'tb')).to.equal(0.009094947017729282); + expect(getRealSize(10, 'pb', 'mb')).to.equal(9536743164.0625); + expect(getRealSize(100, 'tb', 'gb')).to.equal(93132.25746154785); + expect(getRealSize(1, 'pb', 'gb')).to.equal(931322.5746154785); + expect(getRealSize(1000, 'tb', 'gb')).to.equal(931322.5746154785); + }); +}); diff --git a/src/tools/hdd-calculator/hdd-calculator.service.ts b/src/tools/hdd-calculator/hdd-calculator.service.ts new file mode 100644 index 000000000..50bdc1787 --- /dev/null +++ b/src/tools/hdd-calculator/hdd-calculator.service.ts @@ -0,0 +1,14 @@ +const unitsConversion = { + kb: { dec: 1_000, bin: 1024 }, + mb: { dec: 1_000_000, bin: 1024 * 1024 }, + gb: { dec: 1_000_000_000, bin: 1024 * 1024 * 1024 }, + tb: { dec: 1_000_000_000_000, bin: 1024 * 1024 * 1024 * 1024 }, + pb: { dec: 1_000_000_000_000_000, bin: 1024 * 1024 * 1024 * 1024 * 1024 }, +}; + +export type Units = 'kb' | 'mb' | 'gb' | 'tb' | 'pb'; +export function getRealSize(claimedCapacity: number, claimedUnit: Units, toUnit: Units) { + const fromUnit = unitsConversion[claimedUnit as Units]; + const toUnitBin = unitsConversion[toUnit as Units].bin; + return claimedCapacity * fromUnit.dec / toUnitBin; +}; diff --git a/src/tools/hdd-calculator/hdd-calculator.vue b/src/tools/hdd-calculator/hdd-calculator.vue new file mode 100644 index 000000000..4c293641d --- /dev/null +++ b/src/tools/hdd-calculator/hdd-calculator.vue @@ -0,0 +1,38 @@ + + + diff --git a/src/tools/hdd-calculator/index.ts b/src/tools/hdd-calculator/index.ts new file mode 100644 index 000000000..8b3071ff1 --- /dev/null +++ b/src/tools/hdd-calculator/index.ts @@ -0,0 +1,14 @@ +import { DeviceDesktop } from '@vicons/tabler'; +import { defineTool } from '../tool'; + +export const tool = defineTool({ + name: 'HDD calculator', + path: '/hdd-calculator', + description: 'Compute real storage space (binary) from HDD Capacity (decimal)', + keywords: ['hdd', 'calculator', 'size', 'conversion', 'binary', 'decimal', + 'gb', 'mb', 'tb', + 'gigabyte', 'gibibyte', 'megabyte', 'mebibyte', 'terabyte', 'tebibyte'], + component: () => import('./hdd-calculator.vue'), + icon: DeviceDesktop, + createdAt: new Date('2024-04-07'), +}); diff --git a/src/tools/index.ts b/src/tools/index.ts index aa861c935..f2410d6a2 100644 --- a/src/tools/index.ts +++ b/src/tools/index.ts @@ -6,6 +6,7 @@ import { tool as asciiTextDrawer } from './ascii-text-drawer'; import { tool as textToUnicode } from './text-to-unicode'; import { tool as safelinkDecoder } from './safelink-decoder'; +import { tool as hddCalculator } from './hdd-calculator'; import { tool as pdfSignatureChecker } from './pdf-signature-checker'; import { tool as numeronymGenerator } from './numeronym-generator'; import { tool as macAddressGenerator } from './mac-address-generator'; @@ -107,6 +108,7 @@ export const toolsByCategory: ToolCategory[] = [ listConverter, tomlToJson, tomlToYaml, + hddCalculator, ], }, {