parse-css-number
v1.0.0
Published
Parses a CSS number, being either an integer or a number with a fractional component.
Downloads
7
Maintainers
Readme
parse-css-number
Parses a CSS <number>
, being either an integer or a number with a fractional component.
Installation
$ npm install parse-css-number [--save[-dev]]
Usage
const parseCSSNumber = require('parse-css-number')
// Valid numbers
// https://developer.mozilla.org/en-US/docs/Web/CSS/number#Valid_numbers
parseCSSNumber('12') // A raw <integer> is also a <number>.
parseCSSNumber('4.01') // Positive fraction
parseCSSNumber('-456.8') // Negative fraction
parseCSSNumber('0.0') // Zero
parseCSSNumber('+0.0') // Zero, with a leading +
parseCSSNumber('-0.0') // Zero, with a leading -
parseCSSNumber('.60') // Fractional number without a leading zero
parseCSSNumber('10e3') // Scientific notation
parseCSSNumber('-3.4e-2') // Complicated scientific notation
// Invalid numbers
// https://developer.mozilla.org/en-US/docs/Web/CSS/number#Invalid_numbers
parseCSSNumber('12.') // Decimal points must be followed by at least one digit.
parseCSSNumber('+-12.2') // Only one leading +/- is allowed.
parseCSSNumber('12.1.1') // Only one decimal point is allowed.
See the tests for more scenarios.
ES6/2015 import
import parseCSSNumber from 'parse-css-number'
Testing
$ npm test
This will run tests and generate a code coverage report. Anything less than 100% coverage will throw an error.