Skip to content

Commit

Permalink
Rename hasRegExpFlags to getRegExpFlags
Browse files Browse the repository at this point in the history
Update JSDoc
Get rid of unnecessary flagsStr
  • Loading branch information
AdamWr committed Jun 29, 2023
1 parent 8f02390 commit 9395fa5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/helpers/string-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '.?';
Expand All @@ -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) {
Expand All @@ -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('\\/')
Expand All @@ -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);
}
Expand Down
4 changes: 4 additions & 0 deletions tests/helpers/string-utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/,
Expand Down

0 comments on commit 9395fa5

Please sign in to comment.