Skip to content

Commit

Permalink
chore(core): optimise function implementation
Browse files Browse the repository at this point in the history
Also, add backup approaches
  • Loading branch information
SMAKSS committed Dec 31, 2023
1 parent e3b95af commit 082232d
Show file tree
Hide file tree
Showing 6 changed files with 383 additions and 412 deletions.
3 changes: 1 addition & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
],
"react-hooks/exhaustive-deps": "off",
// Enforce the use of the shorthand syntax.
"object-shorthand": "error",
"no-console": "warn"
"object-shorthand": "error"
}
}
34 changes: 17 additions & 17 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

![npm](https://img.shields.io/npm/v/@smakss/convert-numbers) ![NPM](https://img.shields.io/npm/l/@smakss/convert-numbers) ![npm](https://img.shields.io/npm/dt/@smakss/convert-numbers) ![npm bundle size (scoped)](https://img.shields.io/bundlephobia/min/@smakss/convert-numbers)

Convert Numbers is a utility package that helps ensure consistency in data transfer by converting Arabic and Persian numerals to English numerals. This is particularly useful when user inputs can come from different keyboard settings, and there's a need to standardize all digits to English format before sending data to a database or processing it further.
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

You can check the working demo on CodeSandbox:
Check out the working demo on CodeSandbox:

[![View @smakss/convert-numbers](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/smakss-convert-numbers-bstmfj?fontsize=14&hidenavigation=1&theme=dark)

## Installation

To install the package, you can use npm or yarn with the following commands:
Install the package using npm or yarn:

```bash
npm i @smakss/convert-numbers
Expand All @@ -22,41 +22,41 @@ yarn add @smakss/convert-numbers

## Usage

Import the `convertNumbers` function into your project using CommonJS or ECMAScript modules:

CommonJS:

```js
const ConvertNumbers = require('@smakss/convert-numbers');
```
Import `convertNumbers` into your project as a CommonJS or ECMAScript module:

ECMAScript Modules:

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

Then, use the function as follows:
Then, use the function like this:

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

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

// Returns "" for no input
// 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 function usage, please refer to the JSDoc comments within the code.
For more detailed examples and information about the function usage, refer to the JSDoc comments in the source code.

## Contributing

Contributions to the project are welcome! Please refer to [CONTRIBUTING.md](./CONTRIBUTING.md) for contribution guidelines.
We welcome contributions! For guidelines on how to contribute, please check [CONTRIBUTING.md](./CONTRIBUTING.md).

## Code of Conduct

To maintain a welcoming and positive community, please see our [Code of Conduct](./CODE_OF_CONDUCT.md).
We are committed to fostering a welcoming and positive community. Please see our [Code of Conduct](./CODE_OF_CONDUCT.md) for more information.
26 changes: 12 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,25 @@
},
"description": "Convert Numbers transforms Arabic and Persian numerals to English, ensuring data uniformity in multilingual environments. Ideal for user input standardization and database consistency.",
"devDependencies": {
"@commitlint/cli": "^18.2.0",
"@commitlint/config-conventional": "^18.1.0",
"@rollup/plugin-commonjs": "^25.0.7",
"@commitlint/cli": "^18.4.3",
"@commitlint/config-conventional": "^18.4.3",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-typescript": "^11.1.5",
"@typescript-eslint/eslint-plugin": "^6.9.1",
"@typescript-eslint/parser": "^6.9.1",
"eslint": "^8.53.0",
"@typescript-eslint/eslint-plugin": "^6.16.0",
"@typescript-eslint/parser": "^6.16.0",
"eslint": "^8.56.0",
"husky": "^8.0.3",
"lint-staged": "^15.0.2",
"prettier": "^3.0.3",
"rollup": "^4.3.0",
"lint-staged": "^15.2.0",
"prettier": "^3.1.1",
"rollup": "^4.9.1",
"tslib": "^2.6.2",
"typescript": "^5.2.2"
"typescript": "^5.3.3"
},
"engines": {
"node": ">=18.0.0"
},
"exports": {
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js"
".": "./dist/index.js"
},
"files": [
"dist"
Expand Down Expand Up @@ -82,6 +80,6 @@
"typecheck": "tsc -b ."
},
"type": "module",
"types": "lib",
"version": "2.0.5"
"types": "./dist/index.d.ts",
"version": "2.1.0-beta.0"
}
13 changes: 3 additions & 10 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import typescript from '@rollup/plugin-typescript';
import packageJson from './package.json' assert { type: 'json' };

Expand All @@ -8,19 +7,13 @@ export default [
input: 'src/index.ts',
output: [
{
file: packageJson.main,
format: 'cjs',
exports: 'named',
sourcemap: true
},
{
file: packageJson.module,
format: 'esm',
file: 'dist/index.js',
format: 'es',
exports: 'named',
sourcemap: true
}
],
plugins: [resolve(), commonjs(), typescript()],
plugins: [resolve(), typescript()],
external: [
...Object.keys(packageJson.devDependencies || {}),
...Object.keys(packageJson.peerDependencies || {})
Expand Down
66 changes: 53 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
// Threshold for the number of characters in the input string
const THRESHOLD = 100;

// Define the type for digitMap
const DIGIT_MAP: Record<string, string> = {
'٠': '0',
'١': '1',
'٢': '2',
'٣': '3',
'٤': '4',
'٥': '5',
'٦': '6',
'٧': '7',
'٨': '8',
'٩': '9', // Arabic-Indic
'۰': '0',
'۱': '1',
'۲': '2',
'۳': '3',
'۴': '4',
'۵': '5',
'۶': '6',
'۷': '7',
'۸': '8',
'۹': '9' // Persian
};

/**
* Converts Persian or Arabic numbers to English numbers.
*
* @param {string} [inputValue] - The string containing Persian or Arabic numbers to convert.
* @returns {string} - The string with Persian or Arabic numbers converted to English, or "" if input is not provided.
* @param {string} inputValue - The string containing Persian or Arabic numbers to convert.
* @returns {string} - The string with Persian or Arabic numbers converted to English, or the original string if an error occurs.
*
* @example
* // returns "123"
Expand All @@ -13,23 +40,36 @@
* convertNumbers("۴۵۶");
*
* @example
* // returns ""
* convertNumbers();
*
* @example
* // returns "789"
* convertNumbers("٧٨٩");
*/
function convertNumbers(inputValue: string): string {
if (!inputValue) return '';

return inputValue
.replace(/[\u0660-\u0669]/g, (c) => {
return String.fromCharCode(c.charCodeAt(0) - 0x0660 + 0x0030);
})
.replace(/[\u06F0-\u06F9]/g, (c) => {
return String.fromCharCode(c.charCodeAt(0) - 0x06f0 + 0x0030);
});
try {
if (inputValue.length <= THRESHOLD) {
return inputValue
.replace(/[\u0660-\u0669]/g, (c) =>
String.fromCharCode(c.charCodeAt(0) - 0x0660 + 0x0030)
)
.replace(/[\u06F0-\u06F9]/g, (c) =>
String.fromCharCode(c.charCodeAt(0) - 0x06f0 + 0x0030)
);
} else {
return inputValue
.split('')
.map((char) => DIGIT_MAP[char] ?? char)
.join('');
}
} catch (error) {
console.error('Error in convertNumbers function:', error);

let result = '';
for (const char of inputValue) {
result += DIGIT_MAP[char] || char;
}
return result;
}
}

export default convertNumbers;
Loading

0 comments on commit 082232d

Please sign in to comment.