Options
All
  • Public
  • Public/Protected
  • All
Menu

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

:beers:

Index

Modules

"parse-dimension"

"parse-dimension":

Const parseDimension

  • parseDimension(dimensionValue: string, __namedParameters?: { defaultUnits: units; outputUnits: units }): number
  • Parameters

    • dimensionValue: string

      string value to parse. For example, 1cm, 1 1/2", 1m, 2ft 6in

    • Default value __namedParameters: { defaultUnits: units; outputUnits: units } = {}

    Returns number

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Type alias with type parameter
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc