-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
pixelbucket-dev
wants to merge
28
commits into
validatorjs:master
Choose a base branch
from
pixelbucket-dev:isAlpha-options-refactor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 afda0bd
feat(isAfter): allow usage of options object
WikiRik cad192e
style: make options italic
WikiRik 5ba1a43
refactor: rename test file extension to .test.js
WikiRik 71102d4
refactor: rename test-functions to testFunctions
WikiRik f32d228
refactor: implement suggestion from #2019 review
WikiRik 7799d9b
refactor: remove custom repeat to use native function
WikiRik 5375f17
refactor: implement suggestion new Date
WikiRik 17370bd
Refactor isAlpha with options API
pixelbucket-dev 2c33aa7
Refactor isAlpha tests
pixelbucket-dev 4bf7277
Improve readability of test helper
pixelbucket-dev 8559785
Fix name bug
pixelbucket-dev 1f65dac
Format code
pixelbucket-dev 74a024e
Fix typo
pixelbucket-dev 3fe6d24
Improve comment
pixelbucket-dev a596cdb
Improve Error message
pixelbucket-dev bb77b71
Update Readme
pixelbucket-dev d3ad86c
Rename function
pixelbucket-dev e0fb4b2
Improve resilience with missing "locale" option
pixelbucket-dev 47877de
Fix test description
pixelbucket-dev 1ee8afc
Improve README
pixelbucket-dev b55cab6
Reintroduce legacy args for backwards-compat
pixelbucket-dev 8949343
Remove commented code
pixelbucket-dev 9aedc11
Merge branch 'master' into isAlpha-options-refactor
pixelbucket-dev b1de4d7
Fix liniting error
pixelbucket-dev 26ad363
Remove redundant file
pixelbucket-dev bb2a6c6
Remove trailing spaces
pixelbucket-dev 0760c01
Fix tests
pixelbucket-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import assert from 'assert'; | ||
import validator from '../index'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI: I don't know what this file is used for, but |
||
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'; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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 becausetoDate
implicitely runsassertString
in already?