-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
feat(isAlphanumeric): allow usage of options object #2087
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
Open
pixelbucket-dev
wants to merge
35
commits into
validatorjs:master
Choose a base branch
from
pixelbucket-dev:isAlphanumeric-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
35 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 dab3ee1
Make helper reusable
pixelbucket-dev 1ce0ccb
Refactor isAlphanumeric with options API
pixelbucket-dev e5fc2b6
Refactor isAlphanumeric tests
pixelbucket-dev 04f1eae
Refactor to use UPPER_SNAKE_CASE for constants
pixelbucket-dev 9c2166e
Remove unnecessary export statements
pixelbucket-dev 00ec566
Refactor for better code reuse
pixelbucket-dev 15830a7
Harden tests against with missing "locale" option
pixelbucket-dev 90d1465
Fix README
pixelbucket-dev 7eefdeb
Reintroduce legacy args for backwards-compat
pixelbucket-dev cfddf79
Improve comment
pixelbucket-dev fd02a94
Revert "Refactor to use UPPER_SNAKE_CASE for constants"
pixelbucket-dev ed8ce77
Simplify
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,5 @@ | ||
| import assertString from './util/assertString'; | ||
| import { alpha } from './alpha'; | ||
| import { alpha, isAlpha } from './alpha'; | ||
|
|
||
| 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); | ||
| } | ||
| throw new Error(`Invalid locale '${locale}'`); | ||
| } | ||
| export default isAlpha; | ||
|
|
||
| export const locales = Object.keys(alpha); |
This file contains hidden or 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,5 @@ | ||
| import assertString from './util/assertString'; | ||
| import { alphanumeric } from './alpha'; | ||
| import { alphanumeric, isAlphanumeric } from './alpha'; | ||
|
|
||
| export default function isAlphanumeric(_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 alphanumeric) { | ||
| return alphanumeric[locale].test(str); | ||
| } | ||
| throw new Error(`Invalid locale '${locale}'`); | ||
| } | ||
| export default isAlphanumeric; | ||
|
|
||
| export const locales = Object.keys(alphanumeric); |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import assert from 'assert'; | ||
| import { format } from 'util'; | ||
| import validator from '../src/index'; | ||
|
|
||
| function test(options) { | ||
| const args = options.args || []; | ||
|
|
||
| args.unshift(null); | ||
|
|
||
| if (options.error) { | ||
| options.error.forEach((error) => { | ||
| args[0] = error; | ||
|
|
||
| try { | ||
| assert.throws(() => validator[options.validator](...args)); | ||
| } catch (err) { | ||
| const warning = format( | ||
| 'validator.%s(%s) passed but should error', | ||
| options.validator, args.join(', ') | ||
| ); | ||
|
|
||
| throw new Error(warning); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| if (options.valid) { | ||
| options.valid.forEach((valid) => { | ||
| args[0] = valid; | ||
|
|
||
| if (validator[options.validator](...args) !== true) { | ||
| const warning = format( | ||
| 'validator.%s(%s) failed but should have passed', | ||
| options.validator, args.join(', ') | ||
| ); | ||
|
|
||
| throw new Error(warning); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| if (options.invalid) { | ||
| options.invalid.forEach((invalid) => { | ||
| args[0] = invalid; | ||
|
|
||
| if (validator[options.validator](...args) !== false) { | ||
| const warning = format( | ||
| 'validator.%s(%s) passed but should have failed', | ||
| options.validator, args.join(', ') | ||
| ); | ||
|
|
||
| throw new Error(warning); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| export default { | ||
| test, | ||
| }; |
File renamed without changes.
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.
Should we make a separate table for later in this page where we show the different locales? And just refer to that here. Same holds for
isAlphanumericofcThere 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.
Yeah, good idea. It's pretty hard to read and redundant as well. Should be a single source of truth, so to speak :D. So, on a second thought, we could just remove the listings here and keep only the reference to
validator.isAlphaLocalesandvalidator.isAlphanumericLocales. I am not sure, however, if this reduces transparency. One has to navigated manually though the code base to find the original definition.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 would make a new table for
isAlphaLocales,isAlphanumericLocalesetc in the README and link to there. So in this table you only reference to that list in the README and mention the default.People don't have to go through the code that way and that makes this easier to read