hemanth / nmotw.in

Node Module Of The Week
https://nmotw.in
42 stars 7 forks source link

numParse - parse number from any string if it's possible #69

Open PazzaVlad opened 4 years ago

PazzaVlad commented 4 years ago

parseFloat can't parse number between words, can't parse formatted numbers like "5,000.55" and has other restrictions. So I created this tiny lib to solve this problem.

const numParse = require('num-parse')

numParse('text5.5text') // -> 5.5
numParse('text-5.5text') // -> -5.5
numParse('text%&*-5.5text') // -> -5.5
numParse('text--5.5text') // -> -5.5
numParse('test some-5.5m') // -> -5.5

numParse('5,000') // -> 5000
numParse('text 5,000,55') // -> 500055
numParse('5,000.55') // -> 5000.55
numParse('text5,000.55some') // -> 5000.55

numParse('text 5,000 55') // -> 5000
numParse('text 5,000.55 some 100000 other 345') // -> 5000.55

numParse('srt') // -> NaN
numParse('') // -> NaN
numParse(undefined) // -> NaN
numParse(null) // -> NaN