Skip to content

Commit

Permalink
Do not return boolean in hasRegExpFlags
Browse files Browse the repository at this point in the history
Use isValidRegExpFlag instead of match method
  • Loading branch information
AdamWr committed Jun 28, 2023
1 parent b26f700 commit 8f02390
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 26 deletions.
38 changes: 13 additions & 25 deletions src/helpers/string-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,39 +85,27 @@ export const toRegExp = (input: RawStrPattern = ''): RegExp => {
};

/**
* Checks whether the text string contains regexp flags
* Checks whether the text string contains valid regexp flags
*
* @param textRegeExp string
* @param textFlags textFlags
* @returns boolean, true if regexp contains flags
* @param regExpStr string
* @param flagsStr string
* @returns string, flagsStr if flag is valid, otherwise empty string
*/
const hasRegExpFlags = (textRegeExp: string, textFlags: string): boolean => {
const hasRegExpFlags = (regExpStr: string, flagsStr: string): string => {
if (
textFlags
&& textRegeExp.startsWith(FORWARD_SLASH)
&& textRegeExp.endsWith(FORWARD_SLASH)
flagsStr
&& regExpStr.startsWith(FORWARD_SLASH)
&& regExpStr.endsWith(FORWARD_SLASH)
// Not a correct regex if ends with '\\/'
&& !textRegeExp.endsWith('\\/')
// Only valid flags - dgimsuvy
&& textFlags.match(/[dgimsuvyDGIMSUVY]$/)
&& !regExpStr.endsWith('\\/')
&& isValidRegExpFlag(flagsStr)
) {
return true;
return flagsStr;
}
return false;
};

/**
* Returns the regexp flags as a string
* or empty string if flag is not valid
*
* @param flags string
* @returns string
*/
const getRegExpFlags = (flags: string): string => {
return isValidRegExpFlag(flags) ? flagsPart : '';
return '';
};

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

if ((input[0] === FORWARD_SLASH && input[input.length - 1] === FORWARD_SLASH) || flags) {
const regExpInput = flags ? regExpPart : input;
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers/string-utils.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { toRegExp, inferValue } from '../../src/helpers';

describe('Test inferValue', () => {
describe('Test string utils', () => {
describe('Test toRegExp for valid inputs', () => {
const DEFAULT_VALUE = '.?';
const defaultRegexp = new RegExp(DEFAULT_VALUE);
Expand Down

0 comments on commit 8f02390

Please sign in to comment.