feat: improve units

This commit is contained in:
SethBurkart123
2025-04-01 17:24:18 +11:00
parent e884b0526b
commit 0788b78e73
3 changed files with 199 additions and 28 deletions
@@ -1,6 +1,6 @@
<script lang="ts">
import { createEventDispatcher, onDestroy } from 'svelte';
import debounce from '@/seqta/utils/debounce';
import { unitFullNames } from './unitMap';
import * as math from 'mathjs';
let { searchTerm = '', isSelected = false } = $props<{ searchTerm: string, isSelected: boolean }>();
@@ -14,28 +14,6 @@
let inputUnit = $state<string>('');
let outputUnit = $state<string>('');
// Map of unit abbreviations to full names
const unitFullNames: Record<string, string> = {
km: 'Kilometers',
m: 'Meters',
cm: 'Centimeters',
mm: 'Millimeters',
mi: 'Miles',
ft: 'Feet',
in: 'Inches',
kg: 'Kilograms',
g: 'Grams',
lb: 'Pounds',
oz: 'Ounces',
l: 'Liters',
ml: 'Milliliters',
gal: 'Gallons',
h: 'Hours',
min: 'Minutes',
s: 'Seconds',
ms: 'Milliseconds'
};
function detectUnit(expression: string): string {
try {
const unit = math.unit(expression);
@@ -51,7 +29,7 @@
}
// Process the input with debounce to avoid unnecessary calculations
const processInput = debounce((input: string) => {
const processInput = (input: string) => {
try {
if (!input.trim()) {
result = null;
@@ -103,7 +81,7 @@
} finally {
isCalculating = false;
}
}, 2);
}
$effect(() => {
processInput(searchTerm);