From 8f02390dae6947c9ee07b5dffada87e908e3db99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Wr=C3=B3blewski?= Date: Wed, 28 Jun 2023 11:03:32 +0200 Subject: [PATCH] Do not return boolean in hasRegExpFlags Use isValidRegExpFlag instead of match method --- src/helpers/string-utils.ts | 38 ++++++++++-------------------- tests/helpers/string-utils.spec.js | 2 +- 2 files changed, 14 insertions(+), 26 deletions(-) diff --git a/src/helpers/string-utils.ts b/src/helpers/string-utils.ts index 9b7c8d480..33561d614 100644 --- a/src/helpers/string-utils.ts +++ b/src/helpers/string-utils.ts @@ -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; diff --git a/tests/helpers/string-utils.spec.js b/tests/helpers/string-utils.spec.js index 91a18a80f..04bc3a1ba 100644 --- a/tests/helpers/string-utils.spec.js +++ b/tests/helpers/string-utils.spec.js @@ -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);