Skip to content

Commit

Permalink
feat(isAlphanumeric): added options(optional parameter)
Browse files Browse the repository at this point in the history
  • Loading branch information
varsubham committed Jan 18, 2021
1 parent 069a8c3 commit d519b06
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Validator | Description
**equals(str, comparison)** | check if the string matches the comparison.
**isAfter(str [, date])** | check if the string is a date that's after the specified date (defaults to now).
**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.
**isAlphanumeric(str [, locale])** | check if the string contains only letters and numbers.<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`.
**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.
**isAscii(str)** | check if the string contains ASCII chars only.
**isBase32(str)** | check if a string is base32 encoded.
**isBase58(str)** | check if a string is base58 encoded.
Expand Down
18 changes: 16 additions & 2 deletions src/lib/isAlphanumeric.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import assertString from './util/assertString';
import { alphanumeric } from './alpha';

export default function isAlphanumeric(str, locale = 'en-US') {
assertString(str);
export default function isAlphanumeric(_str, locale = 'en-US', options = {}) {
assertString(_str);

let str = _str;
const { ignore } = options;

if (ignore) {
if (ignore instanceof RegExp) {
str = str.replace(ignore, '');
} else if (typeof ignore === 'string') {
str = str.replace(new RegExp(`[${ignore.replace(/[-[\]{}()*+?.,\\^$|#\\s]/g, '\\$&')}]`, 'g'), ''); // escape regex for ignore
} else {
throw new Error('ignore should be instance of a String or RegExp');
}
}

if (locale in alphanumeric) {
return alphanumeric[locale].test(str);
}
Expand Down
39 changes: 39 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -1571,6 +1571,45 @@ describe('Validators', () => {
});
});

it('should validate alphanumeric string with ignored characters', () => {
test({
validator: 'isAlphaNumeric',
args: ['en-US', { ignore: '@_- ' }], // ignore [@ space _ -]
valid: [
'Hello@123',
'this is a valid alphaNumeric string',
'En-US @ alpha_numeric',
],
invalid: [
'In*Valid',
'hello$123',
'{invalid}',
],
});

test({
validator: 'isAlphaNumeric',
args: ['en-US', { ignore: /[\s/-]/g }], // ignore [space -]
valid: [
'en-US',
'this is a valid alphaNumeric string',
],
invalid: [
'INVALID$ AlphaNum Str',
'hello@123',
'abc*123',
],
});

test({
validator: 'isAlphaNumeric',
args: ['en-US', { ignore: 1234 }], // invalid ignore matcher (ignore should be instance of a String or RegExp)
error: [
'alpha',
],
});
});

it('should validate defined english aliases', () => {
test({
validator: 'isAlphanumeric',
Expand Down
17 changes: 15 additions & 2 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1082,9 +1082,22 @@ function isAlpha(_str) {
}
var locales$1 = Object.keys(alpha);

function isAlphanumeric(str) {
function isAlphanumeric(_str) {
var locale = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'en-US';
assertString(str);
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
assertString(_str);
var str = _str;
var ignore = options.ignore;

if (ignore) {
if (ignore instanceof RegExp) {
str = str.replace(ignore, '');
} else if (typeof ignore === 'string') {
str = str.replace(new RegExp("[".concat(ignore.replace(/[-[\]{}()*+?.,\\^$|#\\s]/g, '\\$&'), "]"), 'g'), ''); // escape regex for ignore
} else {
throw new Error('ignore should be instance of a String or RegExp');
}
}

if (locale in alphanumeric) {
return alphanumeric[locale].test(str);
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.

0 comments on commit d519b06

Please sign in to comment.