unit-converter —— Unit Conversion / Specification Structuring
Parse text values with units into standardized unit values. Pure functions (Node ESM, zero dependencies), directly importable. Extracted from
caijiqi-kaifa/electron/collector/content-parser.ts'sparseWeightToKg/parseDimensionsToCm, and extended with voltage/power/color temperature/lumen.
Use Cases
- Amazon / e-commerce platform scraped weight and dimension values in pounds / inches / oz, need to unify to kg / cm
- Product spec text (e.g.,
Weight: 2.5 kg,Dimensions: 10 x 8 x 2 inches,Voltage: 120V) needs to be structured for database storage - Logistics pricing based on weight, supply chain dimension database normalization before storing
Usage
import { parseWeightToKg, parseDimensionsToCm, parseVoltageToV, parseSpec } from './scripts/unit-converter.mjs';
parseWeightToKg('15.4 pounds'); // 6.984 (kg)
parseWeightToKg('500 g'); // 0.5
parseDimensionsToCm('10 x 8 x 2 inches');// { length: 25.4, width: 20.32, height: 5.08 }
parseDimensionsToCm('300x200x100 mm'); // { length: 30, width: 20, height: 10 }
parseVoltageToV('120 volts'); // 120
parseSpec('Weight: 1.2kg, Dimensions: 10x8x2in, Voltage 120V 60W'); // auto extract key parameters
Core API
parseWeightToKg(value)→number | null: Supports kg / pounds(lbs) / ounces(oz) / grams(g) → kg (rounded to 3 decimal places)parseDimensionsToCm(value)→{ length?, width?, height? } | null: SupportsL x W x H (inches|in|cm|mm)→ cm (rounded to 2 decimal places, defaults to inches if unit missing)parseVoltageToV(value)→number | null: V / voltsparseWattageToW(value)→number | null: W / wattsparseColorTempToK(value)→number | null: K / kelvinparseLumens(value)→number | null: lm / lumensparseSpec(text)→ Batch extract all recognizable parameters from a text string (returns object)
Notes
- Returns
nullon parsing failure (no throwing), caller decides default values. - Fully local parsing, no network calls, no data upload.