Dimension Helpers
Utilities for converting between millimeters and imperial units live in src/utils/dimensions.ts and are exported by the package entry point:
import {
mmToInches,
inchesToMm,
imperialToMm,
dimensionToMmNumber,
formatDimensions,
toInchesDisplay,
} from "inresi-indexeddb-orm";
Conversion utilities
| Function | Description |
|---|---|
mmToInches(mm, precision?, appendSymbol?) | Converts raw millimeters to inches. Optional precision controls rounding, and setting appendSymbol affixes a double-quote (") to the returned string. |
inchesToMm(inches) | Converts inches back to millimeters without rounding. |
imperialToMm(value) | Parses human-entered imperial strings such as 6' 8" or 80in and returns a millimeter number, or null when parsing fails. |
dimensionToMmNumber(value) | Accepts either a numeric millimeter value or an imperial string. Bare numbers are treated as inches, all other units are normalized through imperialToMm, and the result is rounded to the nearest millimeter. Returns undefined when the input cannot be parsed. |
Display helpers
toInchesDisplay(value, { precision = 1, appendSymbol = false })converts a millimeter value (or string) into a rounded inch string ready for UI display.formatDimensions({ width?, depth?, height? }, { precision = 1, appendSymbol = false, delimiter = " x " })builds a composite label like80"W x 24"D x 34"H, omitting axes with missing data.
Both helpers accept numbers or strings and reuse the shared conversion pipeline so formatting is consistent with the parsing functions above.
Common flows
// Parse an input field that may be "6' 8\"", "80", or "2032"
const mm = dimensionToMmNumber(userInput);
// Persist the millimeter value directly in IndexedDB
await kvSet("app", { doorwayWidth: mm ?? null });
// Later render the saved value
const label = toInchesDisplay(mm, { appendSymbol: true });
// or render multiple dimensions at once:
const size = formatDimensions({
width: unit.widthMm,
depth: unit.depthMm,
height: unit.heightMm,
});
These helpers ensure conversions remain consistent across storage, validation, and presentation layers.