From ec1ca128b8b4ee5b0d1d77a1dd18d40792173c27 Mon Sep 17 00:00:00 2001 From: Shinigami Date: Mon, 1 May 2023 11:20:12 +0200 Subject: [PATCH] chore: improve regex usage (#2108) --- scripts/apidoc/signature.ts | 2 +- src/modules/finance/index.ts | 2 +- src/modules/helpers/index.ts | 12 ++++++------ src/modules/internet/index.ts | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/scripts/apidoc/signature.ts b/scripts/apidoc/signature.ts index 671015747d9..c3be50e51a1 100644 --- a/scripts/apidoc/signature.ts +++ b/scripts/apidoc/signature.ts @@ -212,7 +212,7 @@ function typeToText(type_?: Type, short = false): string { if ( (reflectionType?.type === 'literal' || reflectionType?.type === 'union') && - !type.name.match(/Char$/) + !/Char$/.test(type.name) ) { return typeToText(reflectionType, short); } diff --git a/src/modules/finance/index.ts b/src/modules/finance/index.ts index 32b04795ae9..4add0243138 100644 --- a/src/modules/finance/index.ts +++ b/src/modules/finance/index.ts @@ -834,7 +834,7 @@ export class FinanceModule { const normalizedIssuer = issuer.toLowerCase(); if (normalizedIssuer in localeFormat) { format = this.faker.helpers.arrayElement(localeFormat[normalizedIssuer]); - } else if (issuer.match(/#/)) { + } else if (/#/.test(issuer)) { // The user chose an optional scheme format = issuer; } else { diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index 048614cf5f7..ba7e083a880 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -277,7 +277,7 @@ export class HelpersModule { let max: number; let tmp: number; let repetitions: number; - let token = string.match(RANGE_REP_REG); + let token = RANGE_REP_REG.exec(string); while (token != null) { min = parseInt(token[2]); max = parseInt(token[3]); @@ -293,23 +293,23 @@ export class HelpersModule { string.slice(0, token.index) + token[1].repeat(repetitions) + string.slice(token.index + token[0].length); - token = string.match(RANGE_REP_REG); + token = RANGE_REP_REG.exec(string); } // Deal with repeat `{num}` - token = string.match(REP_REG); + token = REP_REG.exec(string); while (token != null) { repetitions = parseInt(token[2]); string = string.slice(0, token.index) + token[1].repeat(repetitions) + string.slice(token.index + token[0].length); - token = string.match(REP_REG); + token = REP_REG.exec(string); } // Deal with range `[min-max]` (only works with numbers for now) //TODO: implement for letters e.g. [0-9a-zA-Z] etc. - token = string.match(RANGE_REG); + token = RANGE_REG.exec(string); while (token != null) { min = parseInt(token[1]); // This time we are not capturing the char before `[]` max = parseInt(token[2]); @@ -324,7 +324,7 @@ export class HelpersModule { string.slice(0, token.index) + this.faker.number.int({ min, max }).toString() + string.slice(token.index + token[0].length); - token = string.match(RANGE_REG); + token = RANGE_REG.exec(string); } return string; diff --git a/src/modules/internet/index.ts b/src/modules/internet/index.ts index 066044aa376..37ada74aa0d 100644 --- a/src/modules/internet/index.ts +++ b/src/modules/internet/index.ts @@ -1484,7 +1484,7 @@ export class InternetModule { } if (memorable) { - if (prefix.match(consonant)) { + if (consonant.test(prefix)) { pattern = vowel; } else { pattern = consonant; @@ -1497,7 +1497,7 @@ export class InternetModule { char = char.toLowerCase(); } - if (!char.match(pattern)) { + if (!pattern.test(char)) { return _password(length, memorable, pattern, prefix); }