numberify-converter
is a simple, lightweight package for converting numbers written in text form (currently supporting English and French) into their numeric equivalents. This can be useful for localization, natural language processing, or any scenario where you need to handle numbers written in natural language.
- Features
- Installation
- Usage
- API Documentation
- Supported Languages
- Error Handling
- Contributing
- Testing
- License
-
Convert Numbers in Natural Language: Transform numbers written in text (e.g., "two hundred and twenty-three") into their numeric equivalent (
223
). -
Multiple Language Support: Currently supports English (
en
) and French (fr
). - TypeScript Ready: Fully typed for seamless TypeScript integration.
- Lightweight: Minimal dependencies to keep your project lean.
You can install the package via npm
or yarn
:
npm install numberify-converter
yarn add numberify-converter
After installing, you can use the numberifyString
function to convert numbers from text to numeric format. Here's an example for both English and French.
import { numberifyString } from "numberify-converter";
// Convert from French
const frenchNumber = numberifyString("deux cent vingt-trois", "fr"); // Outputs: 223
// Convert from English
const englishNumber = numberifyString("two hundred and twenty-three", "en"); // Outputs: 223
import React from "react";
import { numberifyString } from "numberify-converter";
const NumberConverter = () => {
const textNumber = "deux cent vingt-trois";
const numericValue = numberifyString(textNumber, "fr");
return (
<div>
<p>
The number "{textNumber}" in numeric form is: {numericValue}
</p>
</div>
);
};
export default NumberConverter;
- text: The number string written in natural language (e.g., "two hundred and twenty-three").
-
lang: The language code of the number string. Supported values are
"en"
for English and"fr"
for French.
const result = numberifyString("two hundred and fifty", "en"); // Output: "250"
Currently, numberify-converter
supports two languages:
- English (
en
) - French (
fr
)
const result = numberifyString("cent cinquante", "fr"); // Output: "150"
const result = numberifyString("one hundred fifty", "en"); // Output: "150"
More languages may be added in future updates. Feel free to contribute!
The function will return an empty string (""
) if the input cannot be parsed correctly or the language is unsupported.
try {
const result = numberifyString("invalid text", "en");
if (!result) throw new Error("Unable to convert text to number");
} catch (error) {
console.error(error.message); // Outputs: Unable to convert text to number
}
We welcome contributions! Here's how you can contribute:
Start by forking this repository to your GitHub account.
git clone https://github.com/your-username/numberify-converter.git
Install all dependencies for development:
npm install
# or
yarn install
Create a new branch for your feature:
git checkout -b feature/new-language-support
Run the tests to ensure everything works as expected:
npm run test
# or
yarn test
git commit -m "Add support for a new language"
Push your changes to your forked repository and create a pull request.
git push origin feature/new-language-support
In your pull request, describe the changes you've made and why they are important.
To ensure the package works as expected, we use Jest for testing.
npm run test
# or
yarn test
Tests are located in the tests/
directory, and you can add new test cases as needed.
test("Convert numbers from French", () => {
expect(numberifyString("deux cent vingt-trois", "fr")).toBe("223");
});
test("Convert numbers from English", () => {
expect(numberifyString("two hundred and twenty-three", "en")).toBe("223");
});
This project is licensed under the MIT License. See the LICENSE file for details.