Photo by Pankaj Patel on Unsplash
A Node.JS Library For Parsing Numbers
A zero-dependency library for parsing numbers written out in words.
Table of contents
While working on track-time-cli, I encountered the need to parse numbers written out with words (i.e., "two days ago.") as the current implementation only supported parsing digits (i.e., "2 days ago.") via the time-speak library.
Introducing numbers-from-words
To solve this, I've created and published a new Node.JS package called numbers-from-words, which I will integrate into time-speak soon. It parses strings containing words representing a numeric value to the value itself, such as "twenty-five" or "fifty."
It separates the input into words representing numbers, magnitudes (hundred, thousand, etc.), multiples (twenty, thirty, etc.) and sums up all parsed values for the final numeric result.
How To Use It
To use numbers-from-words, add it to your project with npm i --save numbers-from-words, import the parse method, and call it with your input strings as shown below:
import { parse } from 'numbers-from-words'
const valueA = parse('twenty')
const valueB = parse('one hundred and twenty three')
const valueC = parse('one thousand, two hundred and thirty four')
console.log({
valueA, // 20
valueB, // 123
valueC // 1234
})
For more examples of possible inputs, clone the repo and run the tests, as they verify various combinations of numbers, multiples, and magnitudes and provide an overview of supported inputs.
In Conclusion
In essence, the numbers-from-words Node.JS library is a compact yet powerful tool that enables the parsing of numeric values written in word form. It's an ideal solution for projects that require this specific functionality. Your feedback and suggestions to enhance this library are warmly welcomed!