Skip to content

Commit 35b5ffb

Browse files
authored
Merge pull request #1678 from rak810/master
feat(isAlpha, isAlphanumeric): Added support for Bengali language
2 parents 3873ebf + c7c0760 commit 35b5ffb

File tree

4 files changed

+60
-11
lines changed

4 files changed

+60
-11
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+

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# validator.js
2-
2+
# Changes made - 1707074
33
[![NPM version][npm-image]][npm-url]
44
[![CI][ci-image]][ci-url]
55
[![Coverage][codecov-image]][codecov-url]
@@ -92,8 +92,8 @@ Validator | Description
9292
**contains(str, seed [, options ])** | check if the string contains the seed.<br/><br/>`options` is an object that defaults to `{ ignoreCase: false, minOccurrences: 1 }`.<br />Options: <br/> `ignoreCase`: Ignore case when doing comparison, default false<br/>`minOccurences`: Minimum number of occurrences for the seed in the string. Defaults to 1.
9393
**equals(str, comparison)** | check if the string matches the comparison.
9494
**isAfter(str [, date])** | check if the string is a date that's after the specified date (defaults to now).
95-
**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', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', '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.
96-
**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', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', '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.
95+
**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', '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', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', '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.
96+
**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', 'bn', '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', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', '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.
9797
**isAscii(str)** | check if the string contains ASCII chars only.
9898
**isBase32(str [, options])** | check if a string is base32 encoded. `options` is optional and defaults to `{crockford: false}`.<br/> When `crockford` is true it tests the given base32 encoded string using [Crockford's base32 alternative](http://www.crockford.com/base32.html).
9999
**isBase58(str)** | check if a string is base58 encoded.
@@ -137,7 +137,9 @@ Validator | Description
137137
**isISO4217(str)** | check if the string is a valid [ISO 4217](https://en.wikipedia.org/wiki/ISO_4217) officially assigned currency code.
138138
**isISRC(str)** | check if the string is a [ISRC](https://en.wikipedia.org/wiki/International_Standard_Recording_Code).
139139
**isISSN(str [, options])** | check if the string is an [ISSN](https://en.wikipedia.org/wiki/International_Standard_Serial_Number).<br/><br/>`options` is an object which defaults to `{ case_sensitive: false, require_hyphen: false }`. If `case_sensitive` is true, ISSNs with a lowercase `'x'` as the check digit are rejected.
140-
**isJSON(str [, options])** | check if the string is valid JSON (note: uses JSON.parse).<br/><br/>`options` is an object which defaults to `{ allow_primitives: false }`. If `allow_primitives` is true, the primitives 'true', 'false' and 'null' are accepted as valid JSON values.
140+
**isJSON(str [, options])** | check if the string is valid JSON (note: uses JSON.parse).<br/><br/>`options` is an object which defaults to `{ allow_primitives: false }`. If `allow_primitives` is true, the primitives 'true', 'false' and '
141+
142+
null' are accepted as valid JSON values.
141143
**isJWT(str)** | check if the string is valid JWT token.
142144
**isLatLong(str [, options])** | check if the string is a valid latitude-longitude coordinate in the format `lat,long` or `lat, long`.<br/><br/>`options` is an object that defaults to `{ checkDMS: false }`. Pass `checkDMS` as `true` to validate DMS(degrees, minutes, and seconds) latitude-longitude format.
143145
**isLength(str [, options])** | check if the string's length falls in a range.<br/><br/>`options` is an object which defaults to `{min:0, max: undefined}`. Note: this function takes into account surrogate pairs.

src/lib/alpha.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const alpha = {
3131
ar: /^[ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوىيًٌٍَُِّْٰ]+$/,
3232
he: /^[א-ת]+$/,
3333
fa: /^['آاءأؤئبپتثجچحخدذرزژسشصضطظعغفقکگلمنوهةی']+$/i,
34+
bn: /^['ি']+$/,
3435
'hi-IN': /^[\u0900-\u0961]+[\u0972-\u097F]*$/i,
3536
'si-LK': /^[\u0D80-\u0DFF]+$/,
3637
};
@@ -67,6 +68,7 @@ export const alphanumeric = {
6768
ar: /^[٠١٢٣٤٥٦٧٨٩0-9ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوىيًٌٍَُِّْٰ]+$/,
6869
he: /^[0-9א-ת]+$/,
6970
fa: /^['0-9آاءأؤئبپتثجچحخدذرزژسشصضطظعغفقکگلمنوهةی۱۲۳۴۵۶۷۸۹۰']+$/i,
71+
bn: /^['ি']+$/,
7072
'hi-IN': /^[\u0900-\u0963]+[\u0966-\u097F]*$/i,
7173
'si-LK': /^[0-9\u0D80-\u0DFF]+$/,
7274
};
@@ -87,10 +89,8 @@ for (let locale, i = 0; i < englishLocales.length; i++) {
8789
}
8890

8991
// Source: http://www.localeplanet.com/java/
90-
export const arabicLocales = [
91-
'AE', 'BH', 'DZ', 'EG', 'IQ', 'JO', 'KW', 'LB', 'LY',
92-
'MA', 'QM', 'QA', 'SA', 'SD', 'SY', 'TN', 'YE',
93-
];
92+
export const arabicLocales = ['AE', 'BH', 'DZ', 'EG', 'IQ', 'JO', 'KW', 'LB', 'LY',
93+
'MA', 'QM', 'QA', 'SA', 'SD', 'SY', 'TN', 'YE'];
9494

9595
for (let locale, i = 0; i < arabicLocales.length; i++) {
9696
locale = `ar-${arabicLocales[i]}`;
@@ -99,16 +99,23 @@ for (let locale, i = 0; i < arabicLocales.length; i++) {
9999
decimal[locale] = decimal.ar;
100100
}
101101

102-
export const farsiLocales = [
103-
'IR', 'AF',
104-
];
102+
export const farsiLocales = ['IR', 'AF'];
105103

106104
for (let locale, i = 0; i < farsiLocales.length; i++) {
107105
locale = `fa-${farsiLocales[i]}`;
108106
alphanumeric[locale] = alphanumeric.fa;
109107
decimal[locale] = decimal.ar;
110108
}
111109

110+
export const bengaliLocales = ['BD', 'IN'];
111+
112+
for (let locale, i = 0; i < bengaliLocales.length; i++) {
113+
locale = `bn-${bengaliLocales[i]}`;
114+
alpha[locale] = alpha.bn;
115+
alphanumeric[locale] = alphanumeric.bn;
116+
decimal[locale] = decimal['en-US'];
117+
}
118+
112119
// Source: https://en.wikipedia.org/wiki/Decimal_mark
113120
export const dotDecimal = ['ar-EG', 'ar-LB', 'ar-LY'];
114121
export const commaDecimal = [

test/validators.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,6 +1424,26 @@ describe('Validators', () => {
14241424
});
14251425
});
14261426

1427+
it('should validate Bengali alpha strings', () => {
1428+
test({
1429+
validator: 'isAlpha',
1430+
args: ['bn-BD'],
1431+
valid: [
1432+
'অয়াওর',
1433+
'ফগফদ্রত',
1434+
'ফদ্ম্যতভ',
1435+
'বেরেওভচনভন',
1436+
'আমারবাসগা',
1437+
],
1438+
invalid: [
1439+
'দাস২৩৪',
1440+
' দ্গফহ্নভ ',
1441+
'',
1442+
'(গফদ)',
1443+
],
1444+
});
1445+
});
1446+
14271447
it('should validate czech alpha strings', () => {
14281448
test({
14291449
validator: 'isAlpha',
@@ -2095,6 +2115,25 @@ describe('Validators', () => {
20952115
});
20962116
});
20972117

2118+
it('should validate Bengali alphanumeric strings', () => {
2119+
test({
2120+
validator: 'isAlphanumeric',
2121+
args: ['bn-BD'],
2122+
valid: [
2123+
'দ্গজ্ঞহ্রত্য১২৩',
2124+
'দ্গগফ৮৯০',
2125+
'চব৩৬৫ভবচ',
2126+
'১২৩৪',
2127+
'৩৪২৩৪দফজ্ঞদফ',
2128+
],
2129+
invalid: [
2130+
' ',
2131+
'১২৩ ',
2132+
'hel৩২0',
2133+
],
2134+
});
2135+
});
2136+
20982137
it('should validate czech alphanumeric strings', () => {
20992138
test({
21002139
validator: 'isAlphanumeric',

0 commit comments

Comments
 (0)