-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy patharabicString.js
executable file
·162 lines (132 loc) · 2.86 KB
/
arabicString.js
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* The percentage of Arabic letters in the `String`.
*
* Example:
*
* 'foobar'.howArabic()
* //=> 0.0
*
* 'فوو bar'.howArabic()
* //=> 0.5
*
* 'فوبار'.howArabic()
* //=> 1.0
*
*
* @returns {Float}
* @api public
*/
String.prototype.howArabic = function () {
var result, match, str = this
// strip punctuation, digits and spaces
str = str.replace(/[\u0021-\u0040\s]/gm, '')
match = str.match(/[\u0621-\u0652]/gm) || []
result = match.length / str.length
return result;
}
/**
* The percentage of non-Arabic letters in the `String`.
*
* Example:
*
* 'فوبار'.howNotArabic()
* //=> 0.0
*
* 'فوو bar'.howNotArabic()
* //=> 0.5
*
* 'foobar'.howNotArabic()
* //=> 1.0
*
*
* @returns {Float}
*/
String.prototype.howNotArabic = function () {
var result, match, str = this
// strip punctuation, digits and spaces
str = str.replace(/[\u0021-\u0040\s]/gm, '')
match = str.match(/[^\u0621-\u0652]/gm) || []
result = match.length / str.length
return result;
}
/**
* Is the `String` Arabic, based on
* a given `threshold` between `0` and `1`. Defaults to `0.79`.
*
* Example:
*
* 'فوو'.isArabic()
* //=> true
* 'فوو bar baz'.isArabic(0.5)
* //=> flase
*
* @param {Float} [threshold=0.79]
* @returns {Boolean}
*/
String.prototype.isArabic = function (threshold) {
threshold = threshold || 0.79
return this.howArabic() >= threshold;
}
/**
* Does the `String` have _any_ Arabic letter.
*
* Example:
*
* 'فوو bar'.hasArabic()
* //=> ture
* 'foo bar'.hasArabic()
* //=> false
*
*
* @returns {Boolean}
*/
String.prototype.hasArabic = function () {
return /[\u0621-\u064A]/.test(this);
}
/**
* Remove the Arabic tashkil -diacritics- from the 'String'.
*
* Example
*
* 'مٌحمْد'.removeTashkel()
* //=> 'محمد'
*
* 'وَتُرى الْكَوَاكِبِ فِي الْمَجَرَّةِ شَرَعَا*** مِثْلُ الظِّباءِ كوارعا فِي جَدْوَلِ'.removeTashkel()
* //=> 'وترى الكواكب في المجرة شرعا *** مثل الظباء كوارعا في جدول'
*
*
* @returns {String}
*/
String.prototype.removeTashkel = function () {
return this.replace(/[\u064B-\u0652]/gm, '');
}
/**
* Remove non-Arabic letters
*
* Example
*
* 'hello مرحبا'.removeNonArabic()
* //=> 'مرحبا'
*
*
*
* @returns {String}
*/
String.prototype.removeNonArabic = function () {
return this.replace(/[^\u0621-\u0652]/gm, '');
}
/**
* Remove Arabic letters
*
* Example
*
* 'hello مرحبا'.removeTashkel()
* //=> 'hello'
*
*
*
* @returns {String}
*/
String.prototype.removeArabic = function () {
return this.replace(/[\u0621-\u0652]/gm, '');
}