is-string-a-number

2.0.2 • Public • Published

is-string-a-number NPM version Bundle size

Returns true if the value is a string number written in fixed notation and in decimal base.

Motivation

This utility module has a front-end purpose. As JavaScript doesn't provide a clean and unique way to check if a value is a number or written in a specific notation or base, type checking on numbers is hard.

Front-end developers often have to deal with user inputs, especially within forms. The need, here, is to validate a string value as a simple number in decimal base and fixed notation.

Code example

import isStringANumber from 'is-string-a-number'
 
isStringANumber('42')    // => true
isStringANumber('42e42') // => false
isStringANumber('0x2a')  // => false
isStringANumber('42.')   // => false
isStringANumber(42)      // => false

More details on the usage section.

Install

Install with npm:

$ npm install --save is-string-a-number

Usage

With CommonJs:

const isStringANumber = require('is-string-a-number')

With ECMAScript module:

import isStringANumber from 'is-string-a-number'

true

isStringANumber('0')     // => true
isStringANumber('42')    // => true
isStringANumber('42.2')  // => true
isStringANumber('-42')   // => true
isStringANumber('-42.2') // => true
isStringANumber('-0.42') // => true

false

isStringANumber(0)                     // => false
isStringANumber(.42)                   // => false
isStringANumber(42)                    // => false
isStringANumber(-42)                   // => false
isStringANumber(-42.2)                 // => false
isStringANumber('+42')                 // => false
isStringANumber('42,2')                // => false
isStringANumber('42e24')               // => false
isStringANumber('42E24')               // => false
isStringANumber('42e+24')              // => false
isStringANumber('42e-24')              // => false
isStringANumber('42foobar')            // => false
isStringANumber('0x2a')                // => false
isStringANumber('.')                   // => false
isStringANumber('42.')                 // => false
isStringANumber(null)                  // => false
isStringANumber(undefined)             // => false
isStringANumber(NaN)                   // => false
isStringANumber('Alice in Wonderland') // => false
isStringANumber('')                    // => false
isStringANumber('   ')                 // => false
isStringANumber('\r\n\t')              // => false
isStringANumber([])                    // => false
isStringANumber({})                    // => false
isStringANumber(function() {})         // => false
isStringANumber(true)                  // => false
isStringANumber(false)                 // => false
isStringANumber('true')                // => false
isStringANumber('false')               // => false
isStringANumber(Infinity)              // => false
isStringANumber(-Infinity)             // => false
isStringANumber(new Date())            // => false

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/Ynote/is-string-a-number. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

Code of Conduct

Everyone interacting in the is-string-a-number project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Credits

Special thanks to Jon Schlinkert for his great is-number module that inspired me.

Resources

Licence

The module is available as open source under the terms of the MIT License.

MIT © Ynote_hk

Package Sidebar

Install

npm i is-string-a-number

Weekly Downloads

191

Version

2.0.2

License

MIT

Unpacked Size

12.7 kB

Total Files

9

Last publish

Collaborators

  • ynote_hk