Skip to content

Commit

Permalink
Merge d313ed6 into be8874d
Browse files Browse the repository at this point in the history
  • Loading branch information
mum-never-proud authored Nov 9, 2020
2 parents be8874d + d313ed6 commit 9edb2ca
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ Validator | Description
**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.
**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])** | 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', 'az-AZ', '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', 'fr-FR', 'fa', 'fa-AF', 'fa-IR', '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', 'th-TH', 'tr-TR', 'uk-UA', 'vi-VN']`) and defaults to `en-US`. Locale list is `validator.isAlphaLocales`.
**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', 'az-AZ', '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', 'fr-FR', 'fa', 'fa-AF', 'fa-IR', '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', 'vi-VN']`) and defaults to `en-US`. Locale list is `validator.isAlphanumericLocales`.
**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', 'fr-FR', 'fa-IR', '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', 'fr-FR', 'fa-IR', '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`.
**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/isAlpha.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import assertString from './util/assertString';
import { alpha } from './alpha';

export default function isAlpha(str, locale = 'en-US') {
assertString(str);
export default function isAlpha(_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 alpha) {
return alpha[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 @@ -925,6 +925,45 @@ describe('Validators', () => {
});
});

it('should validate alpha string with ignored characters', () => {
test({
validator: 'isAlpha',
args: ['en-US', { ignore: '- /' }], // ignore [space-/]
valid: [
'en-US',
'this is a valid alpha string',
'us/usa',
],
invalid: [
'1. this is not a valid alpha string',
'this$is also not a valid.alpha string',
'this is also not a valid alpha string.',
],
});

test({
validator: 'isAlpha',
args: ['en-US', { ignore: /[\s/-]/g }], // ignore [space -]
valid: [
'en-US',
'this is a valid alpha string',
],
invalid: [
'1. this is not a valid alpha string',
'this$is also not a valid.alpha string',
'this is also not a valid alpha string.',
],
});

test({
validator: 'isAlpha',
args: ['en-US', { ignore: 1234 }], // invalid ignore matcher
error: [
'alpha',
],
});
});

it('should validate Azerbaijani alpha strings', () => {
test({
validator: 'isAlpha',
Expand Down
19 changes: 16 additions & 3 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function _unsupportedIterableToArray(o, minLen) {
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === "Object" && o.constructor) n = o.constructor.name;
if (n === "Map" || n === "Set") return Array.from(o);
if (n === "Map" || n === "Set") return Array.from(n);
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
}

Expand Down Expand Up @@ -1056,9 +1056,22 @@ function isLocale(str) {
return localeReg.test(str);
}

function isAlpha(str) {
function isAlpha(_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 alpha) {
return alpha[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 9edb2ca

Please sign in to comment.