parse-dimension
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

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:

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'
}

🍻

Package Sidebar

Install

npm i parse-dimension

Weekly Downloads

0

Version

2.0.0

License

MIT

Unpacked Size

420 kB

Total Files

10

Last publish

Collaborators

  • tyschroed