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);
@@ -2,7 +2,7 @@
import { onMount, tick } from 'svelte';
import { settingsState } from '@/seqta/utils/listeners/SettingsState'
import { fade, scale } from 'svelte/transition';
import { quintOut } from 'svelte/easing';
import { circOut, quintOut } from 'svelte/easing';
import { getStaticCommands, type StaticCommandItem } from './commands';
import { getAllDynamicItems, type DynamicContentItem } from './dynamicSearch';
import type { CombinedResult } from './types';
@@ -162,7 +162,7 @@
<div role="dialog" aria-modal="true" class={settingsState.DarkMode ? 'dark' : ''}>
<div
class="fixed inset-0 z-[50000] bg-zinc-900/40 dark:bg-black/60"
transition:fade={{ duration: 150 }}
transition:fade={{ duration: 150, easing: quintOut }}
></div>
<div class="fixed inset-0 z-[50000] flex justify-center place-items-start p-8 sm:p-6 md:p-8 select-none"
@@ -172,7 +172,7 @@
tabindex="0">
<div
class="w-full max-w-2xl rounded-xl ring-1 shadow-2xl ring-black/5 dark:ring-white/10 { transparencyEffects ? 'bg-white/80 dark:bg-zinc-900/80 backdrop-blur' : 'bg-white dark:bg-zinc-900' }"
transition:scale={{ duration: 200, start: 0.95, opacity: 0, easing: quintOut }}
transition:scale={{ duration: 100, start: 0.95, opacity: 0, easing: circOut }}
onclick={(e) => {
e.stopPropagation();
}}
@@ -0,0 +1,193 @@
export const unitFullNames: Record<string, string> = {
// --- Length ---
"m": "Meters",
"km": "Kilometers",
"cm": "Centimeters",
"mm": "Millimeters",
"µm": "Micrometers",
"nm": "Nanometers",
"pm": "Picometers",
"fm": "Femtometers",
"am": "Attometers",
"zm": "Zeptometers",
"ym": "Yoctometers",
"mi": "Miles",
"yd": "Yards",
"ft": "Feet",
"in": "Inches",
"nmi": "Nautical Miles",
"angstrom": "Angstroms",
"au": "Astronomical Units",
"ly": "Light Years",
"pc": "Parsecs",
// --- Mass ---
"kg": "Kilograms",
"g": "Grams",
"mg": "Milligrams",
"µg": "Micrograms",
"ng": "Nanograms",
"lb": "Pounds",
"oz": "Ounces",
"ton": "Tons (Imperial)",
"tonne": "Tonnes (Metric)",
"slug": "Slugs",
"stone": "Stones",
// --- Time ---
"s": "Seconds",
"ms": "Milliseconds",
"µs": "Microseconds",
"ns": "Nanoseconds",
"ps": "Picoseconds",
"min": "Minutes",
"h": "Hours",
"day": "Days",
"week": "Weeks",
"month": "Months (30 days)",
"year": "Years (365 days)",
"fortnight": "Fortnights",
// --- Temperature ---
"K": "Kelvin",
"degC": "Degrees Celsius",
"degF": "Degrees Fahrenheit",
"degR": "Degrees Rankine",
// --- Volume ---
"m³": "Cubic Meters",
"cm³": "Cubic Centimeters",
"mm³": "Cubic Millimeters",
"l": "Liters",
"ml": "Milliliters",
"gal": "Gallons (US)",
"qt": "Quarts (US)",
"pt": "Pints (US)",
"cup": "Cups (US)",
"floz": "Fluid Ounces (US)",
"tbsp": "Tablespoons (US)",
"tsp": "Teaspoons (US)",
// --- Area ---
"m²": "Square Meters",
"km²": "Square Kilometers",
"cm²": "Square Centimeters",
"mm²": "Square Millimeters",
"ha": "Hectares",
"acre": "Acres",
"ft²": "Square Feet",
"in²": "Square Inches",
"mi²": "Square Miles",
// --- Speed ---
"m/s": "Meters per Second",
"km/h": "Kilometers per Hour",
"mph": "Miles per Hour",
"knot": "Knots",
// --- Acceleration ---
"m/s²": "Meters per Second Squared",
// --- Force ---
"N": "Newtons",
"lbf": "Pound-Force",
"dyn": "Dynes",
// --- Energy ---
"J": "Joules",
"kJ": "Kilojoules",
"cal": "Calories",
"kcal": "Kilocalories",
"Wh": "Watt Hours",
"kWh": "Kilowatt Hours",
"BTU": "British Thermal Units",
"erg": "Ergs",
"eV": "Electronvolts",
// --- Power ---
"W": "Watts",
"kW": "Kilowatts",
"MW": "Megawatts",
"GW": "Gigawatts",
"hp": "Horsepower",
// --- Pressure ---
"Pa": "Pascals",
"kPa": "Kilopascals",
"bar": "Bar",
"atm": "Atmospheres",
"psi": "Pounds per Square Inch",
"torr": "Torr",
"mmHg": "Millimeters of Mercury",
// --- Frequency ---
"Hz": "Hertz",
"kHz": "Kilohertz",
"MHz": "Megahertz",
"GHz": "Gigahertz",
"THz": "Terahertz",
// --- Electric ---
"V": "Volts",
"A": "Amperes",
"C": "Coulombs",
"Ω": "Ohms",
"F": "Farads",
"S": "Siemens",
"H": "Henries",
"Wb": "Webers",
"T": "Teslas",
"lx": "Lux",
// --- Angle & Rotation ---
"rad": "Radians",
"deg": "Degrees",
"grad": "Gradians",
"cycle": "Cycles",
"turn": "Turns",
"rev": "Revolutions",
// --- Charge & Capacitance ---
"e": "Elementary Charges",
// --- Magnetic & Light ---
"lm": "Lumens",
"ph": "Photons",
// --- Miscellaneous / Dimensionless ---
"%": "Percent",
"ppm": "Parts per Million",
"ppb": "Parts per Billion",
"pptr": "Parts per Trillion",
"dB": "Decibels",
"bit": "Bits",
"byte": "Bytes",
// --- Digital Storage ---
"b": "Bits",
"B": "Bytes",
"kb": "Kilobits",
"kB": "Kilobytes",
"Mb": "Megabits",
"MB": "Megabytes",
"Gb": "Gigabits",
"GB": "Gigabytes",
"Tb": "Terabits",
"TB": "Terabytes",
// --- Currency ---
"USD": "US Dollars",
"EUR": "Euros",
"GBP": "British Pounds",
"AUD": "Australian Dollars",
"CAD": "Canadian Dollars",
"CHF": "Swiss Francs",
"JPY": "Japanese Yen",
"CNY": "Chinese Yuan",
"INR": "Indian Rupees",
"NZD": "New Zealand Dollars",
"SEK": "Swedish Krona",
"NOK": "Norwegian Krone",
"SGD": "Singapore Dollars",
"HKD": "Hong Kong Dollars"
};