From 0df56748f933fa3f176256feeec394c1e3bd4507 Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Sat, 21 Oct 2023 00:03:22 +0200 Subject: [PATCH 1/4] infra(unicorn): better-regex --- .eslintrc.js | 1 - scripts/apidoc/parameterDefaults.ts | 2 +- scripts/apidoc/signature.ts | 4 +- scripts/apidoc/typedoc.ts | 4 +- scripts/generateLocales.ts | 2 +- src/modules/finance/iban.ts | 2 +- src/modules/git/index.ts | 2 +- src/modules/helpers/index.ts | 16 ++--- src/modules/image/providers/unsplash.ts | 2 +- src/modules/internet/index.ts | 6 +- test/modules/airline.spec.ts | 20 +++---- test/modules/color.spec.ts | 58 ++++++++----------- test/modules/commerce.spec.ts | 2 +- test/modules/datatype.spec.ts | 12 ++-- test/modules/finance.spec.ts | 14 ++--- test/modules/finance_iban.spec.ts | 4 +- test/modules/git.spec.ts | 4 +- test/modules/helpers.spec.ts | 30 +++++----- test/modules/image.spec.ts | 4 +- test/modules/internet.spec.ts | 24 ++++---- test/modules/random.spec.ts | 22 +++---- test/modules/string.spec.ts | 34 +++++------ test/modules/system.spec.ts | 10 ++-- test/modules/vehicle.spec.ts | 10 +--- test/scripts/apidoc/verify-jsdoc-tags.spec.ts | 4 +- 25 files changed, 135 insertions(+), 158 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 6a01e343588..c481a687016 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -47,7 +47,6 @@ module.exports = defineConfig({ // TODO @Shinigami92 2023-09-23: The following rules currently conflict with our code. // Each rule should be checked whether it should be enabled/configured and the problems fixed, or stay disabled permanently. - 'unicorn/better-regex': 'off', 'unicorn/consistent-function-scoping': 'off', 'unicorn/filename-case': 'off', 'unicorn/import-style': 'off', diff --git a/scripts/apidoc/parameterDefaults.ts b/scripts/apidoc/parameterDefaults.ts index 34264b05b8f..de7a302a6bc 100644 --- a/scripts/apidoc/parameterDefaults.ts +++ b/scripts/apidoc/parameterDefaults.ts @@ -58,7 +58,7 @@ function cleanParameterDefault(value?: string): string | undefined { } // Strip type casts: "'foobar' as unknown as T" => "'foobar'" - return value.replace(/ as unknown as [A-Za-z<>]+/, ''); + return value.replace(/ as unknown as [<>A-Za-z]+/, ''); } /** diff --git a/scripts/apidoc/signature.ts b/scripts/apidoc/signature.ts index 2cca92b99d9..0ff360e1551 100644 --- a/scripts/apidoc/signature.ts +++ b/scripts/apidoc/signature.ts @@ -333,7 +333,7 @@ function extractDefaultFromComment(comment?: Comment): string | undefined { return; } - const result = /^(.*)[ \n]Defaults to `([^`]+)`\.(.*)$/s.exec(text); + const result = /^(.*)[\n ]Defaults to `([^`]+)`\.(.*)$/s.exec(text); if (!result) { return; } @@ -344,6 +344,6 @@ function extractDefaultFromComment(comment?: Comment): string | undefined { summary.splice(summary.length - 2, 2); const lastSummaryPart = summary[summary.length - 1]; - lastSummaryPart.text = lastSummaryPart.text.replace(/[ \n]Defaults to $/, ''); + lastSummaryPart.text = lastSummaryPart.text.replace(/[\n ]Defaults to $/, ''); return result[2]; } diff --git a/scripts/apidoc/typedoc.ts b/scripts/apidoc/typedoc.ts index 28a3db8545a..418825ef93a 100644 --- a/scripts/apidoc/typedoc.ts +++ b/scripts/apidoc/typedoc.ts @@ -188,7 +188,7 @@ export function extractSourceBaseUrl( reflection: DeclarationReflection | SignatureReflection ): string { return extractSourceUrl(reflection).replace( - /^(.*\/blob\/[0-9a-f]+\/)(.*)$/, + /^(.*\/blob\/[\da-f]+\/)(.*)$/, '$1' ); } @@ -202,7 +202,7 @@ export function extractSourcePath( reflection: DeclarationReflection | SignatureReflection ): string { return extractSourceUrl(reflection).replace( - /^(.*\/blob\/[0-9a-f]+\/)(.*)$/, + /^(.*\/blob\/[\da-f]+\/)(.*)$/, '$2' ); } diff --git a/scripts/generateLocales.ts b/scripts/generateLocales.ts index d2e9513e1b0..f4bb30b2e61 100644 --- a/scripts/generateLocales.ts +++ b/scripts/generateLocales.ts @@ -242,7 +242,7 @@ async function updateLocaleFile(filePath: string): Promise { if (lstatSync(filePath).isFile()) { const [locale, moduleKey, entryKey] = filePath .substring(pathLocales.length + 1, filePath.length - 3) - .split(/[\\/]/); + .split(/[/\\]/); await updateLocaleFileHook(filePath, locale, moduleKey, entryKey); } } diff --git a/src/modules/finance/iban.ts b/src/modules/finance/iban.ts index 2eb6fbba4f2..400b18db087 100644 --- a/src/modules/finance/iban.ts +++ b/src/modules/finance/iban.ts @@ -1407,7 +1407,7 @@ const iban: Iban = { pattern10: ['01', '02', '03', '04', '05', '06', '07', '08', '09'], pattern100: ['001', '002', '003', '004', '005', '006', '007', '008', '009'], toDigitString: (str) => - str.replace(/[A-Z]/gi, (match) => + str.replace(/[a-z]/gi, (match) => String(match.toUpperCase().charCodeAt(0) - 55) ), }; diff --git a/src/modules/git/index.ts b/src/modules/git/index.ts index cbbeb3518e7..8984bb68858 100644 --- a/src/modules/git/index.ts +++ b/src/modules/git/index.ts @@ -99,7 +99,7 @@ export class GitModule { const email = this.faker.internet.email({ firstName, lastName }); // Normalize user according to https://github.com/libgit2/libgit2/issues/5342 - user = user.replace(/^[.,:;"\\']|[<>\n]|[.,:;"\\']$/g, ''); + user = user.replace(/^["',.:;\\]|[\n<>]|["',.:;\\]$/g, ''); lines.push( `Author: ${user} <${email}>`, diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index 81007156995..948b1432f5d 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -99,9 +99,9 @@ function legacyRegexpStringParse( string: string = '' ): string { // Deal with range repeat `{min,max}` - const RANGE_REP_REG = /(.)\{(\d+),(\d+)\}/; - const REP_REG = /(.)\{(\d+)\}/; - const RANGE_REG = /\[(\d+)-(\d+)\]/; + const RANGE_REP_REG = /(.){(\d+),(\d+)}/; + const REP_REG = /(.){(\d+)}/; + const RANGE_REG = /\[(\d+)-(\d+)]/; let min: number; let max: number; let tmp: number; @@ -416,7 +416,7 @@ export class SimpleHelpersModule { // Deal with single wildcards const SINGLE_CHAR_REG = - /([.A-Za-z0-9])(?:\{(\d+)(?:,(\d+)|)\}|(\?|\*|\+))(?![^[]*]|[^{]*})/; + /([\d.A-Za-z])(?:{(\d+)(?:,(\d+)|)}|([*+?]))(?![^[]*]|[^{]*})/; let token = SINGLE_CHAR_REG.exec(pattern); while (token != null) { const quantifierMin: string = token[2]; @@ -437,9 +437,9 @@ export class SimpleHelpersModule { token = SINGLE_CHAR_REG.exec(pattern); } - const SINGLE_RANGE_REG = /(\d-\d|\w-\w|\d|\w|[-!@#$&()`.+,/"])/; + const SINGLE_RANGE_REG = /(\d-\d|\w-\w|\d|\w|[!"#$&()+,./@`-])/; const RANGE_ALPHANUMEMRIC_REG = - /\[(\^|)(-|)(.+?)\](?:\{(\d+)(?:,(\d+)|)\}|(\?|\*|\+)|)/; + /\[(\^|)(-|)(.+?)](?:{(\d+)(?:,(\d+)|)}|([*+?])|)/; // Deal with character classes with quantifiers `[a-z0-9]{min[, max]}` token = RANGE_ALPHANUMEMRIC_REG.exec(pattern); while (token != null) { @@ -552,7 +552,7 @@ export class SimpleHelpersModule { token = RANGE_ALPHANUMEMRIC_REG.exec(pattern); } - const RANGE_REP_REG = /(.)\{(\d+),(\d+)\}/; + const RANGE_REP_REG = /(.){(\d+),(\d+)}/; // Deal with quantifier ranges `{min,max}` token = RANGE_REP_REG.exec(pattern); while (token != null) { @@ -571,7 +571,7 @@ export class SimpleHelpersModule { token = RANGE_REP_REG.exec(pattern); } - const REP_REG = /(.)\{(\d+)\}/; + const REP_REG = /(.){(\d+)}/; // Deal with repeat `{num}` token = REP_REG.exec(pattern); while (token != null) { diff --git a/src/modules/image/providers/unsplash.ts b/src/modules/image/providers/unsplash.ts index a2b4ee0e16a..0f5aeecc566 100644 --- a/src/modules/image/providers/unsplash.ts +++ b/src/modules/image/providers/unsplash.ts @@ -69,7 +69,7 @@ export class Unsplash { url += `/${width}x${height}`; if (keyword != null) { - const keywordFormat = /^([A-Za-z0-9].+,[A-Za-z0-9]+)$|^([A-Za-z0-9]+)$/; + const keywordFormat = /^([\dA-Za-z].+,[\dA-Za-z]+)$|^([\dA-Za-z]+)$/; if (keywordFormat.test(keyword)) { url += `?${keyword}`; } diff --git a/src/modules/internet/index.ts b/src/modules/internet/index.ts index 42d05c5523d..ad23b53738e 100644 --- a/src/modules/internet/index.ts +++ b/src/modules/internet/index.ts @@ -265,7 +265,7 @@ export class InternetModule { let localPart: string = this.userName({ firstName, lastName }); // Strip any special characters from the local part of the email address // This could happen if invalid chars are passed in manually in the firstName/lastName - localPart = localPart.replace(/[^A-Za-z0-9._+-]+/g, ''); + localPart = localPart.replace(/[^\w+.-]+/g, ''); // The local part of an email address is limited to 64 chars per RFC 3696 // We limit to 50 chars to be more realistic @@ -1462,8 +1462,8 @@ export class InternetModule { * Copyright(c) 2011-2013 Bermi Ferrer * MIT Licensed */ - const vowel = /[aeiouAEIOU]$/; - const consonant = /[bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ]$/; + const vowel = /[aeiou]$/i; + const consonant = /[b-df-hj-np-tv-z]$/i; const _password = ( length: number, memorable: boolean, diff --git a/test/modules/airline.spec.ts b/test/modules/airline.spec.ts index 45737f5abe1..e6b87d3c342 100644 --- a/test/modules/airline.spec.ts +++ b/test/modules/airline.spec.ts @@ -68,13 +68,13 @@ describe('airline', () => { describe(`recordLocator()`, () => { it('should use the default values when not passing arguments', () => { const recordLocator = faker.airline.recordLocator(); - expect(recordLocator).toMatch(/^[A-HJ-KM-NP-Z]{6}$/); + expect(recordLocator).toMatch(/^[A-HJKMNP-Z]{6}$/); }); it('should allow numeric characters', () => { const recordLocator = faker.airline.recordLocator({ allowNumerics: true, }); - expect(recordLocator).toMatch(/^[2-9A-HJ-KM-NP-Z]{6}$/); + expect(recordLocator).toMatch(/^[2-9A-HJKMNP-Z]{6}$/); }); it('should allow visually similar characters', () => { const recordLocator = faker.airline.recordLocator({ @@ -87,7 +87,7 @@ describe('airline', () => { allowNumerics: true, allowVisuallySimilarCharacters: true, }); - expect(recordLocator).toMatch(/^[0-9A-Z]{6}$/); + expect(recordLocator).toMatch(/^[\dA-Z]{6}$/); }); }); @@ -129,7 +129,7 @@ describe('airline', () => { const row = matchResult[1]; const seatLetter = matchResult[2]; expect(row).toSatisfy((row: number) => row >= 1 && row <= 60); - expect(seatLetter).toMatch(/^[A-HJ-K]$/); + expect(seatLetter).toMatch(/^[A-HJK]$/); }); }); @@ -143,37 +143,37 @@ describe('airline', () => { describe(`flightNumber()`, () => { it('should return a random flight number', () => { const flightNumber = faker.airline.flightNumber(); - expect(flightNumber).toMatch(/^[1-9][0-9]{0,3}$/); + expect(flightNumber).toMatch(/^[1-9]\d{0,3}$/); }); it('should return a random flight number with 3 digits', () => { const flightNumber = faker.airline.flightNumber({ length: 3 }); - expect(flightNumber).toMatch(/^[1-9][0-9]{2}$/); + expect(flightNumber).toMatch(/^[1-9]\d{2}$/); }); it('should return a random flight number with 2 to 4 digits', () => { const flightNumber = faker.airline.flightNumber({ length: { min: 2, max: 4 }, }); - expect(flightNumber).toMatch(/^[1-9][0-9]{1,3}$/); + expect(flightNumber).toMatch(/^[1-9]\d{1,3}$/); }); it('should return a random flight number with leading zeros', () => { const flightNumber = faker.airline.flightNumber({ addLeadingZeros: true, }); - expect(flightNumber).toMatch(/^[0-9]{4}$/); + expect(flightNumber).toMatch(/^\d{4}$/); }); it('should return a random flight number with 3 digits and leading zeros', () => { const flightNumber = faker.airline.flightNumber({ length: 3, addLeadingZeros: true, }); - expect(flightNumber).toMatch(/^[0-9][1-9][0-9]{2}$/); + expect(flightNumber).toMatch(/^\d[1-9]\d{2}$/); }); it('should return a random flight number with 2 to 4 digits and leading zeros', () => { const flightNumber = faker.airline.flightNumber({ length: { min: 2, max: 4 }, addLeadingZeros: true, }); - expect(flightNumber).toMatch(/^[0-9]{1,4}$/); + expect(flightNumber).toMatch(/^\d{1,4}$/); }); }); } diff --git a/test/modules/color.spec.ts b/test/modules/color.spec.ts index 354233f20f8..70ef06a0548 100644 --- a/test/modules/color.spec.ts +++ b/test/modules/color.spec.ts @@ -59,28 +59,28 @@ describe('color', () => { describe(`rgb()`, () => { it('should return a random rgb hex color', () => { const color = faker.color.rgb(); - expect(color).match(/^(#[a-f0-9]{6})$/); + expect(color).match(/^(#[\da-f]{6})$/); }); }); describe(`rgb({ prefix: '0x' })`, () => { it('should return a random rgb hex color with # prefix', () => { const color = faker.color.rgb({ prefix: '0x' }); - expect(color).match(/^(0x[a-f0-9]{6})$/); + expect(color).match(/^(0x[\da-f]{6})$/); }); }); describe(`rgbHex({ prefix: '0x', casing: 'lower' })`, () => { it('should return a random rgb hex color with # prefix and lower casing only', () => { const color = faker.color.rgb({ prefix: '0x', casing: 'lower' }); - expect(color).match(/^(0x[a-f0-9]{6})$/); + expect(color).match(/^(0x[\da-f]{6})$/); }); }); describe(`rgb({ prefix: '0x', casing: 'upper' })`, () => { it('should return a random rgb hex color with # prefix and upper casing only', () => { const color = faker.color.rgb({ prefix: '0x', casing: 'upper' }); - expect(color).match(/^(0x[A-F0-9]{6})$/); + expect(color).match(/^(0x[\dA-F]{6})$/); }); }); @@ -98,21 +98,21 @@ describe('color', () => { describe(`rgb({ format: 'css' })`, () => { it('should return a random rgb color in css format', () => { const color = faker.color.rgb({ format: 'css' }); - expect(color).match(/^(rgb\([0-9]{1,3}, [0-9]{1,3}, [0-9]{1,3}\))$/); + expect(color).match(/^(rgb\((?:\d{1,3}, ){2}\d{1,3}\))$/); }); }); describe(`rgb({ format: 'binary' })`, () => { it('should return a random rgb color in binary format', () => { const color = faker.color.rgb({ format: 'binary' }); - expect(color).match(/^([01]{8} [01]{8} [01]{8})$/); + expect(color).match(/^((?:[01]{8} ){2}[01]{8})$/); }); }); describe(`rgb({ includeAlpha: true })`, () => { it('should return a random rgb color in hex format with alpha value', () => { const color = faker.color.rgb({ includeAlpha: true }); - expect(color).match(/^(#[a-fA-F0-9]{8})$/); + expect(color).match(/^(#[\dA-Fa-f]{8})$/); }); }); @@ -137,16 +137,14 @@ describe('color', () => { format: 'binary', includeAlpha: true, }); - expect(color).match(/^([01]{8} [01]{8} [01]{8} [01]{8,32})$/); + expect(color).match(/^((?:[01]{8} ){3}[01]{8,32})$/); }); }); describe(`rgb({ format: 'css', includeAlpha: true })`, () => { it('should return a random rgb color in css format with alpha value', () => { const color = faker.color.rgb({ format: 'css', includeAlpha: true }); - expect(color).match( - /^(rgba\([0-9]{1,3}, [0-9]{1,3}, [0-9]{1,3}, \d*\.?\d*\))$/ - ); + expect(color).match(/^(rgba\((?:\d{1,3}, ){3}\d*\.?\d*\))$/); }); }); @@ -175,18 +173,14 @@ describe('color', () => { describe(`cmyk({ format: 'css' })`, () => { it('should return a random cmyk color in css format', () => { const color = faker.color.cmyk({ format: 'css' }); - expect(color).match( - /^(cmyk\([0-9]{1,3}%, [0-9]{1,3}%, [0-9]{1,3}%, [0-9]{1,3}%\))$/ - ); + expect(color).match(/^(cmyk\((?:\d{1,3}%, ){3}\d{1,3}%\))$/); }); }); describe(`cmyk({ format: 'binary' })`, () => { it('should return a random cmyk color in binary format', () => { const color = faker.color.cmyk({ format: 'binary' }); - expect(color).match( - /^([01]{8,32} [01]{8,32} [01]{8,32} [01]{8,32})$/ - ); + expect(color).match(/^((?:[01]{8,32} ){3}[01]{8,32})$/); }); }); @@ -206,9 +200,7 @@ describe('color', () => { describe(`hsl({ format: 'css' })`, () => { it('should return a random hsl color in css format', () => { const color = faker.color.hsl({ format: 'css' }); - expect(color).match( - /^(hsl\([0-9]{1,3}deg [0-9]{1,3}% [0-9]{1,3}%\))$/ - ); + expect(color).match(/^(hsl\(\d{1,3}deg(?: \d{1,3}%){2}\))$/); }); }); @@ -216,7 +208,7 @@ describe('color', () => { it('should return a random hsl color in css format with an alpha value', () => { const color = faker.color.hsl({ format: 'css', includeAlpha: true }); expect(color).match( - /^(hsl\([0-9]{1,3}deg [0-9]{1,3}% [0-9]{1,3}% \/ \d*\.?\d*\))$/ + /^(hsl\(\d{1,3}deg(?: \d{1,3}%){2} \/ \d*\.?\d*\))$/ ); }); }); @@ -224,7 +216,7 @@ describe('color', () => { describe(`hsl({ format: 'binary' })`, () => { it('should return a random hsl color in binary format', () => { const color = faker.color.hsl({ format: 'binary' }); - expect(color).match(/^([01]{8,32} [01]{8,32} [01]{8,32})$/); + expect(color).match(/^((?:[01]{8,32} ){2}[01]{8,32})$/); }); }); @@ -234,9 +226,7 @@ describe('color', () => { format: 'binary', includeAlpha: true, }); - expect(color).match( - /^([01]{8,32} [01]{8,32} [01]{8,32} [01]{8,32})$/ - ); + expect(color).match(/^((?:[01]{8,32} ){3}[01]{8,32})$/); }); }); @@ -269,14 +259,14 @@ describe('color', () => { describe(`hwb({ format: 'css' })`, () => { it('should return a random hwb color in css format', () => { const color = faker.color.hwb({ format: 'css' }); - expect(color).match(/^(hwb\([0-9]{1,3} [0-9]{1,3}% [0-9]{1,3}%\))$/); + expect(color).match(/^(hwb\(\d{1,3}(?: \d{1,3}%){2}\))$/); }); }); describe(`hwb({ format: 'binary' })`, () => { it('should return a random hwb color in binary format', () => { const color = faker.color.hwb({ format: 'binary' }); - expect(color).match(/^([01]{8,32} [01]{8,32} [01]{8,32})$/); + expect(color).match(/^((?:[01]{8,32} ){2}[01]{8,32})$/); }); }); @@ -310,7 +300,7 @@ describe('color', () => { it('should return a random lab color in css format', () => { const color = faker.color.lab({ format: 'css' }); expect(color).match( - /^(lab\((\d*\.?\d*|[0-9]{1,3})% -?\d*\.?\d* -?\d*\.?\d*\))$/ + /^(lab\((\d*\.?\d*|\d{1,3})%(?: -?\d*\.?\d*){2}\))$/ ); }); }); @@ -318,7 +308,7 @@ describe('color', () => { describe(`lab({ format: 'binary' })`, () => { it('should return a random lab color in binary format', () => { const color = faker.color.lab({ format: 'binary' }); - expect(color).match(/^([01]{8,32} [01]{8,32} [01]{8,32})$/); + expect(color).match(/^((?:[01]{8,32} ){2}[01]{8,32})$/); }); }); @@ -352,7 +342,7 @@ describe('color', () => { it('should return a random lch color in css format', () => { const color = faker.color.lch({ format: 'css' }); expect(color).match( - /^(lch\((\d*\.?\d*|[0-9]{1,3})% \d*\.?\d* \d*\.?\d*\))$/ + /^(lch\((\d*\.?\d*|\d{1,3})%(?: \d*\.?\d*){2}\))$/ ); }); }); @@ -360,7 +350,7 @@ describe('color', () => { describe(`lch({ format: 'binary' })`, () => { it('should return a random lch color in binary format', () => { const color = faker.color.lch({ format: 'binary' }); - expect(color).match(/^([01]{8,32} [01]{8,32} [01]{8,32})$/); + expect(color).match(/^((?:[01]{8,32} ){2}[01]{8,32})$/); }); }); @@ -392,16 +382,14 @@ describe('color', () => { format: 'css', space: 'prophoto-rgb', }); - expect(color).match( - /^color\(prophoto-rgb \d*\.?\d* \d*\.?\d* \d*\.?\d*\)$/ - ); + expect(color).match(/^color\(prophoto-rgb(?: \d*\.?\d*){3}\)$/); }); }); describe(`colorByCSSColorSpace({ format: 'binary' })`, () => { it('should return a random color for a CSS color space in binary format', () => { const color = faker.color.colorByCSSColorSpace({ format: 'binary' }); - expect(color).match(/^([01]{8,32} [01]{8,32} [01]{8,32})$/); + expect(color).match(/^((?:[01]{8,32} ){2}[01]{8,32})$/); }); }); } diff --git a/test/modules/commerce.spec.ts b/test/modules/commerce.spec.ts index 6ccc18f7b5e..a23afa57e32 100644 --- a/test/modules/commerce.spec.ts +++ b/test/modules/commerce.spec.ts @@ -110,7 +110,7 @@ describe('commerce', () => { expect( amount, 'The expected match should not include a currency symbol' - ).toMatch(/^[0-9.]+$/); + ).toMatch(/^[\d.]+$/); }); it('should handle negative amounts, but return 0', () => { diff --git a/test/modules/datatype.spec.ts b/test/modules/datatype.spec.ts index 964636bc995..2dbb92960cf 100644 --- a/test/modules/datatype.spec.ts +++ b/test/modules/datatype.spec.ts @@ -400,7 +400,7 @@ describe('datatype', () => { it('generates a valid UUID', () => { const UUID = faker.datatype.uuid(); const RFC4122 = - /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/; + /^[\da-f]{8}-[\da-f]{4}-[1-5][\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$/; expect(UUID).toMatch(RFC4122); }); }); @@ -408,25 +408,25 @@ describe('datatype', () => { describe('hexadecimal', () => { it('generates single hex character when no additional argument was provided', () => { const hex = faker.datatype.hexadecimal(); - expect(hex).toMatch(/^(0x)[0-9a-f]{1}$/i); + expect(hex).toMatch(/^(0x)[\da-f]$/i); expect(hex.substring(2)).toHaveLength(1); }); it('generates a hex string with a provided prefix', () => { const hex = faker.datatype.hexadecimal({ prefix: '0x' }); - expect(hex).toMatch(/^(0x)[0-9A-F]+$/i); + expect(hex).toMatch(/^(0x)[\da-f]+$/i); expect(hex).toHaveLength(3); }); it('generates a random hex string with a provided length', () => { const hex = faker.datatype.hexadecimal({ length: 5 }); - expect(hex).toMatch(/^(0x)[0-9a-f]+$/i); + expect(hex).toMatch(/^(0x)[\da-f]+$/i); expect(hex.substring(2)).toHaveLength(5); }); it('generates a hex string with a provided casing', () => { const hex = faker.datatype.hexadecimal({ case: 'lower' }); - expect(hex).toMatch(/^(0x)[0-9a-f]+$/i); + expect(hex).toMatch(/^(0x)[\da-f]+$/i); expect(hex.substring(2)).toHaveLength(1); }); @@ -436,7 +436,7 @@ describe('datatype', () => { length: 7, case: 'upper', }); - expect(hex).toMatch(/^(0x)[0-9A-F]+$/i); + expect(hex).toMatch(/^(0x)[\da-f]+$/i); expect(hex.substring(2)).toHaveLength(7); }); }); diff --git a/test/modules/finance.spec.ts b/test/modules/finance.spec.ts index a0cfbb2ecef..73972777160 100644 --- a/test/modules/finance.spec.ts +++ b/test/modules/finance.spec.ts @@ -279,7 +279,7 @@ describe('finance', () => { expect( amount, 'The expected match should not include a currency symbol' - ).toMatch(/^[0-9.]+$/); + ).toMatch(/^[\d.]+$/); }); it('should handle negative amounts', () => { @@ -414,7 +414,7 @@ describe('finance', () => { const litecoinAddress = faker.finance.litecoinAddress(); expect(litecoinAddress).toBeTypeOf('string'); - expect(litecoinAddress).toMatch(/^[LM3][1-9a-km-zA-HJ-NP-Z]{25,32}$/); + expect(litecoinAddress).toMatch(/^[3LM][1-9A-HJ-NP-Za-km-z]{25,32}$/); }); }); @@ -456,7 +456,7 @@ describe('finance', () => { it('should return a correct credit card number when issuer provided', () => { //TODO: implement checks for each format with regexp const visa = faker.finance.creditCardNumber('visa'); - expect(visa).toMatch(/^4(([0-9]){12}|([0-9]){3}(-([0-9]){4}){3})$/); + expect(visa).toMatch(/^4(\d{12}|\d{3}(-\d{4}){3})$/); expect(visa).toSatisfy(luhnCheck); const mastercard = faker.finance.creditCardNumber('mastercard'); @@ -530,7 +530,7 @@ describe('finance', () => { it('should contain only digits', () => { const pin = faker.finance.pin(); - expect(pin).toMatch(/^[0-9]+$/); + expect(pin).toMatch(/^\d+$/); }); it('should default to a length of 4', () => { @@ -553,7 +553,7 @@ describe('finance', () => { const ethereumAddress = faker.finance.ethereumAddress(); expect(ethereumAddress).toBeTypeOf('string'); - expect(ethereumAddress).toMatch(/^(0x)[0-9a-f]{40}$/); + expect(ethereumAddress).toMatch(/^(0x)[\da-f]{40}$/); }); }); @@ -598,7 +598,7 @@ describe('finance', () => { const bic = faker.finance.bic(); expect(bic).toBeTypeOf('string'); - expect(bic).toMatch(/^[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?$/); + expect(bic).toMatch(/^[A-Z]{6}[\dA-Z]{2}([\dA-Z]{3})?$/); expect(ibanLib.iso3166).toContain(bic.substring(4, 6)); }); @@ -606,7 +606,7 @@ describe('finance', () => { const bic = faker.finance.bic({ includeBranchCode: true }); expect(bic).toBeTypeOf('string'); - expect(bic).toMatch(/^[A-Z]{6}[A-Z0-9]{2}[A-Z0-9]{3}$/); + expect(bic).toMatch(/^[A-Z]{6}[\dA-Z]{5}$/); expect(ibanLib.iso3166).toContain(bic.substring(4, 6)); }); }); diff --git a/test/modules/finance_iban.spec.ts b/test/modules/finance_iban.spec.ts index c2dc12a7819..a5e87a59b6c 100644 --- a/test/modules/finance_iban.spec.ts +++ b/test/modules/finance_iban.spec.ts @@ -196,8 +196,8 @@ describe('finance_iban', () => { `National Digit:${iban.substring( 9, 10 - )} must contains only digits in PK IBAN ${ibanFormated}` - ).toMatch(/^\d{1}$/); + )} must contains only one digit in PK IBAN ${ibanFormated}` + ).toMatch(/^\d$/); expect( iban.substring(10, 26), `Account Code:${iban.substring( diff --git a/test/modules/git.spec.ts b/test/modules/git.spec.ts index 2b721e8bf71..44f8268df92 100644 --- a/test/modules/git.spec.ts +++ b/test/modules/git.spec.ts @@ -57,7 +57,7 @@ describe('git', () => { expect(parts.length).toBeGreaterThanOrEqual(6); expect(parts.length).toBeLessThanOrEqual(7); - expect(parts[0]).toMatch(/^commit [a-f0-9]+$/); + expect(parts[0]).toMatch(/^commit [\da-f]+$/); const isValidAuthor = (email: string) => { // `validator.isEmail()` does not support display names // that contain unquoted characters like . output by Git so we need @@ -70,7 +70,7 @@ describe('git', () => { const authorRegex = /^Author: .*$/; if (parts.length === 7) { - expect(parts[1]).toMatch(/^Merge: [a-f0-9]+ [a-f0-9]+$/); + expect(parts[1]).toMatch(/^Merge:(?: [\da-f]+){2}$/); expect(parts[2]).toMatch(authorRegex); expect(parts[2].substring(8)).toSatisfy(isValidAuthor); expect(parts[3]).toMatch(/^Date: .+$/); diff --git a/test/modules/helpers.spec.ts b/test/modules/helpers.spec.ts index 0564a794b08..1f63d52fd79 100644 --- a/test/modules/helpers.spec.ts +++ b/test/modules/helpers.spec.ts @@ -44,6 +44,7 @@ describe('helpers', () => { .it('some string', 'Hello !#{3}test[1-5]'); }); + /* eslint-disable unicorn/better-regex -- we don't support the fancy patterns yet */ t.describe('fromRegExp', (t) => { t.it('with static string', 'Hello World!') .it('with static RegExp', /Hello World!/) @@ -61,6 +62,7 @@ describe('helpers', () => { .it('with negation and case insensitive flag', /[^a-t0-7]{10}/i) .it('with negation', /[^A-Za-y0-9]{10}/); }); + /* eslint-enable unicorn/better-regex */ t.describe('mustache', (t) => { t.it('template with string', 'Hello {{name}}!', { name: 'John' }).it( @@ -562,9 +564,7 @@ describe('helpers', () => { const number = faker.helpers.replaceCreditCardSymbols( '6453-####-####-####-###L' ); - expect(number).toMatch( - /^6453-([0-9]){4}-([0-9]){4}-([0-9]){4}-([0-9]){4}$/ - ); + expect(number).toMatch(/^6453(?:-\d{4}){4}$/); expect(number).toSatisfy(luhnCheck); }); @@ -573,9 +573,7 @@ describe('helpers', () => { '6453-****-****-****-***L', '*' ); - expect(number).toMatch( - /^6453-([0-9]){4}-([0-9]){4}-([0-9]){4}-([0-9]){4}$/ - ); + expect(number).toMatch(/^6453(?:-\d{4}){4}$/); expect(number).toSatisfy(luhnCheck); }); @@ -584,16 +582,12 @@ describe('helpers', () => { '6453-*{4}-*{4}-*{4}-*{3}L', '*' ); - expect(number).toMatch( - /^6453-([0-9]){4}-([0-9]){4}-([0-9]){4}-([0-9]){4}$/ - ); + expect(number).toMatch(/^6453(?:-\d{4}){4}$/); expect(number).toSatisfy(luhnCheck); number = faker.helpers.replaceCreditCardSymbols( '645[5-9]-#{4,6}-#{1,2}-#{4,6}-#{3}L' ); - expect(number).toMatch( - /^645[5-9]-([0-9]){4,6}-([0-9]){1,2}-([0-9]){4,6}-([0-9]){4}$/ - ); + expect(number).toMatch(/^645[5-9]-\d{4,6}-\d{1,2}-\d{4,6}-\d{4}$/); expect(number).toSatisfy(luhnCheck); }); }); @@ -631,14 +625,14 @@ describe('helpers', () => { it('creates a numerical range', () => { const string = faker.helpers.regexpStyleStringParse('Hello[0-9]'); - expect(string).toMatch(/^Hello[0-9]$/); + expect(string).toMatch(/^Hello\d$/); }); it('deals with multiple tokens in one string', () => { const string = faker.helpers.regexpStyleStringParse( 'Test#{5}%{2,5}Testing**[1-5]**{10}END' ); - expect(string).toMatch(/^Test#{5}%{2,5}Testing\*\*[1-5]\*\*{10}END$/); + expect(string).toMatch(/^Test#{5}%{2,5}Testing\*\*[1-5]\*{11}END$/); }); }); @@ -658,7 +652,7 @@ describe('helpers', () => { it('creates a numerical range', () => { const string = faker.helpers.fromRegExp('Hello[0-9]'); - expect(string).toMatch(/^Hello[0-9]$/); + expect(string).toMatch(/^Hello\d$/); }); it('deals with multiple tokens in one string', () => { @@ -676,6 +670,7 @@ describe('helpers', () => { expect(() => faker.helpers.fromRegExp('[a-z0-9]{10,5}')).toThrow(); }); + /* eslint-disable unicorn/better-regex -- we don't support the fancy patterns yet */ it('deals with RegExp object', () => { const string = faker.helpers.fromRegExp(/[A-D0-9]{4}-[A-D0-9]{4}/); expect(string).toMatch(/^[A-D0-9]{4}-[A-D0-9]{4}$/); @@ -690,6 +685,7 @@ describe('helpers', () => { const string = faker.helpers.fromRegExp(/[A-D0-9]{4}-[A-D0-9]{4}/i); expect(string).toMatch(/^[A-D0-9]{4}-[A-D0-9]{4}$/i); }); + /* eslint-enable unicorn/better-regex */ }); describe('shuffle()', () => { @@ -1102,13 +1098,13 @@ describe('helpers', () => { it('should be able to handle random }} brackets', () => { expect(faker.helpers.fake('}}hello{{string.alpha}}')).toMatch( - /^}}hello[a-zA-Z]$/ + /^}}hello[A-Za-z]$/ ); }); it('should be able to handle connected brackets', () => { expect(faker.helpers.fake('{{{string.alpha}}}')).toMatch( - /^{[a-zA-Z]}$/ + /^{[a-z]}$/i ); }); diff --git a/test/modules/image.spec.ts b/test/modules/image.spec.ts index d2c0152fa53..b4198266cfc 100644 --- a/test/modules/image.spec.ts +++ b/test/modules/image.spec.ts @@ -474,7 +474,7 @@ describe('image', () => { expect(imageUrl).toBeTypeOf('string'); expect(imageUrl).toMatch( - /^https:\/\/picsum\.photos\/seed\/[0-9a-zA-Z]+\/\d+\/\d+$/ + /^https:\/\/picsum\.photos\/seed\/[\dA-Za-z]+\/\d+\/\d+$/ ); }); }); @@ -485,7 +485,7 @@ describe('image', () => { expect(imageUrl).toBeTypeOf('string'); expect(imageUrl).toMatch( - /^https:\/\/via\.placeholder\.com\/\d+x\d+\/[0-9a-fA-F]{6}\/[0-9a-fA-F]{6}\.[a-z]{3,4}\?text=.+$/ + /^ht{2}ps:\/{2}via\.placeholder\.com\/\d+x\d+(?:\/[\dA-Fa-f]{6}){2}\.[a-z]{3,4}\?text=.+$/ ); }); }); diff --git a/test/modules/internet.spec.ts b/test/modules/internet.spec.ts index 79d329de3ea..9490818cf78 100644 --- a/test/modules/internet.spec.ts +++ b/test/modules/internet.spec.ts @@ -96,7 +96,7 @@ describe('internet', () => { memorable: false, }) .it('with pattern option', { - pattern: /[0-9]/, + pattern: /\d/, }) .it('with prefix option', { prefix: 'test', @@ -104,17 +104,17 @@ describe('internet', () => { .it('with length, memorable, pattern and prefix option', { length: 10, memorable: false, - pattern: /[0-9]/, + pattern: /\d/, prefix: 'test', }) .it('with legacy length', 10) .it('with legacy length and memorable', 10, false) - .it('with legacy length, memorable and pattern', 10, false, /[0-9]/) + .it('with legacy length, memorable and pattern', 10, false, /\d/) .it( 'with legacy length, memorable, pattern and prefix', 10, false, - /[0-9]/, + /\d/, 'test' ); }); @@ -297,7 +297,7 @@ describe('internet', () => { const [prefix, suffix] = email.split('@'); expect(prefix).toMatch( - /^Mike((\d{1,2})|([.!#$%&'*+-/=?^_`{|}~]Smith\d{1,2})|([.!#$%&'*+-/=?^_`{|}~]Smith))/ + /^Mike((\d{1,2})|([!#$%&'*+-/=?^_`{|}~]Smith\d{1,2})|([!#$%&'*+-/=?^_`{|}~]Smith))/ ); expect(faker.definitions.internet.free_email).toContain(suffix); }); @@ -365,7 +365,7 @@ describe('internet', () => { expect(suffix).toMatch(/^example\.(com|net|org)$/); expect(faker.definitions.internet.example_email).toContain(suffix); - expect(prefix).toMatch(/^Mike([.!#$%&'*+-/=?^_`{|}~]Smith)?\d*/); + expect(prefix).toMatch(/^Mike([!#$%&'*+-/=?^_`{|}~]Smith)?\d*/); }); }); @@ -649,7 +649,7 @@ describe('internet', () => { expect(ua).toBeTypeOf('string'); expect(ua.length).toBeGreaterThanOrEqual(1); expect(ua).toMatch( - /^(([^\d]+\/[\dA-Za-z.]+(\s\(.*\)))|([^\d]+\/[\dA-Za-z.]+(\s\(.*\)*))(\s[^\d]+\/[\dA-Za-z.]+(\s\(.*\)*))*)$/ + /^((\D+\/[\d.A-Za-z]+(\s\(.*\)))|(\D+\/[\d.A-Za-z]+(\s\(.*\)*))(\s\D+\/[\d.A-Za-z]+(\s\(.*\)*))*)$/ ); }); }); @@ -683,7 +683,7 @@ describe('internet', () => { expect(mac).toBeTruthy(); expect(mac).toBeTypeOf('string'); expect(mac).toHaveLength(17); - expect(mac).toMatch(/^([a-f0-9]{2}:){5}[a-f0-9]{2}$/); + expect(mac).toMatch(/^([\da-f]{2}:){5}[\da-f]{2}$/); expect(mac).toSatisfy(validator.isMACAddress); }); @@ -693,7 +693,7 @@ describe('internet', () => { expect(mac).toBeTruthy(); expect(mac).toBeTypeOf('string'); expect(mac).toHaveLength(17); - expect(mac).toMatch(/^([a-f0-9]{2}-){5}[a-f0-9]{2}$/); + expect(mac).toMatch(/^([\da-f]{2}-){5}[\da-f]{2}$/); expect(mac).toSatisfy(validator.isMACAddress); }); @@ -715,7 +715,7 @@ describe('internet', () => { expect(mac).toBeTruthy(); expect(mac).toBeTypeOf('string'); expect(mac).toHaveLength(17); - expect(mac).toMatch(/^([a-f0-9]{2}:){5}[a-f0-9]{2}$/); + expect(mac).toMatch(/^([\da-f]{2}:){5}[\da-f]{2}$/); expect(mac).toSatisfy(validator.isMACAddress); } ); @@ -773,7 +773,7 @@ describe('internet', () => { const password = faker.internet.password({ length: 32, memorable: false, - pattern: /(!|\?|&|\[|\]|%|\$|[a-zA-Z0-9])/, + pattern: /(!|\?|&|\[|]|%|\$|[\dA-Za-z])/, }); expect(password).toBeTruthy(); @@ -787,7 +787,7 @@ describe('internet', () => { const password = faker.internet.password({ length: 32, memorable: false, - pattern: /(!|\?|&|\[|\]|%|\$|[a-zA-Z0-9])/, + pattern: /(!|\?|&|\[|]|%|\$|[\dA-Za-z])/, prefix: 'a!G6', }); diff --git a/test/modules/random.spec.ts b/test/modules/random.spec.ts index 80fa10aa703..78e65c08212 100644 --- a/test/modules/random.spec.ts +++ b/test/modules/random.spec.ts @@ -142,7 +142,7 @@ describe('random', () => { it.each([ ['upper', /^[A-Z]{250}$/], ['lower', /^[a-z]{250}$/], - ['mixed', /^[a-zA-Z]{250}$/], + ['mixed', /^[a-z]{250}$/i], ] as const)('should return %s-case', (casing, pattern) => { const actual = faker.random.alpha({ count: 250, casing }); expect(actual).toMatch(pattern); @@ -233,9 +233,9 @@ describe('random', () => { }); it.each([ - ['upper', /^[A-Z0-9]{250}$/], - ['lower', /^[a-z0-9]{250}$/], - ['mixed', /^[a-zA-Z0-9]{250}$/], + ['upper', /^[\dA-Z]{250}$/], + ['lower', /^[\da-z]{250}$/], + ['mixed', /^[\da-z]{250}$/i], ] as const)('should return %s-case', (casing, pattern) => { const actual = faker.random.alphaNumeric(250, { casing }); expect(actual).toMatch(pattern); @@ -311,7 +311,7 @@ describe('random', () => { }); expect(alphaText).toHaveLength(5); - expect(alphaText).toMatch(/^[0-9b-oq-z]{5}$/); + expect(alphaText).toMatch(/^[\db-oq-z]{5}$/); }); it('should throw if all possible characters being banned', () => { @@ -355,7 +355,7 @@ describe('random', () => { const actual = faker.random.numeric(); expect(actual).toHaveLength(1); - expect(actual).toMatch(/^[0-9]$/); + expect(actual).toMatch(/^\d$/); }); it.each(times(100))( @@ -364,7 +364,7 @@ describe('random', () => { const actual = faker.random.numeric(length); expect(actual).toHaveLength(length); - expect(actual).toMatch(/^[0-9]*$/); + expect(actual).toMatch(/^\d*$/); } ); @@ -385,13 +385,13 @@ describe('random', () => { expect(actual).toBeTypeOf('string'); expect(actual).toHaveLength(1000); - expect(actual).toMatch(/^[0-9]+$/); + expect(actual).toMatch(/^\d+$/); }); it('should allow leading zeros via option', () => { const actual = faker.random.numeric(15, { allowLeadingZeros: true }); - expect(actual).toMatch(/^[0-9]+$/); + expect(actual).toMatch(/^\d+$/); }); it('should allow leading zeros via option and all other digits banned', () => { @@ -444,7 +444,7 @@ describe('random', () => { }); expect(actual).toHaveLength(1000); - expect(actual).toMatch(/^[0235679]{1000}$/); + expect(actual).toMatch(/^[0235-79]{1000}$/); }); it('should ban all digits passed via bannedDigits via string', () => { @@ -453,7 +453,7 @@ describe('random', () => { }); expect(actual).toHaveLength(1000); - expect(actual).toMatch(/^[0235679]{1000}$/); + expect(actual).toMatch(/^[0235-79]{1000}$/); }); }); } diff --git a/test/modules/string.spec.ts b/test/modules/string.spec.ts index 9d2debba257..6b30c4c90b5 100644 --- a/test/modules/string.spec.ts +++ b/test/modules/string.spec.ts @@ -139,7 +139,7 @@ describe('string', () => { it('should only contain characters from provided string', () => { const actual = faker.string.fromCharacters('foobar'); - expect(actual).toMatch(/^[foobar]$/); + expect(actual).toMatch(/^[abfor]$/); }); it('should generate 5 random letters', () => { @@ -197,13 +197,13 @@ describe('string', () => { it('should return any letters when no option is provided', () => { const actual = faker.string.alpha(); - expect(actual).toMatch(/^[a-zA-Z]$/); + expect(actual).toMatch(/^[A-Za-z]$/); }); it.each([ ['upper', /^[A-Z]{250}$/], ['lower', /^[a-z]{250}$/], - ['mixed', /^[a-zA-Z]{250}$/], + ['mixed', /^[A-Za-z]{250}$/], ] as const)('should return %s-case', (casing, pattern) => { const actual = faker.string.alpha({ length: 250, casing }); expect(actual).toMatch(pattern); @@ -321,9 +321,9 @@ describe('string', () => { }); it.each([ - ['upper', /^[A-Z0-9]{250}$/], - ['lower', /^[a-z0-9]{250}$/], - ['mixed', /^[a-zA-Z0-9]{250}$/], + ['upper', /^[\dA-Z]{250}$/], + ['lower', /^[\da-z]{250}$/], + ['mixed', /^[\da-z]{250}$/i], ] as const)('should return %s-case', (casing, pattern) => { const actual = faker.string.alphanumeric({ length: 250, casing }); expect(actual).toMatch(pattern); @@ -418,7 +418,7 @@ describe('string', () => { }); expect(alphaText).toHaveLength(5); - expect(alphaText).toMatch(/^[0-9b-oq-z]{5}$/); + expect(alphaText).toMatch(/^[\db-oq-z]{5}$/); }); it('should throw if all possible characters being excluded (string)', () => { @@ -546,7 +546,7 @@ describe('string', () => { describe(`hexadecimal`, () => { it('generates single hex character when no additional argument was provided', () => { const hex = faker.string.hexadecimal(); - expect(hex).toMatch(/^0x[0-9a-f]*$/i); + expect(hex).toMatch(/^0x[\da-f]*$/i); expect(hex).toHaveLength(3); }); @@ -555,7 +555,7 @@ describe('string', () => { length: 5, prefix: '', }); - expect(hex).toMatch(/^[0-9a-f]*$/i); + expect(hex).toMatch(/^[\da-f]*$/i); expect(hex).toHaveLength(5); }); @@ -587,7 +587,7 @@ describe('string', () => { const actual = faker.string.numeric(); expect(actual).toHaveLength(1); - expect(actual).toMatch(/^[0-9]$/); + expect(actual).toMatch(/^\d$/); }); it.each(times(100))( @@ -596,7 +596,7 @@ describe('string', () => { const actual = faker.string.numeric(length); expect(actual).toHaveLength(length); - expect(actual).toMatch(/^[0-9]*$/); + expect(actual).toMatch(/^\d*$/); } ); @@ -629,7 +629,7 @@ describe('string', () => { expect(actual).toBeTypeOf('string'); expect(actual).toHaveLength(1000); - expect(actual).toMatch(/^[0-9]+$/); + expect(actual).toMatch(/^\d+$/); }); it('should allow leading zeros via option', () => { @@ -638,7 +638,7 @@ describe('string', () => { allowLeadingZeros: true, }); - expect(actual).toMatch(/^[0-9]+$/); + expect(actual).toMatch(/^\d+$/); }); it('should allow leading zeros via option and all other digits excluded', () => { @@ -696,7 +696,7 @@ describe('string', () => { }); expect(actual).toHaveLength(1000); - expect(actual).toMatch(/^[0235679]{1000}$/); + expect(actual).toMatch(/^[0235-79]{1000}$/); }); it('should ban all digits passed via exclude via string', () => { @@ -706,7 +706,7 @@ describe('string', () => { }); expect(actual).toHaveLength(1000); - expect(actual).toMatch(/^[0235679]{1000}$/); + expect(actual).toMatch(/^[0235-79]{1000}$/); }); }); @@ -751,7 +751,7 @@ describe('string', () => { it('generates a valid UUID', () => { const UUID = faker.string.uuid(); const RFC4122 = - /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/; + /^[\da-f]{8}-[\da-f]{4}-[1-5][\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$/; expect(UUID).toMatch(RFC4122); }); }); @@ -759,7 +759,7 @@ describe('string', () => { describe(`nanoid`, () => { it('generates a valid Nano ID', () => { const id = faker.string.nanoid(); - const regex = /^[0-9a-zA-Z_-]+$/; + const regex = /^[\w-]+$/; expect(id).toMatch(regex); }); diff --git a/test/modules/system.spec.ts b/test/modules/system.spec.ts index b325468d6f8..d628f063e5f 100644 --- a/test/modules/system.spec.ts +++ b/test/modules/system.spec.ts @@ -306,7 +306,7 @@ describe('system', () => { networkInterface, `generated network interface should be valid network interface.` ).toMatch( - /^(?:P\d)?(?:en|wl|ww)(?:o\d|s\d(?:f\d)?(?:d\d)?|x[a-f\d]{12}|p\ds\d(?:f\d)?(?:d\d)?)$/ + /^(?:P\d)?(?:en|wl|ww)(?:o\d|s\d(?:f\d)?(?:d\d)?|x[\da-f]{12}|p\ds\d(?:f\d)?(?:d\d)?)$/ ); }); @@ -319,7 +319,7 @@ describe('system', () => { networkInterface, `generated network interface should be valid network interface.` ).toMatch( - /^(?:P\d)?wl(?:o\d|s\d(?:f\d)?(?:d\d)?|x[a-f\d]{12}|p\ds\d(?:f\d)?(?:d\d)?)$/ + /^(?:P\d)?wl(?:o\d|s\d(?:f\d)?(?:d\d)?|x[\da-f]{12}|p\ds\d(?:f\d)?(?:d\d)?)$/ ); }); @@ -353,7 +353,7 @@ describe('system', () => { expect( networkInterface, `generated network interface should be valid network interface.` - ).toMatch(/^(?:en|wl|ww)x[a-f\d]{12}$/); + ).toMatch(/^(?:en|wl|ww)x[\da-f]{12}$/); }); it('should return a network interface with a pci schema', () => { @@ -376,13 +376,13 @@ describe('system', () => { expect( networkInterface, `generated network interface should be valid network interface.` - ).toMatch(/^enx[a-f\d]{12}$/); + ).toMatch(/^enx[\da-f]{12}$/); }); }); describe('cron()', () => { const regex = - /^([0-9]|[1-5]\d|\*) ([0-9]|1\d|2[0-3]|\*) ([1-9]|[12]\d|3[01]|\*|\?) ([1-9]|1[0-2]|\*) ([0-6]|\*|\?|[A-Z]{3}) ((19[7-9]d)|20\d{2}|\*)?/; + /^(\d|[1-5]\d|\*) (\d|1\d|2[0-3]|\*) ([1-9]|[12]\d|3[01]|\*|\?) ([1-9]|1[0-2]|\*) ([0-6]|\*|\?|[A-Z]{3}) ((19[7-9]d)|20\d{2}|\*)?/; const regexElements = regex.toString().replace(/\//g, '').split(' '); diff --git a/test/modules/vehicle.spec.ts b/test/modules/vehicle.spec.ts index f691a1fa441..b96203f83a0 100644 --- a/test/modules/vehicle.spec.ts +++ b/test/modules/vehicle.spec.ts @@ -88,9 +88,7 @@ describe('vehicle', () => { describe('vin()', () => { it('returns valid vin number', () => { const vin = faker.vehicle.vin(); - expect(vin).toMatch( - /^([A-HJ-NPR-Z0-9]{10}[A-HJ-NPR-Z0-9]{1}[A-HJ-NPR-Z0-9]{1}\d{5})$/ - ); + expect(vin).toMatch(/^([\dA-HJ-NPR-Z]{12}\d{5})$/); }); it('should return valid vin number', () => { @@ -98,9 +96,7 @@ describe('vehicle', () => { expect(vin).toBeTruthy(); expect(vin).toBeTypeOf('string'); - expect(vin).toMatch( - /^([A-HJ-NPR-Z0-9]{10}[A-HJ-NPR-Z0-9]{1}[A-HJ-NPR-Z0-9]{1}\d{5})$/ - ); + expect(vin).toMatch(/^([\dA-HJ-NPR-Z]{12}\d{5})$/); }); }); @@ -110,7 +106,7 @@ describe('vehicle', () => { expect(vrm).toBeTruthy(); expect(vrm).toBeTypeOf('string'); - expect(vrm).toMatch(/^[A-Z]{2}[0-9]{2}[A-Z]{3}$/); + expect(vrm).toMatch(/^[A-Z]{2}\d{2}[A-Z]{3}$/); }); }); diff --git a/test/scripts/apidoc/verify-jsdoc-tags.spec.ts b/test/scripts/apidoc/verify-jsdoc-tags.spec.ts index 64cbcdb4ee5..553a1a31c48 100644 --- a/test/scripts/apidoc/verify-jsdoc-tags.spec.ts +++ b/test/scripts/apidoc/verify-jsdoc-tags.spec.ts @@ -72,9 +72,7 @@ describe('verify JSDoc tags', () => { ); function assertDescription(description: string, isHtml: boolean): void { - const linkRegexp = isHtml - ? /(href)="([^"]+)"/g - : /\[([^\]]+)\]\(([^)]+)\)/g; + const linkRegexp = isHtml ? /(href)="([^"]+)"/g : /\[([^\]]+)]\(([^)]+)\)/g; const links = [...description.matchAll(linkRegexp)].map((m) => m[2]); for (const link of links) { From 53ffd42a60ab374ad44d71207f6554f6d09323ff Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Sat, 21 Oct 2023 01:07:20 +0200 Subject: [PATCH 2/4] chore: regex cleanup --- src/modules/helpers/index.ts | 2 +- src/modules/image/providers/unsplash.ts | 3 +- test/modules/color.spec.ts | 46 ++++++++++++------------- test/modules/vehicle.spec.ts | 4 +-- 4 files changed, 27 insertions(+), 28 deletions(-) diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index 948b1432f5d..bd26c3525a4 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -416,7 +416,7 @@ export class SimpleHelpersModule { // Deal with single wildcards const SINGLE_CHAR_REG = - /([\d.A-Za-z])(?:{(\d+)(?:,(\d+)|)}|([*+?]))(?![^[]*]|[^{]*})/; + /([\d.a-z])(?:{(\d+)(?:,(\d+)|)}|([*+?]))(?![^[]*]|[^{]*})/i; let token = SINGLE_CHAR_REG.exec(pattern); while (token != null) { const quantifierMin: string = token[2]; diff --git a/src/modules/image/providers/unsplash.ts b/src/modules/image/providers/unsplash.ts index 0f5aeecc566..dbd1575a3f8 100644 --- a/src/modules/image/providers/unsplash.ts +++ b/src/modules/image/providers/unsplash.ts @@ -69,7 +69,8 @@ export class Unsplash { url += `/${width}x${height}`; if (keyword != null) { - const keywordFormat = /^([\dA-Za-z].+,[\dA-Za-z]+)$|^([\dA-Za-z]+)$/; + // One or more keywords separated by commas + const keywordFormat = /^[\da-z]+(,[\da-z]+)*$/i; if (keywordFormat.test(keyword)) { url += `?${keyword}`; } diff --git a/test/modules/color.spec.ts b/test/modules/color.spec.ts index 70ef06a0548..be09932065f 100644 --- a/test/modules/color.spec.ts +++ b/test/modules/color.spec.ts @@ -59,28 +59,28 @@ describe('color', () => { describe(`rgb()`, () => { it('should return a random rgb hex color', () => { const color = faker.color.rgb(); - expect(color).match(/^(#[\da-f]{6})$/); + expect(color).match(/^#[\da-f]{6}$/); }); }); describe(`rgb({ prefix: '0x' })`, () => { it('should return a random rgb hex color with # prefix', () => { const color = faker.color.rgb({ prefix: '0x' }); - expect(color).match(/^(0x[\da-f]{6})$/); + expect(color).match(/^0x[\da-f]{6}$/); }); }); describe(`rgbHex({ prefix: '0x', casing: 'lower' })`, () => { it('should return a random rgb hex color with # prefix and lower casing only', () => { const color = faker.color.rgb({ prefix: '0x', casing: 'lower' }); - expect(color).match(/^(0x[\da-f]{6})$/); + expect(color).match(/^0x[\da-f]{6}$/); }); }); describe(`rgb({ prefix: '0x', casing: 'upper' })`, () => { it('should return a random rgb hex color with # prefix and upper casing only', () => { const color = faker.color.rgb({ prefix: '0x', casing: 'upper' }); - expect(color).match(/^(0x[\dA-F]{6})$/); + expect(color).match(/^0x[\dA-F]{6}$/); }); }); @@ -98,21 +98,21 @@ describe('color', () => { describe(`rgb({ format: 'css' })`, () => { it('should return a random rgb color in css format', () => { const color = faker.color.rgb({ format: 'css' }); - expect(color).match(/^(rgb\((?:\d{1,3}, ){2}\d{1,3}\))$/); + expect(color).match(/^rgb\((?:\d{1,3}, ){2}\d{1,3}\)$/); }); }); describe(`rgb({ format: 'binary' })`, () => { it('should return a random rgb color in binary format', () => { const color = faker.color.rgb({ format: 'binary' }); - expect(color).match(/^((?:[01]{8} ){2}[01]{8})$/); + expect(color).match(/^(?:[01]{8} ){2}[01]{8}$/); }); }); describe(`rgb({ includeAlpha: true })`, () => { it('should return a random rgb color in hex format with alpha value', () => { const color = faker.color.rgb({ includeAlpha: true }); - expect(color).match(/^(#[\dA-Fa-f]{8})$/); + expect(color).match(/^#[\dA-Fa-f]{8}$/); }); }); @@ -137,14 +137,14 @@ describe('color', () => { format: 'binary', includeAlpha: true, }); - expect(color).match(/^((?:[01]{8} ){3}[01]{8,32})$/); + expect(color).match(/^(?:[01]{8} ){3}[01]{8,32}$/); }); }); describe(`rgb({ format: 'css', includeAlpha: true })`, () => { it('should return a random rgb color in css format with alpha value', () => { const color = faker.color.rgb({ format: 'css', includeAlpha: true }); - expect(color).match(/^(rgba\((?:\d{1,3}, ){3}\d*\.?\d*\))$/); + expect(color).match(/^rgba\((?:\d{1,3}, ){3}\d*\.?\d*\)$/); }); }); @@ -173,14 +173,14 @@ describe('color', () => { describe(`cmyk({ format: 'css' })`, () => { it('should return a random cmyk color in css format', () => { const color = faker.color.cmyk({ format: 'css' }); - expect(color).match(/^(cmyk\((?:\d{1,3}%, ){3}\d{1,3}%\))$/); + expect(color).match(/^cmyk\((?:\d{1,3}%, ){3}\d{1,3}%\)$/); }); }); describe(`cmyk({ format: 'binary' })`, () => { it('should return a random cmyk color in binary format', () => { const color = faker.color.cmyk({ format: 'binary' }); - expect(color).match(/^((?:[01]{8,32} ){3}[01]{8,32})$/); + expect(color).match(/^(?:[01]{8,32} ){3}[01]{8,32}$/); }); }); @@ -200,7 +200,7 @@ describe('color', () => { describe(`hsl({ format: 'css' })`, () => { it('should return a random hsl color in css format', () => { const color = faker.color.hsl({ format: 'css' }); - expect(color).match(/^(hsl\(\d{1,3}deg(?: \d{1,3}%){2}\))$/); + expect(color).match(/^hsl\(\d{1,3}deg(?: \d{1,3}%){2}\)$/); }); }); @@ -208,7 +208,7 @@ describe('color', () => { it('should return a random hsl color in css format with an alpha value', () => { const color = faker.color.hsl({ format: 'css', includeAlpha: true }); expect(color).match( - /^(hsl\(\d{1,3}deg(?: \d{1,3}%){2} \/ \d*\.?\d*\))$/ + /^hsl\(\d{1,3}deg(?: \d{1,3}%){2} \/ \d*\.?\d*\)$/ ); }); }); @@ -216,7 +216,7 @@ describe('color', () => { describe(`hsl({ format: 'binary' })`, () => { it('should return a random hsl color in binary format', () => { const color = faker.color.hsl({ format: 'binary' }); - expect(color).match(/^((?:[01]{8,32} ){2}[01]{8,32})$/); + expect(color).match(/^(?:[01]{8,32} ){2}[01]{8,32}$/); }); }); @@ -226,7 +226,7 @@ describe('color', () => { format: 'binary', includeAlpha: true, }); - expect(color).match(/^((?:[01]{8,32} ){3}[01]{8,32})$/); + expect(color).match(/^(?:[01]{8,32} ){3}[01]{8,32}$/); }); }); @@ -259,14 +259,14 @@ describe('color', () => { describe(`hwb({ format: 'css' })`, () => { it('should return a random hwb color in css format', () => { const color = faker.color.hwb({ format: 'css' }); - expect(color).match(/^(hwb\(\d{1,3}(?: \d{1,3}%){2}\))$/); + expect(color).match(/^hwb\(\d{1,3}(?: \d{1,3}%){2}\)$/); }); }); describe(`hwb({ format: 'binary' })`, () => { it('should return a random hwb color in binary format', () => { const color = faker.color.hwb({ format: 'binary' }); - expect(color).match(/^((?:[01]{8,32} ){2}[01]{8,32})$/); + expect(color).match(/^(?:[01]{8,32} ){2}[01]{8,32}$/); }); }); @@ -300,7 +300,7 @@ describe('color', () => { it('should return a random lab color in css format', () => { const color = faker.color.lab({ format: 'css' }); expect(color).match( - /^(lab\((\d*\.?\d*|\d{1,3})%(?: -?\d*\.?\d*){2}\))$/ + /^lab\((\d*\.?\d*|\d{1,3})%(?: -?\d*\.?\d*){2}\)$/ ); }); }); @@ -308,7 +308,7 @@ describe('color', () => { describe(`lab({ format: 'binary' })`, () => { it('should return a random lab color in binary format', () => { const color = faker.color.lab({ format: 'binary' }); - expect(color).match(/^((?:[01]{8,32} ){2}[01]{8,32})$/); + expect(color).match(/^(?:[01]{8,32} ){2}[01]{8,32}$/); }); }); @@ -341,16 +341,14 @@ describe('color', () => { describe(`lch({ format: 'css' })`, () => { it('should return a random lch color in css format', () => { const color = faker.color.lch({ format: 'css' }); - expect(color).match( - /^(lch\((\d*\.?\d*|\d{1,3})%(?: \d*\.?\d*){2}\))$/ - ); + expect(color).match(/^lch\((\d*\.?\d*|\d{1,3})%(?: \d*\.?\d*){2}\)$/); }); }); describe(`lch({ format: 'binary' })`, () => { it('should return a random lch color in binary format', () => { const color = faker.color.lch({ format: 'binary' }); - expect(color).match(/^((?:[01]{8,32} ){2}[01]{8,32})$/); + expect(color).match(/^(?:[01]{8,32} ){2}[01]{8,32}$/); }); }); @@ -389,7 +387,7 @@ describe('color', () => { describe(`colorByCSSColorSpace({ format: 'binary' })`, () => { it('should return a random color for a CSS color space in binary format', () => { const color = faker.color.colorByCSSColorSpace({ format: 'binary' }); - expect(color).match(/^((?:[01]{8,32} ){2}[01]{8,32})$/); + expect(color).match(/^(?:[01]{8,32} ){2}[01]{8,32}$/); }); }); } diff --git a/test/modules/vehicle.spec.ts b/test/modules/vehicle.spec.ts index b96203f83a0..f85f17fed4a 100644 --- a/test/modules/vehicle.spec.ts +++ b/test/modules/vehicle.spec.ts @@ -88,7 +88,7 @@ describe('vehicle', () => { describe('vin()', () => { it('returns valid vin number', () => { const vin = faker.vehicle.vin(); - expect(vin).toMatch(/^([\dA-HJ-NPR-Z]{12}\d{5})$/); + expect(vin).toMatch(/^[\dA-HJ-NPR-Z]{12}\d{5}$/); }); it('should return valid vin number', () => { @@ -96,7 +96,7 @@ describe('vehicle', () => { expect(vin).toBeTruthy(); expect(vin).toBeTypeOf('string'); - expect(vin).toMatch(/^([\dA-HJ-NPR-Z]{12}\d{5})$/); + expect(vin).toMatch(/^[\dA-HJ-NPR-Z]{12}\d{5}$/); }); }); From 0e1012351a215db9cf29eb45df557c39afbf5967 Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Sat, 21 Oct 2023 02:10:15 +0200 Subject: [PATCH 3/4] chore: regex improvements --- scripts/generateLocales.ts | 2 +- test/modules/color.spec.ts | 2 +- test/modules/internet.spec.ts | 6 +++--- test/modules/location.spec.ts | 4 ++-- test/modules/string.spec.ts | 8 ++++---- test/modules/system.spec.ts | 2 +- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/scripts/generateLocales.ts b/scripts/generateLocales.ts index f4bb30b2e61..e1e2ff31697 100644 --- a/scripts/generateLocales.ts +++ b/scripts/generateLocales.ts @@ -405,7 +405,7 @@ async function main(): Promise { console.error(error); } - const localizedFaker = `faker${locale.replace(/^([a-z]+)/, (part) => + const localizedFaker = `faker${locale.replace(/^[a-z]+/, (part) => part.toUpperCase() )}`; diff --git a/test/modules/color.spec.ts b/test/modules/color.spec.ts index be09932065f..ee7e7b68b9a 100644 --- a/test/modules/color.spec.ts +++ b/test/modules/color.spec.ts @@ -112,7 +112,7 @@ describe('color', () => { describe(`rgb({ includeAlpha: true })`, () => { it('should return a random rgb color in hex format with alpha value', () => { const color = faker.color.rgb({ includeAlpha: true }); - expect(color).match(/^#[\dA-Fa-f]{8}$/); + expect(color).match(/^#[\da-f]{8}$/i); }); }); diff --git a/test/modules/internet.spec.ts b/test/modules/internet.spec.ts index 9490818cf78..3a966eb7b9c 100644 --- a/test/modules/internet.spec.ts +++ b/test/modules/internet.spec.ts @@ -649,7 +649,7 @@ describe('internet', () => { expect(ua).toBeTypeOf('string'); expect(ua.length).toBeGreaterThanOrEqual(1); expect(ua).toMatch( - /^((\D+\/[\d.A-Za-z]+(\s\(.*\)))|(\D+\/[\d.A-Za-z]+(\s\(.*\)*))(\s\D+\/[\d.A-Za-z]+(\s\(.*\)*))*)$/ + /^((\D+\/[\d.a-z]+(\s\(.*\)))|(\D+\/[\d.a-z]+(\s\(.*))(\s\D+\/[\d.a-z]+(\s\(.*))*)$/i ); }); }); @@ -773,7 +773,7 @@ describe('internet', () => { const password = faker.internet.password({ length: 32, memorable: false, - pattern: /(!|\?|&|\[|]|%|\$|[\dA-Za-z])/, + pattern: /([\d!$%&?[\]a-z])/i, }); expect(password).toBeTruthy(); @@ -787,7 +787,7 @@ describe('internet', () => { const password = faker.internet.password({ length: 32, memorable: false, - pattern: /(!|\?|&|\[|]|%|\$|[\dA-Za-z])/, + pattern: /([\d!$%&?[\]a-z])/i, prefix: 'a!G6', }); diff --git a/test/modules/location.spec.ts b/test/modules/location.spec.ts index 054669a5796..81d4b6fa170 100644 --- a/test/modules/location.spec.ts +++ b/test/modules/location.spec.ts @@ -184,7 +184,7 @@ describe('location', () => { it('returns random zipCode - user specified format', () => { let zipCode = faker.location.zipCode({ format: '?#? #?#' }); - expect(zipCode).toMatch(/^[A-Za-z]\d[A-Za-z]\s\d[A-Za-z]\d$/); + expect(zipCode).toMatch(/^[a-z]\d[a-z]\s\d[a-z]\d$/i); // try another format zipCode = faker.location.zipCode({ format: '###-###' }); @@ -196,7 +196,7 @@ describe('location', () => { // we'll use the en_CA locale.. const zipCode = fakerEN_CA.location.zipCode(); - expect(zipCode).toMatch(/^[A-Za-z]\d[A-Za-z]\s?\d[A-Za-z]\d$/); + expect(zipCode).toMatch(/^[a-z]\d[a-z]\s?\d[a-z]\d$/i); }); it.each([ diff --git a/test/modules/string.spec.ts b/test/modules/string.spec.ts index 6b30c4c90b5..e6f0f9b6e09 100644 --- a/test/modules/string.spec.ts +++ b/test/modules/string.spec.ts @@ -197,13 +197,13 @@ describe('string', () => { it('should return any letters when no option is provided', () => { const actual = faker.string.alpha(); - expect(actual).toMatch(/^[A-Za-z]$/); + expect(actual).toMatch(/^[a-z]$/i); }); it.each([ ['upper', /^[A-Z]{250}$/], ['lower', /^[a-z]{250}$/], - ['mixed', /^[A-Za-z]{250}$/], + ['mixed', /^[a-z]{250}$/i], ] as const)('should return %s-case', (casing, pattern) => { const actual = faker.string.alpha({ length: 250, casing }); expect(actual).toMatch(pattern); @@ -477,7 +477,7 @@ describe('string', () => { length: 5, prefix: '', }); - expect(binary).toMatch(/^[01]*$/i); + expect(binary).toMatch(/^[01]*$/); expect(binary).toHaveLength(5); }); @@ -516,7 +516,7 @@ describe('string', () => { length: 5, prefix: '', }); - expect(octal).toMatch(/^[0-7]*$/i); + expect(octal).toMatch(/^[0-7]*$/); expect(octal).toHaveLength(5); }); diff --git a/test/modules/system.spec.ts b/test/modules/system.spec.ts index d628f063e5f..10809a0a999 100644 --- a/test/modules/system.spec.ts +++ b/test/modules/system.spec.ts @@ -382,7 +382,7 @@ describe('system', () => { describe('cron()', () => { const regex = - /^(\d|[1-5]\d|\*) (\d|1\d|2[0-3]|\*) ([1-9]|[12]\d|3[01]|\*|\?) ([1-9]|1[0-2]|\*) ([0-6]|\*|\?|[A-Z]{3}) ((19[7-9]d)|20\d{2}|\*)?/; + /^([\d*]|[1-5]\d) (\d|1\d|2[0-3]|\*) ([*1-9?]|[12]\d|3[01]) ([*1-9]|1[0-2]) ([*0-6?]|[A-Z]{3}) ((19[7-9]d)|20\d{2}|\*)?/; const regexElements = regex.toString().replace(/\//g, '').split(' '); From 1b029c9102d1fa6615e8e90c498d1dcbc27e2121 Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Sat, 21 Oct 2023 22:25:35 +0200 Subject: [PATCH 4/4] chore: apply review suggestions --- test/modules/datatype.spec.ts | 11 +++++------ test/modules/string.spec.ts | 11 +++++------ 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/test/modules/datatype.spec.ts b/test/modules/datatype.spec.ts index 2dbb92960cf..aa8030b6f12 100644 --- a/test/modules/datatype.spec.ts +++ b/test/modules/datatype.spec.ts @@ -1,3 +1,4 @@ +import validator from 'validator'; import { describe, expect, it } from 'vitest'; import { faker, FakerError } from '../../src'; import { seededTests } from './../support/seededRuns'; @@ -396,12 +397,10 @@ describe('datatype', () => { }); }); - describe('UUID', () => { - it('generates a valid UUID', () => { - const UUID = faker.datatype.uuid(); - const RFC4122 = - /^[\da-f]{8}-[\da-f]{4}-[1-5][\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$/; - expect(UUID).toMatch(RFC4122); + describe('uuid', () => { + it('generates a valid UUID v4', () => { + const actual = faker.datatype.uuid(); + expect(actual).toSatisfy((uuid: string) => validator.isUUID(uuid, 4)); }); }); diff --git a/test/modules/string.spec.ts b/test/modules/string.spec.ts index e6f0f9b6e09..5de1b80062f 100644 --- a/test/modules/string.spec.ts +++ b/test/modules/string.spec.ts @@ -1,3 +1,4 @@ +import validator from 'validator'; import { describe, expect, it } from 'vitest'; import { faker, FakerError } from '../../src'; import { seededTests } from './../support/seededRuns'; @@ -747,12 +748,10 @@ describe('string', () => { }); }); - describe(`uuid`, () => { - it('generates a valid UUID', () => { - const UUID = faker.string.uuid(); - const RFC4122 = - /^[\da-f]{8}-[\da-f]{4}-[1-5][\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$/; - expect(UUID).toMatch(RFC4122); + describe('uuid', () => { + it('generates a valid UUID v4', () => { + const actual = faker.string.uuid(); + expect(actual).toSatisfy((uuid: string) => validator.isUUID(uuid, 4)); }); });