This is a simple little utility to parse free text dimensions (i.e. 2ft, 1 1/2in, 6", 3cm) into their numerical values. It supports imperial and metric values, including shorthand equivalents. It even handles the pesky curly quotes that newer releases of iOS love to use!
npm install parse-dimension
import { parseDimension, units } from 'parse-dimension'
parseDimension(`3ft`)
// -> 36
parseDimension(`1' 6"`)
// -> 18
// specify units output value should be in
parseDimension(`24"`, { outputUnits: units.ft })
// -> 2
// specify the assumed units if none are provided
parseDimension(`2`, { defaultUnits: units.ft })
// -> 24
The second parameter is an optional options object, with the following properties:
Option | Default | Description |
---|---|---|
defaultUnits | units.in | If no units provided, parser will assume these units |
outputUnits | units.in | Dimensions that output will be converted to |
For both options, valid values are one of:
export enum units {
in = 'in',
ft = 'ft',
mm = 'mm',
m = 'm',
cm = 'cm'
}
🍻