@nexim/localizer
Version:
Lightweight i18n utilities to handle translations, number formatting, date/time localization using native browser APIs.
26 lines • 806 B
JavaScript
const languageStartCodes = {
en: 48,
ar: 1632,
fa: 1776,
};
const SEARCH_REGEX = /[0-9٠-٩۰-۹]/g;
/**
* Converts numbers between different Unicode digit representations
*/
export function convertDigits(str, toLanguage) {
if (!str || str.trim() === '')
return str;
const toLangZeroCode = languageStartCodes[toLanguage];
return str.replace(SEARCH_REGEX, (char) => {
const code = char.charCodeAt(0);
let digit = 0;
if (code >= 48 && code <= 57)
digit = code - 48;
else if (code >= 1632 && code <= 1641)
digit = code - 1632;
else if (code >= 1776 && code <= 1785)
digit = code - 1776;
return String.fromCharCode(toLangZeroCode + digit);
});
}
//# sourceMappingURL=unicode-digits.js.map