Words To Numbers
This repository was forked from
words-from-numbers because the
project seems abandoned.
The codebase was modernized and published under 2.0 with the following changes:
- Fix the invalid package manifest
- Remove transpilation and make it
pure ESM
- Add Typescript
type inference
- Remove obsolete and old dependencies
- Use Vitest to replace older testing tools
- Upgrade clj-fuzzy to latest version
- Add prettier formatting
- Use PNPM as package manager
I might convert the code to Typescript later, but the code quality is pretty bad
and I am not sure it's worth it. PRs are welcome.
I have also replaced NPM with PNPM, which was unnecessary but just personal
preference.
Usage
npm install @codecompose/words-to-numbers
If the whole string passed is a number then it will return a Number
type
otherwise it will return the original string with all instances of numbers
replaced.
TODO: Add functionality for parsing mixed numbers and words. PRs welcome.
Basic Examples
import wordsToNumbers from "words-to-numbers";
wordsToNumbers("one hundred");
wordsToNumbers("one hundred and five");
wordsToNumbers("one hundred and twenty five");
wordsToNumbers("four thousand and thirty");
wordsToNumbers("six million five thousand and two");
wordsToNumbers("a thousand one hundred and eleven");
wordsToNumbers("twenty thousand five hundred and sixty nine");
wordsToNumbers("five quintillion");
wordsToNumbers("one-hundred");
wordsToNumbers("one-hundred and five");
wordsToNumbers("one-hundred and twenty-five");
wordsToNumbers("four-thousand and thirty");
wordsToNumbers("six-million five-thousand and two");
wordsToNumbers("a thousand, one-hundred and eleven");
wordsToNumbers("twenty-thousand, five-hundred and sixty-nine");
Multiple numbers in a string
Returns a string with all instances replaced.
wordsToNumbers('there were twenty-thousand, five-hundred and sixty-nine X in the five quintillion Y'))
With Fuzzy Matching
Uses
Jaro distance
to find the best match for the number words. Don't rely on this being completely
accurate...
import wordsToNumbers from "words-to-numbers";
wordsToNumbers("won huntred", { fuzzy: true });
wordsToNumbers("too thousant and fiev", { fuzzy: true });
wordsToNumbers("tree millyon sefen hunderd and twinty sex", { fuzzy: true });
Decimal Points
import wordsToNumbers from "words-to-numbers";
wordsToNumbers("ten point five");
wordsToNumbers("three point one four one five nine two six");
Ordinal Numbers
import wordsToNumbers from "words-to-numbers";
wordsToNumbers("first");
wordsToNumbers("second");
wordsToNumbers("third");
wordsToNumbers("fourteenth");
wordsToNumbers("twenty fifth");
wordsToNumbers("thirty fourth");
wordsToNumbers("forty seventh");
wordsToNumbers("fifty third");
wordsToNumbers("sixtieth");
wordsToNumbers("seventy second");
wordsToNumbers("eighty ninth");
wordsToNumbers("ninety sixth");
wordsToNumbers("one hundred and eighth");
wordsToNumbers("one hundred and tenth");
wordsToNumbers("one hundred and ninety ninth");
Implied Hundreds
wordsToNumbers("nineteen eighty four", { impliedHundreds: true });
wordsToNumbers("one thirty", { impliedHundreds: true });
wordsToNumbers("six sixty two", { impliedHundreds: true });
wordsToNumbers("ten twelve", { impliedHundreds: true });
wordsToNumbers("nineteen ten", { impliedHundreds: true });
wordsToNumbers("twenty ten", { impliedHundreds: true });
wordsToNumbers("twenty seventeen", { impliedHundreds: true });
wordsToNumbers("twenty twenty", { impliedHundreds: true });
wordsToNumbers("twenty twenty one", { impliedHundreds: true });
wordsToNumbers("fifty sixty three", { impliedHundreds: true });
wordsToNumbers("fifty sixty", { impliedHundreds: true });
wordsToNumbers("fifty sixty three thousand", { impliedHundreds: true });
wordsToNumbers("one hundred thousand", { impliedHundreds: true });