Skip to content

NPM package that converts Arabic and Persian numbers to English equivalent to avoid data inconsistency.

License

Notifications You must be signed in to change notification settings

SMAKSS/convert-numbers

Repository files navigation

Convert Numbers

npm NPM npm npm bundle size (scoped)

Convert Numbers is a utility package designed to ensure consistency in data transfer by converting Arabic and Persian numerals to English numerals. This tool is essential when user inputs might originate from different keyboard settings, requiring standardization of all digits to English format before further processing or database storage.

Demo

Check out the working demo on CodeSandbox:

View @smakss/convert-numbers

Installation

Install the package using npm or yarn:

npm i @smakss/convert-numbers
# or
yarn add @smakss/convert-numbers

Usage

Import convertNumbers into your project as a CommonJS or ECMAScript module:

ECMAScript Modules:

import ConvertNumbers from '@smakss/convert-numbers';

Then, use the function like this:

// Converts Persian numbers to '1234567890'
console.log(ConvertNumbers('۱۲۳۴۵۶۷۸۹۰'));

// Converts Arabic numbers to '1234567890'
console.log(ConvertNumbers('١٢٣٤٥٦٧٨٩٠'));

// Returns an empty string for no input
console.log(ConvertNumbers());

Features

  • Robust conversion: Utilizes a mapping approach for efficiency and readability, with a fallback to regular expressions for longer strings.
  • Error resilience: In case of unforeseen errors or compatibility issues, the function gracefully falls back to a basic for-of loop iteration.
  • Wide compatibility: Designed to work across a variety of JavaScript environments.

Documentation

For more detailed examples and information about the function usage, refer to the JSDoc comments in the source code.

Contributing

We welcome contributions! For guidelines on how to contribute, please check CONTRIBUTING.md.

Code of Conduct

We are committed to fostering a welcoming and positive community. Please see our Code of Conduct for more information.