Skip to content

Instantly share code, notes, and snippets.

@a-m-dev
Created April 9, 2019 11:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a-m-dev/c36e3cd812a25e57e96e52a59dd7fa07 to your computer and use it in GitHub Desktop.
Save a-m-dev/c36e3cd812a25e57e96e52a59dd7fa07 to your computer and use it in GitHub Desktop.
convert() {
var numerals = {
persian : ["۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"],
arabic : ["٠", "١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩"]
};
function fromEnglish(str, lang){
var i, len = str.length, result = "";
for( i = 0; i < len; i++ )
result += numerals[lang][str[i]];
return result;
}
return {
toNormal : function(str){
var num, i, len = str.length, result = "";
for( i = 0; i < len; i++ ){
num = numerals["persian"].indexOf(str[i]);
num = num != -1 ? num : numerals["arabic"].indexOf(str[i]);
if( num == -1 ) num = str[i];
result += num;
}
return result;
},
toPersian : function(str, lang){
return fromEnglish(str, "persian");
},
toArabic : function(str){
return fromEnglish(str, "arabic");
}
}
}
@a-m-dev
Copy link
Author

a-m-dev commented Apr 9, 2019

call it like this.convert().toNormal(12312312۱۲۳۱۲۳۱۲۳2314123۲۳۴۲۳۴)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment