Skip to content

Commit

Permalink
Update JSDoc
Browse files Browse the repository at this point in the history
Check only valid flags
  • Loading branch information
AdamWr committed Jun 27, 2023
1 parent 23c7526 commit b26f700
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/helpers/string-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const toRegExp = (input: RawStrPattern = ''): RegExp => {
* Checks whether the string is a valid regexp flag
*
* @param flag string
* @returns boolean
* @returns boolean, true in case if regexp flag is valid
*/
const isValidRegExpFlag = (flag: string): boolean => {
if (!flag) {
Expand All @@ -87,15 +87,19 @@ export const toRegExp = (input: RawStrPattern = ''): RegExp => {
/**
* Checks whether the text string contains regexp flags
*
* @param text string
* @returns boolean
* @param textRegeExp string
* @param textFlags textFlags
* @returns boolean, true if regexp contains flags
*/
const hasRegExpFlags = (text: string): boolean => {
const hasRegExpFlags = (textRegeExp: string, textFlags: string): boolean => {
if (
text.startsWith(FORWARD_SLASH)
// Check if there are 2 slashes at least
&& text.split(FORWARD_SLASH).length - 1 >= 2
&& flagsPart.match(/[a-zA-Z]$/)
textFlags
&& textRegeExp.startsWith(FORWARD_SLASH)
&& textRegeExp.endsWith(FORWARD_SLASH)
// Not a correct regex if ends with '\\/'
&& !textRegeExp.endsWith('\\/')
// Only valid flags - dgimsuvy
&& textFlags.match(/[dgimsuvyDGIMSUVY]$/)
) {
return true;
}
Expand All @@ -104,7 +108,7 @@ export const toRegExp = (input: RawStrPattern = ''): RegExp => {

/**
* Returns the regexp flags as a string
* or empty string in case if flag is not valid
* or empty string if flag is not valid
*
* @param flags string
* @returns string
Expand All @@ -113,7 +117,7 @@ export const toRegExp = (input: RawStrPattern = ''): RegExp => {
return isValidRegExpFlag(flags) ? flagsPart : '';
};

const flags = hasRegExpFlags(input) ? getRegExpFlags(flagsPart) : '';
const flags = hasRegExpFlags(regExpPart, flagsPart) ? getRegExpFlags(flagsPart) : '';

if ((input[0] === FORWARD_SLASH && input[input.length - 1] === FORWARD_SLASH) || flags) {
const regExpInput = flags ? regExpPart : input;
Expand Down
8 changes: 8 additions & 0 deletions tests/helpers/string-utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ describe('Test inferValue', () => {
actual: '/asdf/gmtest',
expected: /\/asdf\/gmtest/,
},
{
actual: '/qwert/ggm',
expected: /\/qwert\/ggm/,
},
{
actual: '/test\\/g',
expected: /\/test\\\/g/,
},
{
actual: '',
expected: defaultRegexp,
Expand Down

0 comments on commit b26f700

Please sign in to comment.