@codecompose/words-to-numbers

中文文档
2.0.1发布于 2024/03/08MITTypeScriptES Module近 30 天下载 8067

Convert textual words to numbers with optional fuzzy text matching

原始使用文档
文档目录7 节
  1. Usage
  2. Basic Examples
  3. Multiple numbers in a string
  4. With Fuzzy Matching
  5. Decimal Points
  6. Ordinal Numbers
  7. Implied Hundreds

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"); //100
wordsToNumbers("one hundred and five"); //105
wordsToNumbers("one hundred and twenty five"); //125
wordsToNumbers("four thousand and thirty"); //4030
wordsToNumbers("six million five thousand and two"); //6005002
wordsToNumbers("a thousand one hundred and eleven"); //1111
wordsToNumbers("twenty thousand five hundred and sixty nine"); //20569
wordsToNumbers("five quintillion"); //5000000000000000000
wordsToNumbers("one-hundred"); //100
wordsToNumbers("one-hundred and five"); //105
wordsToNumbers("one-hundred and twenty-five"); //125
wordsToNumbers("four-thousand and thirty"); //4030
wordsToNumbers("six-million five-thousand and two"); //6005002
wordsToNumbers("a thousand, one-hundred and eleven"); //1111
wordsToNumbers("twenty-thousand, five-hundred and sixty-nine"); //20569

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')) // 'there were 20569 X in the 5000000000000000000 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 }); //100
wordsToNumbers("too thousant and fiev", { fuzzy: true }); //2005
wordsToNumbers("tree millyon sefen hunderd and twinty sex", { fuzzy: true }); //3000726

Decimal Points

import wordsToNumbers from "words-to-numbers";
wordsToNumbers("ten point five"); //10.5
wordsToNumbers("three point one four one five nine two six"); //3.1415926

Ordinal Numbers

import wordsToNumbers from "words-to-numbers";
wordsToNumbers("first"); //1
wordsToNumbers("second"); //2
wordsToNumbers("third"); //3
wordsToNumbers("fourteenth"); //14
wordsToNumbers("twenty fifth"); //25
wordsToNumbers("thirty fourth"); //34
wordsToNumbers("forty seventh"); //47
wordsToNumbers("fifty third"); //53
wordsToNumbers("sixtieth"); //60
wordsToNumbers("seventy second"); //72
wordsToNumbers("eighty ninth"); //89
wordsToNumbers("ninety sixth"); //96
wordsToNumbers("one hundred and eighth"); //108
wordsToNumbers("one hundred and tenth"); //110
wordsToNumbers("one hundred and ninety ninth"); //199

Implied Hundreds

wordsToNumbers("nineteen eighty four", { impliedHundreds: true }); //1984
wordsToNumbers("one thirty", { impliedHundreds: true }); //130
wordsToNumbers("six sixty two", { impliedHundreds: true }); //662
wordsToNumbers("ten twelve", { impliedHundreds: true }); //1012
wordsToNumbers("nineteen ten", { impliedHundreds: true }); //1910
wordsToNumbers("twenty ten", { impliedHundreds: true }); //2010
wordsToNumbers("twenty seventeen", { impliedHundreds: true }); //2017
wordsToNumbers("twenty twenty", { impliedHundreds: true }); //2020
wordsToNumbers("twenty twenty one", { impliedHundreds: true }); //2021
wordsToNumbers("fifty sixty three", { impliedHundreds: true }); //5063
wordsToNumbers("fifty sixty", { impliedHundreds: true }); //5060
wordsToNumbers("fifty sixty three thousand", { impliedHundreds: true }); //5063000
wordsToNumbers("one hundred thousand", { impliedHundreds: true }); //100000

版本与发布记录

@codecompose/words-to-numbers 共发布 4 个版本,首次发布于 2024/02/16;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.1latest2024/03/082 年前
2.0.02024/02/232 年前
2.0.0-0next2024/02/232 年前
1.6.02024/02/162 年前

关于 @codecompose/words-to-numbers 的常见问题

@codecompose/words-to-numbers 是什么?

Convert textual words to numbers with optional fuzzy text matching

如何安装 @codecompose/words-to-numbers?

在项目目录运行 npm install @codecompose/words-to-numbers,或使用 pnpm add / yarn add / bun add。

@codecompose/words-to-numbers 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@codecompose/words-to-numbers 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@codecompose/words-to-numbers 可以商用吗?

@codecompose/words-to-numbers 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@codecompose/words-to-numbers 的下载量有多少?

根据 npm 官方下载统计,@codecompose/words-to-numbers 在 2026/10/10 – 2026/11/08 这 30 天内被下载 8,067 次,日均约 269 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@codecompose/words-to-numbers 还在维护吗?

最新版本 2.0.1 发布于 2024/03/08(2 年前);至今共发布 4 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/02/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。