npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

numberify-converter

v1.0.2

Published

Convert numbers from text in various languages into their numeric equivalent.

Downloads

8

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

  1. Features
  2. Installation
  3. Usage
  4. API Documentation
  5. Supported Languages
  6. Error Handling
  7. Contributing
  8. Testing
  9. 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.