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

feat(isAlpha): allow usage of options object #2086

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
5578dad
refactor: allow for splitting tests to different files
WikiRik Oct 18, 2022
afda0bd
feat(isAfter): allow usage of options object
WikiRik Oct 18, 2022
cad192e
style: make options italic
WikiRik Oct 18, 2022
5ba1a43
refactor: rename test file extension to .test.js
WikiRik Oct 19, 2022
71102d4
refactor: rename test-functions to testFunctions
WikiRik Oct 19, 2022
f32d228
refactor: implement suggestion from #2019 review
WikiRik Oct 23, 2022
7799d9b
refactor: remove custom repeat to use native function
WikiRik Oct 23, 2022
5375f17
refactor: implement suggestion new Date
WikiRik Oct 23, 2022
17370bd
Refactor isAlpha with options API
pixelbucket-dev Oct 25, 2022
2c33aa7
Refactor isAlpha tests
pixelbucket-dev Oct 25, 2022
4bf7277
Improve readability of test helper
pixelbucket-dev Oct 25, 2022
8559785
Fix name bug
pixelbucket-dev Oct 25, 2022
1f65dac
Format code
pixelbucket-dev Oct 25, 2022
74a024e
Fix typo
pixelbucket-dev Oct 25, 2022
3fe6d24
Improve comment
pixelbucket-dev Oct 25, 2022
a596cdb
Improve Error message
pixelbucket-dev Oct 25, 2022
bb77b71
Update Readme
pixelbucket-dev Oct 25, 2022
d3ad86c
Rename function
pixelbucket-dev Oct 25, 2022
e0fb4b2
Improve resilience with missing "locale" option
pixelbucket-dev Oct 25, 2022
47877de
Fix test description
pixelbucket-dev Oct 25, 2022
1ee8afc
Improve README
pixelbucket-dev Oct 25, 2022
b55cab6
Reintroduce legacy args for backwards-compat
pixelbucket-dev Oct 26, 2022
8949343
Remove commented code
pixelbucket-dev Oct 26, 2022
9aedc11
Merge branch 'master' into isAlpha-options-refactor
pixelbucket-dev Feb 5, 2023
b1de4d7
Fix liniting error
pixelbucket-dev Feb 5, 2023
26ad363
Remove redundant file
pixelbucket-dev Feb 5, 2023
bb2a6c6
Remove trailing spaces
pixelbucket-dev Feb 5, 2023
0760c01
Fix tests
pixelbucket-dev Feb 5, 2023
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,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, 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.
**equals(str, comparison)** | check if the string matches the comparison.
**isAfter(str [, options])** | check if the string is a date that is after the specified date.<br/><br/>`options` is an object that defaults to `{ comparisonDate: Date().toString() }`.<br/>**Options:**<br/>`comparisonDate`: Date to compare to. Defaults to `Date().toString()` (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', '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', 'ko-KR', 'ja-JP', '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', 'th-TH', '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, 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', 'ko-KR', 'ja-JP','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', 'th-TH', '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.
**isAlpha(str [, options])** | check if the string contains only letters (a-zA-Z).<br/><br/>`options` is an object that defaults to `{ locale: 'en-US' }`.<br/><br/>**Options:**<br/>`locale`: The supported 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', 'ko-KR', 'ja-JP', '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`. The locale list is `validator.isAlphaLocales`.<br/><br/>`ignore`: (optional) A string or RegExp containing characters to ignore, e.g. `'- /' ` or `/[\s/-]/g` to ignore spaces and "-".
**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', 'ko-KR', 'ja-JP','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.
**isAscii(str)** | check if the string contains ASCII chars only.
**isBase32(str [, options])** | check if the 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][Crockford Base32].
**isBase58(str)** | check if the string is base58 encoded.
Expand Down
42 changes: 27 additions & 15 deletions src/lib/isAlpha.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
import assertString from './util/assertString';
import { alpha } from './alpha';

export default function isAlpha(_str, locale = 'en-US', options = {}) {
assertString(_str);
Copy link
Author

Choose a reason for hiding this comment

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

In upstream this was removed. Any idea why, @WikiRik ?

Copy link
Author

Choose a reason for hiding this comment

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

I am referring to the function isAfter. Is it because toDate implicitely runs assertString in already?

function removeIgnoredCharacters(str, ignoredCharacters) {
if (!ignoredCharacters) {
return str;
}

if (ignoredCharacters instanceof RegExp) {
return str.replace(ignoredCharacters, '');
}

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 (typeof ignoredCharacters === 'string') {
return str.replace(new RegExp(`[${ignoredCharacters.replace(/[-[\]{}()*+?.,\\^$|#\\s]/g, '\\$&')}]`, 'g'), ''); // escape regex for 'ignoredCharacters'
}

if (locale in alpha) {
throw new Error('"ignore" should be instance of a String or RegExp');
}

export default function isAlpha(_str, ...args) {
assertString(_str);

// For backwards compatibility:
// isAlpha(str [, locale, options])
// i.e. `options` could be used as argument for the legacy `locale`
const locale = (typeof args[0] === 'object' ? args[0].locale : args[0]) || 'en-US';
const ignore = (typeof args[0] === 'object' ? args[0].ignore : args[1]?.ignore);

const str = removeIgnoredCharacters(_str, ignore);

if (alpha[locale]) {
return alpha[locale].test(str);
}
throw new Error(`Invalid locale '${locale}'`);

throw new Error(`Invalid "locale" '${locale}'`);
}

export const locales = Object.keys(alpha);
2 changes: 1 addition & 1 deletion test/exports.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from 'assert';
import validator from '../index';
Copy link
Author

Choose a reason for hiding this comment

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

FYI: I don't know what this file is used for, but npm run lint:fix errors here.

import validator from '../src/index';
import { locales as isPostalCodeLocales } from '../src/lib/isPostalCode';
import { locales as isAlphaLocales } from '../src/lib/isAlpha';
import { locales as isAlphanumericLocales } from '../src/lib/isAlphanumeric';
Expand Down
Loading