* Take an arabic number as a string and return a javascript number. * By default, this function does not try to convert the arabic decimal and thousand separator characters. * This returns `NaN` is the conversion is not possible. * Based on http://stackoverflow.com/a/17025392/2834898
(arabicNumbers, returnANumber = true, parseDecimalCharacter = false, parseThousandSeparator = false)
| 1016 | * @returns {string|number|NaN} |
| 1017 | */ |
| 1018 | static arabicToLatinNumbers(arabicNumbers, returnANumber = true, parseDecimalCharacter = false, parseThousandSeparator = false) { |
| 1019 | if (this.isNull(arabicNumbers)) { |
| 1020 | return arabicNumbers; |
| 1021 | } |
| 1022 | |
| 1023 | let result = arabicNumbers.toString(); |
| 1024 | if (result === '') { |
| 1025 | return arabicNumbers; |
| 1026 | } |
| 1027 | |
| 1028 | if (result.match(/[٠١٢٣٤٥٦٧٨٩۴۵۶]/g) === null) { |
| 1029 | // If no Arabic/Persian numbers are found, return the numeric string or number directly |
| 1030 | if (returnANumber) { |
| 1031 | result = Number(result); |
| 1032 | } |
| 1033 | |
| 1034 | return result; |
| 1035 | } |
| 1036 | |
| 1037 | if (parseDecimalCharacter) { |
| 1038 | result = result.replace(/٫/, '.'); // Decimal character |
| 1039 | } |
| 1040 | |
| 1041 | if (parseThousandSeparator) { |
| 1042 | result = result.replace(/٬/g, ''); // Thousand separator |
| 1043 | } |
| 1044 | |
| 1045 | // Replace the numbers only |
| 1046 | result = result.replace(/[٠١٢٣٤٥٦٧٨٩]/g, d => d.charCodeAt(0) - 1632) // Arabic numbers |
| 1047 | .replace(/[۰۱۲۳۴۵۶۷۸۹]/g, d => d.charCodeAt(0) - 1776); // Persian numbers |
| 1048 | |
| 1049 | // `NaN` has precedence over the string `'NaN'` |
| 1050 | const resultAsNumber = Number(result); |
| 1051 | if (isNaN(resultAsNumber)) { |
| 1052 | return resultAsNumber; |
| 1053 | } |
| 1054 | |
| 1055 | if (returnANumber) { |
| 1056 | result = resultAsNumber; |
| 1057 | } |
| 1058 | |
| 1059 | return result; |
| 1060 | } |
| 1061 | |
| 1062 | /** |
| 1063 | * Create a custom event and immediately sent it from the given element. |
no test coverage detected