Created
September 27, 2016 06:23
-
-
Save amirasaran/05f97748cd1d268dc3f6c85bdacb197b to your computer and use it in GitHub Desktop.
JavaScript Arabic character to Persian (تبدیل حروف عربی به فارسی)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String.prototype.replaceAll = function(search, replacement) { | |
var target = this; | |
return target.replace(new RegExp(search, 'g'), replacement); | |
}; | |
String.prototype.toPersianCharacter = function () { | |
var string = this; | |
var obj = { | |
'ك' :'ک', | |
'دِ': 'د', | |
'بِ': 'ب', | |
'زِ': 'ز', | |
'ذِ': 'ذ', | |
'شِ': 'ش', | |
'سِ': 'س', | |
'ى' :'ی', | |
'ي' :'ی', | |
'١' :'۱', | |
'٢' :'۲', | |
'٣' :'۳', | |
'٤' :'۴', | |
'٥' :'۵', | |
'٦' :'۶', | |
'٧' :'۷', | |
'٨' :'۸', | |
'٩' :'۹', | |
'٠' :'۰', | |
}; | |
Object.keys(obj).forEach(function(key) { | |
string = string.replaceAll(key, obj[key]); | |
}); | |
return string | |
}; | |
/** | |
* Example | |
*/ | |
var string = 'ك ٤ ٦٦'; | |
string = string.toPersianCharacter(); | |
console.log(string); //out put "ک ۴ ۶۶" |
عملکرد بالا با ES6 کوتاه تر است
added 'ئ'
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.replace(new RegExp(search, 'g'), replacement);
};
String.prototype.toPersianCharacter = function () {
var string = this;
var obj = {
'ك' :'ک',
'دِ': 'د',
'بِ': 'ب',
'زِ': 'ز',
'ذِ': 'ذ',
'شِ': 'ش',
'سِ': 'س',
'ى' :'ی',
'ي' :'ی',
'ئ' :'ی',
'١' :'۱',
'٢' :'۲',
'٣' :'۳',
'٤' :'۴',
'٥' :'۵',
'٦' :'۶',
'٧' :'۷',
'٨' :'۸',
'٩' :'۹',
'٠' :'۰',
};
Object.keys(obj).forEach(function(key) {
string = string.replaceAll(key, obj[key]);
});
return string
};
Thanks Amir
bellow snippet just a shorter
function convertArabicCharToPersian(str) {
const charsMapping = {
ك: "ک",
دِ: "د",
بِ: "ب",
زِ: "ز",
ذِ: "ذ",
شِ: "ش",
سِ: "س",
ى: "ی",
ي: "ی",
ئ: "ی",
"١": "۱",
"٢": "۲",
"٣": "۳",
"٤": "۴",
"٥": "۵",
"٦": "۶",
"٧": "۷",
"٨": "۸",
"٩": "۹",
"٠": "۰",
};
return Object.keys(charsMapping).reduce((prev, curr) => {
return prev.replaceAll(curr, charsMapping[curr]);
}, str || "");
}
Thanks Amir,
This is equivalent to code, in typescript:
export {};
declare global {
interface String {
replaceAll(
search: string,
replacement: string | ((substring: string, ...args: any[]) => string),
): string;
toPersianCharacter(): string;
toArabicCharacter(): string;
}
}
String.prototype.replaceAll = function (
search: string,
replacement: string | ((substring: string, ...args: any[]) => string),
): string {
return this.replace(new RegExp(search, 'g'), replacement);
};
String.prototype.toPersianCharacter = function (this: string) {
const obj: { [key: string]: string } = {
'ك': 'ک',
'دِ': 'د',
'بِ': 'ب',
'زِ': 'ز',
'ذِ': 'ذ',
'شِ': 'ش',
'سِ': 'س',
'ى': 'ی',
'ي': 'ی',
'١': '۱',
'٢': '۲',
'٣': '۳',
'٤': '۴',
'٥': '۵',
'٦': '۶',
'٧': '۷',
'٨': '۸',
'٩': '۹',
'٠': '۰',
};
let string = this;
Object.keys(obj).forEach(function (key) {
string = string.replaceAll(key, obj[key]);
});
return string;
};
String.prototype.toArabicCharacter = function (this: string) {
const obj: { [key: string]: string } = {
'ک': 'ك',
'د': 'دِ',
'ب': 'بِ',
'ز': 'زِ',
'ذ': 'ذِ',
'ش': 'شِ',
'س': 'سِ',
'ى': 'ی',
'ی': 'ي',
'۱': '١',
'۲': '٢',
'۳': '٣',
'۴': '٤',
'۵': '٥',
'۶': '٦',
'۷': '٧',
'۸': '٨',
'۹': '٩',
'۰': '٠',
};
let string = this;
Object.keys(obj).forEach(function (key) {
string = string.replaceAll(key, obj[key]);
});
return string;
};
Usage:
import './char-convertor.ts'
let text: string='میلاد';
const arabic = text.toArabicCharacter();
console.log(arabic);
Thanks
const arabicToPersian = str => {
if (!str) return str;
const diff = {
'ك': 'ک',
'دِ': 'د',
'بِ': 'ب',
'زِ': 'ز',
'ذِ': 'ذ',
'شِ': 'ش',
'سِ': 'س',
'ى': 'ی',
'ي': 'ی',
'ئ': 'ی',
'١': '۱',
'٢': '۲',
'٣': '۳',
'٤': '۴',
'٥': '۵',
'٦': '۶',
'٧': '۷',
'٨': '۸',
'٩': '۹',
'٠': '۰',
};
for (const [key, value] of Object.entries(diff)) {
str = str.replaceAll(key, value);
}
return str;
};
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
const arabicToPersian = str => {
const diff = {
"ة": "ه",
"ك": "ک",
"دِ": "د",
"بِ": "ب",
"زِ": "ز",
"ذِ": "ذ",
"شِ": "ش",
"سِ": "س",
"ى": "ی",
"ي": "ی",
"٠": "۰",
"١": "۱",
"٢": "۲",
"٣": "۳",
"٤": "۴",
"٥": "۵",
"٦": "۶",
"٧": "۷",
"٨": "۸",
"٩": "۹",
}
for (const [key, value] of Object.entries(diff)) {
str.replaceAll(key, value)
}
return str
}