Skip to content
This repository has been archived by the owner on Nov 12, 2020. It is now read-only.
/ is-string-a-number Public archive

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

License

Notifications You must be signed in to change notification settings

Ynote/is-string-a-number

Repository files navigation

? This project has moved to https://gitlab.com/ynote_hk/is-string-a-number.

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

About

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

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published