feat(new tool): dBm mW Converter

Fix #1658
This commit is contained in:
ShareVB committed 2025-10-04 21:33:57 +02:00
1 parent a47e25a360
commit cb3d366080
4 files changed
+98

No files matched your search

@@ -0,0 +1,38 @@
import { describe, expect, it } from 'vitest';
import { dbmToMw, mwToDbm } from './dbm-mw-converter.service'; // Adjust path as needed
describe('Power Conversion Utilities', () => {
describe('dbmToMw', () => {
it('should convert 30 dBm to 1 W', () => {
expect(dbmToMw(30)).toBeCloseTo(1000, 5);
});
it('should convert 0 dBm to 1 mW', () => {
expect(dbmToMw(0)).toBeCloseTo(1, 5);
});
it('should convert 10 dBm to 10 mW', () => {
expect(dbmToMw(10)).toBeCloseTo(10, 5);
});
it('should convert -30 dBm to 0.001 mW', () => {
expect(dbmToMw(-30)).toBeCloseTo(0.001, 5);
});
});
describe('mwToDbm', () => {
it('should convert 1 mW to 0 dBm', () => {
expect(mwToDbm(1)).toBeCloseTo(0, 5);
});
it('should convert 10 mW to 10 dBm', () => {
expect(mwToDbm(10)).toBeCloseTo(10, 5);
});
it('should throw an error for non-positive input', () => {
expect(() => mwToDbm(0)).toThrow();
expect(() => mwToDbm(-5)).toThrow();
});
});
});
@@ -0,0 +1,20 @@
/**
* Converts dBm to milliwatts (mW)
* @param dbm - Power level in dBm
* @returns Power level in milliwatts
*/
export function dbmToMw(dbm: number): number {
return 10 ** (dbm / 10);
}
/**
* Converts milliwatts (mW) to dBm
* @param mw - Power level in milliwatts
* @returns Power level in dBm
*/
export function mwToDbm(mw: number): number {
if (mw <= 0) {
throw new Error('Power in milliwatts must be greater than 0');
}
return 10 * Math.log10(mw);
}
@@ -0,0 +1,27 @@
<script setup lang="ts">
import { dbmToMw, mwToDbm } from './dbm-mw-converter.service'; // Adjust path as needed
const dbmInput = ref(1);
const mwOutput = computed(() => dbmToMw(dbmInput.value).toFixed(3));
const mwInput = ref(1);
const dbmOutput = computed(() => mwToDbm(mwInput.value).toFixed(3));
</script>
<template>
<c-card title="dBm to mW">
<n-form-item label="dBm value:" label-placement="left">
<n-input-number-i18n v-model:value="dbmInput" />
</n-form-item>
<input-copyable label="Corresponding mW value:" label-position="left" :value="mwOutput" />
</c-card>
<c-card title="mW to dBm">
<n-form-item label="mW value:" label-placement="left">
<n-input-number-i18n v-model:value="mwInput" :min="0" />
</n-form-item>
<input-copyable label="Corresponding dBm value:" label-position="left" :value="dbmOutput" />
</c-card>
</template>
+13
View File
@@ -0,0 +1,13 @@
import { DeviceAudioTape } from '@vicons/tabler';
import { defineTool } from '../tool';
export const tool = defineTool({
name: 'dBm mW Converter',
path: '/dbm-mw-converter',
description: 'Convert dBm to mW and conversely',
keywords: ['dbm', 'mw', 'converter'],
component: () => import('./dbm-mw-converter.vue'),
icon: DeviceAudioTape,
createdAt: new Date('2025-10-03'),
category: 'Default',
});