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.
Check out the working demo on CodeSandbox:
Install the package using npm or yarn:
npm i @smakss/convert-numbers
# or
yarn add @smakss/convert-numbers
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());
- 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.
For more detailed examples and information about the function usage, refer to the JSDoc comments in the source code.
We welcome contributions! For guidelines on how to contribute, please check CONTRIBUTING.md.
We are committed to fostering a welcoming and positive community. Please see our Code of Conduct for more information.