numberify-converter
v1.0.2
Published
Convert numbers from text in various languages into their numeric equivalent.
Downloads
8
Maintainers
Readme
Numberify Converter
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.
Table of Contents
- Features
- Installation
- Usage
- API Documentation
- Supported Languages
- Error Handling
- Contributing
- Testing
- License
Features
- 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.
Installation
You can install the package via npm
or yarn
:
Using npm:
npm install numberify-converter
Using Yarn:
yarn add numberify-converter
Usage
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.
Basic Example:
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
Usage in a React Project:
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;
API Documentation
numberifyString(text: string, lang: string): string
- 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.
Example:
const result = numberifyString("two hundred and fifty", "en"); // Output: "250"
Supported Languages
Currently, numberify-converter
supports two languages:
- English (
en
) - French (
fr
)
Example for French:
const result = numberifyString("cent cinquante", "fr"); // Output: "150"
Example for English:
const result = numberifyString("one hundred fifty", "en"); // Output: "150"
More languages may be added in future updates. Feel free to contribute!
Error Handling
The function will return an empty string (""
) if the input cannot be parsed correctly or the language is unsupported.
Example of Handling Errors:
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
}
Contributing
We welcome contributions! Here's how you can contribute:
1. Fork the repository
Start by forking this repository to your GitHub account.
2. Clone the forked repository
git clone https://github.com/your-username/numberify-converter.git
3. Install dependencies
Install all dependencies for development:
npm install
# or
yarn install
4. Make your changes
Create a new branch for your feature:
git checkout -b feature/new-language-support
5. Test your changes
Run the tests to ensure everything works as expected:
npm run test
# or
yarn test
6. Commit your changes
git commit -m "Add support for a new language"
7. Push and create a pull request
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.
Testing
To ensure the package works as expected, we use Jest for testing.
Running Tests:
npm run test
# or
yarn test
Tests are located in the tests/
directory, and you can add new test cases as needed.
Example Test (Jest):
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");
});
License
This project is licensed under the MIT License. See the LICENSE file for details.