unit-converter —— 单位换算 / 规格参数结构化
把带单位的文本数值解析成统一标准单位的数值。纯函数(Node ESM,零依赖),可直接 import。 抽取自 caijiqi-kaifa/electron/collector/content-parser.ts 中的
parseWeightToKg/parseDimensionsToCm,并扩展电压/功率/色温/流明。
适用场景
- 亚马逊 / 电商平台抓到的重量、尺寸是 pounds / inches / oz,要统一成 kg / cm
- 商品规格文本(如
Weight: 2.5 kg、Dimensions: 10 x 8 x 2 inches、Voltage: 120V)要结构化落库 - 物流按重量计费、供应链按尺寸建库前的单位归一
用法
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'); // 自动挑关键参数
核心 API
parseWeightToKg(value)→number | null:支持 kg / pounds(lbs) / ounces(oz) / grams(g) → kg(保留 3 位)parseDimensionsToCm(value)→{ length?, width?, height? } | null:支持L x W x H (inches|in|cm|mm)→ cm(保留 2 位,缺省按 inches 算)parseVoltageToV(value)→number | null:V / voltsparseWattageToW(value)→number | null:W / wattsparseColorTempToK(value)→number | null:K / kelvinparseLumens(value)→number | null:lm / lumensparseSpec(text)→ 批量从一段文本里提取所有可识别参数(返回对象)
注意
- 解析失败返回
null(不会抛错),调用方自行决定默认值。 - 纯本地解析,不联网、不上传。
微信扫一扫