-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
51 lines (48 loc) · 1.25 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const engishDigits = /[0-9]/g;
const arabicDigits = /[٠-٩]/g;
const notEngishDigits = /[^0-9]/g;
const notArabicDigits = /[^٠-٩]/g;
const gap = 1584; // charcode gap between 0 and ٠;
/**
*
* @param num number or a string that contain the number to be converted
* @param intOnly remove any character that is not a digit
*/
export const toArabic = (
num: number|string,
intOnly: boolean = false
): string => {
if (intOnly) {
num = String(num).replace(notEngishDigits, "")
}
return String(num)
.replace(
engishDigits,
(match:string) => String.fromCharCode( match.charCodeAt(0) + gap )
)
}
/**
*
* @param num a string that contain the number to be converted
* @param intOnly remove any character that is not a digit
*/
export const fromArabic = (
num: string,
intOnly:boolean = false
): string => {
if (intOnly) {
num = String(num).replace(notArabicDigits, "")
}
return String(num)
.replace(
arabicDigits,
(match:string) => String.fromCharCode( match.charCodeAt(0) - gap )
)
}
/**
*
* @param num a string that contain the number to be tested
*/
export const hasArabic = (num: string): RegExpMatchArray => {
return String(num).match(arabicDigits);
};