Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isAlpha enhancement #1286

Merged
merged 11 commits into from
Nov 15, 2020
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Validator | Description
***contains(str, seed)*** | check if the string contains the seed.
**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', '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`.
**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.
Expand Down
17 changes: 15 additions & 2 deletions es/lib/isAlpha.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import assertString from './util/assertString';
import { alpha } from './alpha';
export default function isAlpha(str) {
export default 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
17 changes: 15 additions & 2 deletions lib/isAlpha.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,22 @@ var _alpha = require("./alpha");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function isAlpha(str) {
function isAlpha(_str) {
var locale = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'en-US';
(0, _assertString.default)(str);
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
(0, _assertString.default)(_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.alpha) {
return _alpha.alpha[locale].test(str);
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 = {}) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am just curious why change str to _str and later do

let str = _str

I thought we could pass str as before and do:

assertString(str);

But, all in all, it looks good to me.

Copy link
Contributor Author

@mum-never-proud mum-never-proud Sep 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ezkemboi ah it's not good practice to directly change the arguments, just following best practices, will resolve the conflict in sometime

assertString(_str);

let str = _str;
const { ignore } = options;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am with the opinion that ignore characters should be optional.
This actually fails if a user does not pass ignore values (It is not guaranteed that a user needs to have some values ignored).
Also, if I pass options as args: [{ ignore: /[\s/-]/g }], it will fail or I do pass an args: ['en-US', { }] it also fails.
I am open to discussion on way forward.

Cc. @profnandaa

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm technically i don't see a possibility of failing for any of the above args, may be i can add those

const { ignore } = options;
if (ignore) {

the above line says that ignore is optional which means, args: ['en-US', { }] should pass

as far as about this [{ ignore: /[\s/-]/g }] we are already asserting first argument to be string

let me know if it makes sense @profnandaa @ezkemboi


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 @@ -868,6 +868,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 bulgarian 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 @@ -82,7 +82,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 @@ -983,9 +983,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.