str2num
v1.0.4
Published
strict cast string to number
Downloads
26
Maintainers
Readme
str2num
This module is written on
typescript
, and contains the.d.ts
file. If you write ontypescript
: just use it in your project and definitions will be automatically uploaded.
npm i -S str2num
About
Strict way to convert a string to a number. If the string does not contain the number then will be thrown the error.
Exports
export default function str2num(value: string|number): number;
export function isNum(value: any): boolean;
Examples
const str2num = require('str2num')
const assert = require('assert')
assert.equal(str2num(1000), 1000)
assert.equal(str2num('1000'), 1000)
assert.equal(str2num(' + 1,000 '), 1000)
assert.equal(str2num('- 1 000'), -1000)
assert.equal(str2num('-0'), -0)
assert.equal(str2num('1.20'), 1.2)
assert.equal(str2num('.120'), 0.12)
assert.equal(str2num('120.'), 120)
assert.equal(str2num('-.123e3'), -123)
assert.equal(str2num('-.123e+3'), -123)
assert.equal(str2num('-123e-3'), -0.123)
assert.equal(str2num('-Infinity'), -Infinity)
assert.equal(isNaN(str2num('NaN')), isNaN(NaN))
assert.equal(str2num(new Number(-1)), -1)
assert.equal(str2num(new String('-1')), -1)
assert.throws(() => str2num([]))
assert.throws(() => str2num({}))
assert.throws(() => str2num(/./))
assert.throws(() => str2num(''))
assert.throws(() => str2num('.'))
assert.throws(() => str2num('e'))
assert.throws(() => str2num('.e'))
assert.throws(() => str2num('.e3'))
assert.throws(() => str2num('-123e'))
assert.throws(() => str2num('e123'))
const isNum = require('str2num').isNum
const assert = require('assert')
assert.ok(isNum(Infinity))
assert.ok(isNum('Infinity'))
assert.ok(isNum('- 123, 456 .1e1'))
assert.ok(isNum(new Number(-1)))
assert.ok(isNum(new String('-1')))
assert.ok(!isNum([]))
assert.ok(!isNum({}))
assert.ok(!isNum(/.*/))