@sustainhawaii/unit-converter

1.5.47 • Public • Published

Methods for parsing measurement amounts and units from any text.

convertUnits

Pass through convert-units module as 'convertUnits'

Kind: global constant

regexes

Pass through rgxs as regexes

Kind: global constant

fractionsToDecimals ⇒ string

Converts fractions to decimal equivalents within a string.

Kind: global constant
Params

  • str string

Example

"10 tablespoons (1 1/4 sticks or 5 ounces) butter" becomes "10 tablespoons (1.25 sticks or 5 ounces) butter"

normalFracsToDecimals ⇒ number

Convert a string fraction like '1/4' to 0.25 or '1 1/4' to 1.25

Kind: global constant
Params

  • str string

vulgarFracsToDecimals ⇒ number

Convert a string fraction like '1¼' or '1 ¼' to 1.25

Kind: global constant
Params

  • str string

convertDecimalRanges

Convert strings like '1.8-2', '1 - 2.56', '1 to 2', '1 or 2' within larger strings to the mean, min, or max of the two numbers. Assumes vulgar fractions have been converted to decimals.

Kind: global constant
Params

  • str string
  • [method] string - 'mean', 'min', or 'max'

findUomAndAmountOfMeasurementType ⇒ object

Find the first measurement of typeOfMeasure and return unit of measure and amount. Return undefined if no matching measure type.

Kind: global constant
Returns: object - like {uom: 'g', amount: 52}
Params

  • str string
  • typeOfMeasure string - ('mass', 'volume', or 'energy')
  • [rangeConversionMethod] string - 'mean', 'min', or 'max'

Example

'10 tablespoons (1 1/4 sticks or 5 ounces) butter' results in { uom: 'oz', amount: 5 }

parseUomsAndAmounts ⇒ array

Return array of objects like { amount: 70, uom: 'lb', measure: 'mass'}

Kind: global constant
Params

  • str string
  • [rangeConversionMethod] string - 'mean', 'min', or 'max'

Example

'10 tablespoons (1 1/4 sticks or 5+ ounces or 2 + grams) butter' results in [
     {
          amount: 10,
          uom: 'Tbs',
          originalUom: 'tablespoons',
          measure: 'volume'
        },
        {
          amount: 5,
          uom: 'oz',
          originalUom: 'ounces',
          measure: 'mass'
        },
        {
          amount: 2,
          uom: 'g',
          originalUom: 'grams',
          measure: 'mass'
        }
]

parseUomAndAmount ⇒ Object

Return amount, unitOfMeasure, and measure from a string.

Kind: global constant
Params

  • str string - like '1.5 ounces' or '1.5+ ounces' or '1 15-ounce' or '1 x 15-ounce' or '1 x 15 ounce'

Example

'70 pounds' becomes { amount: 70, uom: 'lb', measure: 'mass'}

NOTE: Amounts must be in decimals, not fractions.

getMeasurementSubstrings ⇒ Array. ⎮ undefined

Return array of amount-uom substrings found in a string whose amounts have been decimalized and ranges converted.

Kind: global constant
Params

  • str string

getAllDecimalMeasurePatternMatches ⇒ Array.

Return all unique simple and compound measurement pattern matches within a decimalized, range-converted string.

Kind: global constant
Params

  • str string

resolveUOM ⇒ Object ⎮ *

Parse unit of measure and general type of measure ('mass', 'volume', or 'energy') from string. Returns empty object if fails.

Kind: global constant
Params

  • str string

Example

Converts "kilogram" to { unitOfMeasure: 'kg', measure: 'mass' }

listUnits

Developer's helper to find certain kinds of units supported by convert-units module.

Kind: global constant

Dependencies (4)

Dev Dependencies (13)

Package Sidebar

Install

npm i @sustainhawaii/unit-converter

Weekly Downloads

4

Version

1.5.47

License

MIT

Unpacked Size

93.8 kB

Total Files

30

Last publish

Collaborators

  • nidhinmathew
  • basavarajaut
  • abhisustain
  • muralisustain
  • anirsustain
  • cksh