2.0.0 • Published 4 years ago

parse-dimension v2.0.0

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

parse-dimension

styled with prettier Travis Coverage Status semantic-release

What is this?

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!

Getting Started

Install

npm install parse-dimension

Usage

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

Options

The second parameter is an optional options object, with the following properties:

OptionDefaultDescription
defaultUnitsunits.inIf no units provided, parser will assume these units
outputUnitsunits.inDimensions 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'
}

:beers: