Skip to content

Commit 58cc918

Browse files
author
rak810
committed
feat: added support for Bengali language
1 parent 907bb07 commit 58cc918

File tree

4 files changed

+56
-9
lines changed

4 files changed

+56
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ yarn.lock
1010
/index.js
1111
validator.js
1212
validator.min.js
13+
.replit

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ Validator | Description
8585
**contains(str, seed [, options ])** | check if the string contains the seed.<br/><br/>`options` is an object that defaults to `{ ignoreCase: false}`.<br/>`ignoreCase` specified whether the case of the substring be same or not.
8686
**equals(str, comparison)** | check if the string matches the comparison.
8787
**isAfter(str [, date])** | check if the string is a date that's after the specified date (defaults to now).
88-
**isAlpha(str [, locale, options])** | check if the string contains only letters (a-zA-Z).<br/><br/>Locale is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fa-IR', 'fr-CA', 'fr-FR', 'he', 'hu-HU', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphaLocales`. options is an optional object that can be supplied with the following key(s): ignore which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s.
89-
**isAlphanumeric(str [, locale, options])** | check if the string contains only letters and numbers (a-zA-Z0-9).<br/><br/>Locale is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fa-IR', 'fr-CA', 'fr-FR', 'he', 'hu-HU', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphanumericLocales`. options is an optional object that can be supplied with the following key(s): ignore which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s.
88+
**isAlpha(str [, locale, options])** | check if the string contains only letters (a-zA-Z).<br/><br/>Locale is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'bn-BD', 'bn-IN', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fa-IR', 'fr-CA', 'fr-FR', 'he', 'hu-HU', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphaLocales`. options is an optional object that can be supplied with the following key(s): ignore which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s.
89+
**isAlphanumeric(str [, locale, options])** | check if the string contains only letters and numbers (a-zA-Z0-9).<br/><br/>Locale is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG','bn-BD', 'bn-IN', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fa-IR', 'fr-CA', 'fr-FR', 'he', 'hu-HU', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphanumericLocales`. options is an optional object that can be supplied with the following key(s): ignore which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s.
9090
**isAscii(str)** | check if the string contains ASCII chars only.
9191
**isBase32(str)** | check if a string is base32 encoded.
9292
**isBase58(str)** | check if a string is base58 encoded.

src/lib/alpha.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export const alpha = {
3030
ar: /^[ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوىيًٌٍَُِّْٰ]+$/,
3131
he: /^[א-ת]+$/,
3232
fa: /^['آاءأؤئبپتثجچحخدذرزژسشصضطظعغفقکگلمنوهةی']+$/i,
33+
bn: /^['ি']+$/,
3334
};
3435

3536
export const alphanumeric = {
@@ -63,6 +64,7 @@ export const alphanumeric = {
6364
ar: /^[٠١٢٣٤٥٦٧٨٩0-9ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوىيًٌٍَُِّْٰ]+$/,
6465
he: /^[0-9א-ת]+$/,
6566
fa: /^['0-9آاءأؤئبپتثجچحخدذرزژسشصضطظعغفقکگلمنوهةی۱۲۳۴۵۶۷۸۹۰']+$/i,
67+
bn: /^['ি']+$/,
6668
};
6769

6870
export const decimal = {
@@ -81,10 +83,8 @@ for (let locale, i = 0; i < englishLocales.length; i++) {
8183
}
8284

8385
// Source: http://www.localeplanet.com/java/
84-
export const arabicLocales = [
85-
'AE', 'BH', 'DZ', 'EG', 'IQ', 'JO', 'KW', 'LB', 'LY',
86-
'MA', 'QM', 'QA', 'SA', 'SD', 'SY', 'TN', 'YE',
87-
];
86+
export const arabicLocales = ['AE', 'BH', 'DZ', 'EG', 'IQ', 'JO', 'KW', 'LB', 'LY',
87+
'MA', 'QM', 'QA', 'SA', 'SD', 'SY', 'TN', 'YE'];
8888

8989
for (let locale, i = 0; i < arabicLocales.length; i++) {
9090
locale = `ar-${arabicLocales[i]}`;
@@ -93,16 +93,23 @@ for (let locale, i = 0; i < arabicLocales.length; i++) {
9393
decimal[locale] = decimal.ar;
9494
}
9595

96-
export const farsiLocales = [
97-
'IR', 'AF',
98-
];
96+
export const farsiLocales = ['IR', 'AF'];
9997

10098
for (let locale, i = 0; i < farsiLocales.length; i++) {
10199
locale = `fa-${farsiLocales[i]}`;
102100
alphanumeric[locale] = alphanumeric.fa;
103101
decimal[locale] = decimal.ar;
104102
}
105103

104+
export const bengaliLocales = ['BD', 'IN'];
105+
106+
for (let locale, i = 0; i < bengaliLocales.length; i++) {
107+
locale = `bn-${bengaliLocales[i]}`;
108+
alpha[locale] = alpha.bn;
109+
alphanumeric[locale] = alphanumeric.bn;
110+
decimal[locale] = decimal['en-US'];
111+
}
112+
106113
// Source: https://en.wikipedia.org/wiki/Decimal_mark
107114
export const dotDecimal = ['ar-EG', 'ar-LB', 'ar-LY'];
108115
export const commaDecimal = [

test/validators.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,26 @@ describe('Validators', () => {
11721172
});
11731173
});
11741174

1175+
it('should validate Bengali alpha strings', () => {
1176+
test({
1177+
validator: 'isAlpha',
1178+
args: ['bn-BD'],
1179+
valid: [
1180+
'অয়াওর',
1181+
'ফগফদ্রত',
1182+
'ফদ্ম্যতভ',
1183+
'বেরেওভচনভন',
1184+
'আমারবাসগা',
1185+
],
1186+
invalid: [
1187+
'দাস২৩৪',
1188+
' দ্গফহ্নভ ',
1189+
'',
1190+
'(গফদ)',
1191+
],
1192+
});
1193+
});
1194+
11751195
it('should validate czech alpha strings', () => {
11761196
test({
11771197
validator: 'isAlpha',
@@ -1786,6 +1806,25 @@ describe('Validators', () => {
17861806
});
17871807
});
17881808

1809+
it('should validate Bengali alphanumeric strings', () => {
1810+
test({
1811+
validator: 'isAlphanumeric',
1812+
args: ['bn-BD'],
1813+
valid: [
1814+
'দ্গজ্ঞহ্রত্য১২৩',
1815+
'দ্গগফ৮৯০',
1816+
'চব৩৬৫ভবচ',
1817+
'১২৩৪',
1818+
'৩৪২৩৪দফজ্ঞদফ',
1819+
],
1820+
invalid: [
1821+
' ',
1822+
'১২৩ ',
1823+
'hel৩২0',
1824+
],
1825+
});
1826+
});
1827+
17891828
it('should validate czech alphanumeric strings', () => {
17901829
test({
17911830
validator: 'isAlphanumeric',

0 commit comments

Comments
 (0)