JSPM

numero

0.1.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 480
  • Score
    100M100P100Q92178F
  • License MIT

A stricter `parseInt` and `parseFloat`.

Package Exports

  • numero

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (numero) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Numero.js npm Version Build Status Coverage Status

A stricter parseInt and parseFloat.

Usage

numero.parseInt(42);      // 42
numero.parseInt('42');    // 42
numero.parseInt('42 ');   // 42
numero.parseInt(3.14);    // null
numero.parseInt('3.14');  // null
numero.parseInt(' 3.14'); // null

numero.parseFloat(42);      // 42
numero.parseFloat('42');    // 42
numero.parseFloat('42 ');   // 42
numero.parseFloat(3.14);    // 3.14
numero.parseFloat('3.14');  // 3.14
numero.parseFloat(' 3.14'); // 3.14
  1. null is returned if x cannot be parsed to an integer/float.

  2. An integer is also a float.

  3. x can have trailing/leading whitespace.

Numero is stricter than the native parseInt and parseFloat in that extraneous characters are not allowed. For example:

parseInt('42foo', 10);    // 42
numero.parseInt('42foo'); // null

parseFloat('3.14foo');        // 3.14
numero.parseFloat('3.14foo'); // null

Numero can also handle numbers in their octal and hexadecimal representations, and negative numbers too:

numero.parseInt('052');     // 42
numero.parseFloat('-0x2a'); // -42

Check the tests.

API

numero.isInt(x)

Returns true if x can be parsed to an integer. Alias: isInteger

numero.parseInt(x)

Returns an integer, or null if x cannot be parsed to an integer. Alias: parseInteger.

numero.isFloat(x)

Returns true if x can be parsed to a float.

numero.parseFloat(x)

Returns a float, or null if x cannot be parsed to a float.

Installation

Install via npm:

$ npm i --save numero

Changelog

  • 0.1.0
    • Initial release

License

MIT license