From 9395fa5fd854c4a22db3e3b6a63143779ad887ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Wr=C3=B3blewski?= Date: Thu, 29 Jun 2023 10:23:20 +0200 Subject: [PATCH] Rename hasRegExpFlags to getRegExpFlags Update JSDoc Get rid of unnecessary flagsStr --- src/helpers/string-utils.ts | 17 ++++++++--------- tests/helpers/string-utils.spec.js | 4 ++++ 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/helpers/string-utils.ts b/src/helpers/string-utils.ts index 33561d614..7c5b050df 100644 --- a/src/helpers/string-utils.ts +++ b/src/helpers/string-utils.ts @@ -52,7 +52,6 @@ export const escapeRegExp = (str: string): string => str.replace(/[.*+?^${}()|[\ * * @param input literal string or regexp pattern; defaults to '' (empty string) * @returns regular expression; defaults to /.?/ - * @throws Throw an error for invalid regex pattern */ export const toRegExp = (input: RawStrPattern = ''): RegExp => { const DEFAULT_VALUE = '.?'; @@ -69,7 +68,7 @@ export const toRegExp = (input: RawStrPattern = ''): RegExp => { * Checks whether the string is a valid regexp flag * * @param flag string - * @returns boolean, true in case if regexp flag is valid + * @returns True if regexp flag is valid, otherwise false. */ const isValidRegExpFlag = (flag: string): boolean => { if (!flag) { @@ -85,16 +84,16 @@ export const toRegExp = (input: RawStrPattern = ''): RegExp => { }; /** - * Checks whether the text string contains valid regexp flags + * Checks whether the text string contains valid regexp flags, + * and returns `flagsStr` if valid, otherwise empty string. * * @param regExpStr string * @param flagsStr string - * @returns string, flagsStr if flag is valid, otherwise empty string + * @returns `flagsStr` if it is valid, otherwise empty string. */ - const hasRegExpFlags = (regExpStr: string, flagsStr: string): string => { + const getRegExpFlags = (regExpStr: string, flagsStr: string): string => { if ( - flagsStr - && regExpStr.startsWith(FORWARD_SLASH) + regExpStr.startsWith(FORWARD_SLASH) && regExpStr.endsWith(FORWARD_SLASH) // Not a correct regex if ends with '\\/' && !regExpStr.endsWith('\\/') @@ -105,9 +104,9 @@ export const toRegExp = (input: RawStrPattern = ''): RegExp => { return ''; }; - const flags = hasRegExpFlags(regExpPart, flagsPart); + const flags = getRegExpFlags(regExpPart, flagsPart); - if ((input[0] === FORWARD_SLASH && input[input.length - 1] === FORWARD_SLASH) || flags) { + if ((input.startsWith(FORWARD_SLASH) && input.endsWith(FORWARD_SLASH)) || flags) { const regExpInput = flags ? regExpPart : input; return new RegExp(regExpInput.slice(1, -1), flags); } diff --git a/tests/helpers/string-utils.spec.js b/tests/helpers/string-utils.spec.js index 04bc3a1ba..8898a6f9d 100644 --- a/tests/helpers/string-utils.spec.js +++ b/tests/helpers/string-utils.spec.js @@ -60,6 +60,10 @@ describe('Test string utils', () => { const defaultRegexp = new RegExp(DEFAULT_VALUE); const testCases = [ + { + actual: 'g', + expected: /g/, + }, { actual: 'qwerty/g', expected: /qwerty\/g/,