feat: convert inputs when switching units

This commit is contained in:
w33ble
2026-01-11 19:47:03 -07:00
parent 7c512f5adb
commit 56c6ac47eb
4 changed files with 52 additions and 3 deletions

View File

@@ -7,6 +7,8 @@
## Recent Changes ## Recent Changes
- Created `memory-bank/projectBrief.md`. - Created `memory-bank/projectBrief.md`.
- Created `memory-bank/productContext.md`. - Created `memory-bank/productContext.md`.
- Implemented unit conversion on toggle in `src/main.ts`.
- Extracted `convertUnits` utility and added tests.
## Current Focus ## Current Focus
- Completing Memory Bank initialization. - Completing Memory Bank initialization.
@@ -18,3 +20,4 @@
- Using **Vanilla TypeScript** to avoid framework overhead. - Using **Vanilla TypeScript** to avoid framework overhead.
- Using **Vitest** for unit testing calculation logic. - Using **Vitest** for unit testing calculation logic.
- Target: A single-page application that can be built into a self-contained output. - Target: A single-page application that can be built into a self-contained output.
- **Unit Toggle Behavior**: Switching units now automatically converts existing input values using a factor of 25.4. MM values are rounded to 1 decimal, Inches to 2 decimals.

View File

@@ -1,5 +1,22 @@
import { describe, it, expect } from 'vitest'; import { describe, it, expect } from 'vitest';
import { calculateGridfinity, DEFAULT_SPEC } from './calculator'; import { calculateGridfinity, DEFAULT_SPEC, convertUnits } from './calculator';
describe('Unit Conversion', () => {
it('converts inches to mm correctly', () => {
expect(convertUnits(1, 'in', 'mm')).toBe(25.4);
expect(convertUnits(2, 'in', 'mm')).toBe(50.8);
});
it('converts mm to inches correctly', () => {
expect(convertUnits(25.4, 'mm', 'in')).toBe(1);
expect(convertUnits(50.8, 'mm', 'in')).toBe(2);
});
it('returns same value if units are same', () => {
expect(convertUnits(42, 'mm', 'mm')).toBe(42);
expect(convertUnits(1, 'in', 'in')).toBe(1);
});
});
describe('Gridfinity Calculator', () => { describe('Gridfinity Calculator', () => {
it('calculates standard 42mm grids correctly in mm', () => { it('calculates standard 42mm grids correctly in mm', () => {

View File

@@ -29,7 +29,14 @@ export interface CalculatorResults {
remainingHeight: number; remainingHeight: number;
} }
const INCH_TO_MM = 25.4; export const INCH_TO_MM = 25.4;
export function convertUnits(value: number, from: 'mm' | 'in', to: 'mm' | 'in'): number {
if (from === to) return value;
if (from === 'in' && to === 'mm') return value * INCH_TO_MM;
if (from === 'mm' && to === 'in') return value / INCH_TO_MM;
return value;
}
export function calculateGridfinity(inputs: CalculatorInputs): CalculatorResults { export function calculateGridfinity(inputs: CalculatorInputs): CalculatorResults {
const { width, length, height, unit, spec } = inputs; const { width, length, height, unit, spec } = inputs;

View File

@@ -1,4 +1,4 @@
import { calculateGridfinity, GridfinitySpec } from './calculator'; import { calculateGridfinity, GridfinitySpec, convertUnits } from './calculator';
// DOM Elements // DOM Elements
const inputWidth = document.getElementById('input-width') as HTMLInputElement; const inputWidth = document.getElementById('input-width') as HTMLInputElement;
@@ -69,6 +69,17 @@ function updateResults() {
}); });
unitMmBtn.addEventListener('click', () => { unitMmBtn.addEventListener('click', () => {
if (currentUnit === 'mm') return;
// Convert values
const width = parseFloat(inputWidth.value) || 0;
const length = parseFloat(inputLength.value) || 0;
const height = parseFloat(inputHeight.value) || 0;
if (width > 0) inputWidth.value = convertUnits(width, 'in', 'mm').toFixed(1);
if (length > 0) inputLength.value = convertUnits(length, 'in', 'mm').toFixed(1);
if (height > 0) inputHeight.value = convertUnits(height, 'in', 'mm').toFixed(1);
currentUnit = 'mm'; currentUnit = 'mm';
unitMmBtn.classList.add('bg-white', 'shadow-sm'); unitMmBtn.classList.add('bg-white', 'shadow-sm');
unitMmBtn.classList.remove('text-slate-600'); unitMmBtn.classList.remove('text-slate-600');
@@ -78,6 +89,17 @@ unitMmBtn.addEventListener('click', () => {
}); });
unitInBtn.addEventListener('click', () => { unitInBtn.addEventListener('click', () => {
if (currentUnit === 'in') return;
// Convert values
const width = parseFloat(inputWidth.value) || 0;
const length = parseFloat(inputLength.value) || 0;
const height = parseFloat(inputHeight.value) || 0;
if (width > 0) inputWidth.value = convertUnits(width, 'mm', 'in').toFixed(2);
if (length > 0) inputLength.value = convertUnits(length, 'mm', 'in').toFixed(2);
if (height > 0) inputHeight.value = convertUnits(height, 'mm', 'in').toFixed(2);
currentUnit = 'in'; currentUnit = 'in';
unitInBtn.classList.add('bg-white', 'shadow-sm'); unitInBtn.classList.add('bg-white', 'shadow-sm');
unitInBtn.classList.remove('text-slate-600'); unitInBtn.classList.remove('text-slate-600');