Package Exports
- numeric-quantity
- numeric-quantity/package.json
Readme
numeric-quantity
Converts a string to a number, like an enhanced version of parseFloat. The return value will be NaN if the provided string does not resemble a number.
Features:
- In addition to plain integers and decimals,
numeric-quantitycan parse numbers with comma or underscore separators ('1,000'or'1_000'), mixed numbers ('1 2/3'), vulgar fractions ('1⅖'), and the fraction slash character ('1 2⁄3'). - To allow and ignore trailing invalid characters à la
parseFloat, pass{ allowTrailingInvalid: true }as the second argument. - To parse Roman numerals like
'MCCXIV'or'Ⅻ', pass{ romanNumerals: true }as the second argument or callparseRomanNumeralsdirectly. - Results will be rounded to three decimal places by default. To avoid rounding, pass
{ round: false }as the second argument. To round to a different number of decimal places, assign that number to theroundoption ({ round: 5 }will round to five decimal places).
For the inverse operation—converting a number to an imperial measurement—check out format-quantity.
For a more complete solution to parsing recipe ingredients, try parse-ingredient.
Usage
Installed
import { numericQuantity } from 'numeric-quantity';
console.log(numericQuantity('1 1/2')); // 1.5
console.log(numericQuantity('2 2/3')); // 2.667CDN
As an ES module:
<script type="module">
import { numericQuantity } from 'https://cdn.jsdelivr.net/npm/numeric-quantity/+esm';
console.log(numericQuantity('10½')); // 10.5
</script>As UMD (all exports are properties of the global object NumericQuantity):
<script src="https://unpkg.com/numeric-quantity"></script>
<script>
console.log(NumericQuantity.numericQuantity('xii')); // 12
</script>Other exports
| Name | Type | Description |
|---|---|---|
numericRegex |
RegExp |
Regular expression matching a string that resembles a number (using Arabic numerals) in its entirety |
numericRegexWithTrailingInvalid |
RegExp |
Same as numericRegex, but allows/ignores trailing invalid characters. |
VulgarFraction |
type |
Union type of all unicode vulgar fraction code points |
vulgarFractionsRegex |
RegExp |
Regular expression matching the first unicode vulgar fraction code point |
vulgarFractionToAsciiMap |
object |
Mapping of each vulgar fraction to its traditional ASCII representation (e.g., '½' to '1/2') |
parseRomanNumerals |
function |
Same function signature as numericQuantity, but only for Roman numerals (used internally) |
romanNumeralRegex |
RegExp |
Regular expression matching valid Roman numeral sequences (uses modern, strict rules) |
romanNumeralUnicodeRegex |
RegExp |
Regular expression matching any unicode Roman numeral code point |
romanNumeralUnicodeToAsciiMap |
object |
Mapping of each Roman numeral to its traditional ASCII representation (e.g., 'Ⅻ' to 'XII') |
romanNumeralValues |
object |
Mapping of each valid Roman numeral sequence fragment to its numeric value |
NumericQuantityOptions |
interface |
Shape of the (optional) second argument to numericQuantity |
RomanNumeralAscii |
type |
Union type of allowable Roman numeral characters (uppercase only) |
RomanNumeralUnicode |
type |
Union type of all Unicode Roman numeral characters (representing 1-12, 50, 100, 500, and 1000) |
RomanNumeral |
type |
Union type of RomanNumeralAscii and RomanNumeralUnicode |
defaultOptions |
object |
Object representing the default options |