This repository has been archived by the owner on Oct 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #198 from studiopress/bugfix/validate-new-category…
…-input Validate new categories on input
- Loading branch information
Showing
3 changed files
with
72 additions
and
2 deletions.
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { hasIllegalChars, stripIllegalChars } from '../validateInput'; | ||
|
||
const regexPattern = new RegExp( /([^a-z0-9 -]+)/gi ); | ||
|
||
describe( 'validateInput', () => { | ||
describe( 'hasIllegalChars', () => { | ||
it.each( [ | ||
[ '', false ], | ||
[ 'Nothing to strip', false ], | ||
[ "String that might've been a problem", true ], | ||
[ 'String with !#@$% illegal ^&*() chars', true ], | ||
] )( 'matches the illegal characters', ( input, expected ) => { | ||
expect( hasIllegalChars( input, regexPattern ) ).toBe( expected ); | ||
} ); | ||
} ); | ||
describe( 'stripIllegalChars', () => { | ||
it.each( [ | ||
[ '', '' ], | ||
[ 'Nothing to strip', 'Nothing to strip' ], | ||
[ | ||
'String with !#@$%^&*() illegal chars', | ||
'String with illegal chars', | ||
], | ||
[ | ||
"This might've caused a whitescreen previously!", | ||
'This mightve caused a whitescreen previously', | ||
], | ||
] )( 'strips the illegal characters', ( input, expected ) => { | ||
expect( stripIllegalChars( input, regexPattern ) ).toBe( expected ); | ||
} ); | ||
} ); | ||
} ); |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const defaultPattern = new RegExp( /([^a-z0-9 -]+)/gi ); | ||
|
||
export function hasIllegalChars( | ||
input: string, | ||
regexPattern = defaultPattern | ||
) { | ||
return !! input.match( regexPattern ); | ||
} | ||
|
||
export function stripIllegalChars( | ||
input: string, | ||
regexPattern = defaultPattern | ||
) { | ||
return input.replace( regexPattern, '' ); | ||
} |