Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions es/lib/isAlpha.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,19 @@ import assertString from './util/assertString';
import { alpha } from './alpha';
export default function isAlpha(str) {
var locale = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'en-US';
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
assertString(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, "]"), 'g'), '');
} 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: 0 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,6 @@ var _isSlug = _interopRequireDefault(require("./lib/isSlug"));

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
Expand Down
12 changes: 12 additions & 0 deletions lib/isAlpha.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,19 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de

function isAlpha(str) {
var locale = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'en-US';
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
(0, _assertString.default)(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, "]"), 'g'), '');
} 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
15 changes: 14 additions & 1 deletion src/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, locale = 'en-US') {
export default function isAlpha(str, locale = 'en-US', options = {}) {
assertString(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}]`, 'g'), '');
} else {
throw new Error('ignore should be instance of a String or RegExp');
}
}

if (locale in alpha) {
return alpha[locale].test(str);
}
Expand Down
38 changes: 38 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,44 @@ 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',
],
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
12 changes: 12 additions & 0 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,19 @@ function isLocale(str) {

function isAlpha(str) {
var locale = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'en-US';
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
assertString(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, "]"), 'g'), '');
} 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.