diff --git a/.eslintrc.cjs b/.eslintrc.cjs index bf6789c6121..287c1fb4112 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -46,8 +46,6 @@ module.exports = defineConfig({ // TODO @Shinigami92 2023-09-23: prefer-at should be turned on when we drop support for Node 14. 'unicorn/prefer-at': 'off', - // TODO @Shinigami92 2023-09-23: prefer-string-replace-all should be turned on when we drop support for Node 14. - 'unicorn/prefer-string-replace-all': 'off', // TODO @ST-DDT 2023-10-28: The following rule should be turned on when we switch to esm. 'unicorn/prefer-top-level-await': 'off', diff --git a/docs/guide/upgrading_v9/2653.md b/docs/guide/upgrading_v9/2653.md new file mode 100644 index 00000000000..628427a7a67 --- /dev/null +++ b/docs/guide/upgrading_v9/2653.md @@ -0,0 +1,7 @@ +### Examples of code-upgrades that are now used + +_This upgrade is an extension to_ [#2121](./2121.md) + +Used Node >= v15 feature [`replaceAll`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll). + +We could hint users to use polyfill or transpile-down steps. diff --git a/scripts/apidoc/markdown.ts b/scripts/apidoc/markdown.ts index 870b4201136..0b1e3b40ca8 100644 --- a/scripts/apidoc/markdown.ts +++ b/scripts/apidoc/markdown.ts @@ -39,15 +39,15 @@ const htmlSanitizeOptions: sanitizeHtml.IOptions = { function comparableSanitizedHtml(html: string): string { return html - .replace(/&#x[0-9A-F]{2};/g, (x) => + .replaceAll(/&#x[0-9A-F]{2};/g, (x) => String.fromCodePoint(Number.parseInt(x.slice(3, -1), 16)) ) - .replace(/>/g, '>') - .replace(/</g, '<') - .replace(/&/g, '&') - .replace(/"/g, '"') - .replace(/=""/g, '') - .replace(/ /g, ''); + .replaceAll('>', '>') + .replaceAll('<', '<') + .replaceAll('&', '&') + .replaceAll('"', '"') + .replaceAll('=""', '') + .replaceAll(' ', ''); } /** diff --git a/scripts/apidoc/utils.ts b/scripts/apidoc/utils.ts index f26c6cdd5e2..b23f35688ad 100644 --- a/scripts/apidoc/utils.ts +++ b/scripts/apidoc/utils.ts @@ -45,7 +45,7 @@ export const pathOutputDir = resolve(pathDocsDir, 'api'); // Functions export function adjustUrls(description: string): string { - return description.replace(/https:\/\/(next.)?fakerjs.dev\//g, '/'); + return description.replaceAll(/https:\/\/(next.)?fakerjs.dev\//g, '/'); } export function mapByName( @@ -65,7 +65,7 @@ export function mapByName( export function methodDiffHash(method: Method): string { return diffHash({ ...method, - sourcePath: method.sourcePath.replace(/#.*/g, ''), + sourcePath: method.sourcePath.replaceAll(/#.*/g, ''), }); } diff --git a/scripts/apidoc/writer.ts b/scripts/apidoc/writer.ts index 67bf1d7a729..b03cfe85381 100644 --- a/scripts/apidoc/writer.ts +++ b/scripts/apidoc/writer.ts @@ -139,7 +139,7 @@ async function writeApiDocsModulePage( ` ) .join('')} - `.replace(/\n +/g, '\n'); + `.replaceAll(/\n +/g, '\n'); content = vitePressInFileOptions + (await formatMarkdown(content)); diff --git a/scripts/generate-locales.ts b/scripts/generate-locales.ts index c1aea203123..bebfea5fc82 100644 --- a/scripts/generate-locales.ts +++ b/scripts/generate-locales.ts @@ -460,7 +460,7 @@ async function main(): Promise { localizationLocales = await formatMarkdown(localizationLocales); let localizationContent = readFileSync(pathDocsGuideLocalization, 'utf8'); - localizationContent = localizationContent.replace( + localizationContent = localizationContent.replaceAll( /(^$).*(^$)/gms, `$1\n\n\n\n${localizationLocales}\n$2` ); diff --git a/src/modules/color/index.ts b/src/modules/color/index.ts index a9b39377668..673445973c7 100644 --- a/src/modules/color/index.ts +++ b/src/modules/color/index.ts @@ -89,7 +89,7 @@ function toBinary(values: number[]): string { const buffer = new ArrayBuffer(4); new DataView(buffer).setFloat32(0, value); const bytes = new Uint8Array(buffer); - return toBinary([...bytes]).replace(/ /g, ''); + return toBinary([...bytes]).replaceAll(' ', ''); } return (value >>> 0).toString(2).padStart(8, '0'); diff --git a/src/modules/finance/iban.ts b/src/modules/finance/iban.ts index d639404cf9d..b730d3655c8 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.replaceAll(/[A-Z]/gi, (match) => String((match.toUpperCase().codePointAt(0) ?? Number.NaN) - 55) ), }; diff --git a/src/modules/finance/index.ts b/src/modules/finance/index.ts index a19595d333e..4013f99f2b7 100644 --- a/src/modules/finance/index.ts +++ b/src/modules/finance/index.ts @@ -850,7 +850,7 @@ export class FinanceModule extends ModuleBase { format = this.faker.helpers.arrayElement(formats); } - format = format.replace(/\//g, ''); + format = format.replaceAll('/', ''); return this.faker.helpers.replaceCreditCardSymbols(format); } diff --git a/src/modules/git/index.ts b/src/modules/git/index.ts index 89ab4799fef..15cd21a3d69 100644 --- a/src/modules/git/index.ts +++ b/src/modules/git/index.ts @@ -94,7 +94,7 @@ export class GitModule extends ModuleBase { 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.replaceAll(/^[.,:;"\\']|[<>\n]|[.,:;"\\']$/g, ''); lines.push( `Author: ${user} <${email}>`, diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index 635b3e09d4f..73728f78776 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -228,9 +228,9 @@ export class SimpleHelpersModule extends SimpleModuleBase { slugify(string: string = ''): string { return string .normalize('NFKD') //for example è decomposes to as e + ̀ - .replace(/[\u0300-\u036F]/g, '') // removes combining marks - .replace(/ /g, '-') // replaces spaces with hyphens - .replace(/[^\w.-]+/g, ''); // removes all non-word characters except for dots and hyphens + .replaceAll(/[\u0300-\u036F]/g, '') // removes combining marks + .replaceAll(' ', '-') // replaces spaces with hyphens + .replaceAll(/[^\w.-]+/g, ''); // removes all non-word characters except for dots and hyphens } /** @@ -797,7 +797,7 @@ export class SimpleHelpersModule extends SimpleModuleBase { let value = data[p]; if (typeof value === 'string') { // escape $, source: https://stackoverflow.com/a/6969486/6897682 - value = value.replace(/\$/g, '$$$$'); + value = value.replaceAll('$', '$$$$'); str = str.replace(re, value); } else { str = str.replace(re, value); diff --git a/src/modules/helpers/luhn-check.ts b/src/modules/helpers/luhn-check.ts index 42fbe1a5485..fd7d54cc6d3 100644 --- a/src/modules/helpers/luhn-check.ts +++ b/src/modules/helpers/luhn-check.ts @@ -24,7 +24,7 @@ export function luhnCheckValue(str: string): number { * @param str The string to generate the checksum for. */ function luhnChecksum(str: string): number { - str = str.replace(/[\s-]/g, ''); + str = str.replaceAll(/[\s-]/g, ''); let sum = 0; let alternate = false; for (let i = str.length - 1; i >= 0; i--) { diff --git a/src/modules/internet/index.ts b/src/modules/internet/index.ts index 94851a536f3..86b03098a1c 100644 --- a/src/modules/internet/index.ts +++ b/src/modules/internet/index.ts @@ -276,7 +276,7 @@ export class InternetModule extends ModuleBase { 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.replaceAll(/[^A-Za-z0-9._+-]+/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 @@ -291,7 +291,7 @@ export class InternetModule extends ModuleBase { } // local parts may not contain two or more consecutive . characters - localPart = localPart.replace(/\.{2,}/g, '.'); + localPart = localPart.replaceAll(/\.{2,}/g, '.'); // local parts may not start with or end with a . character localPart = localPart.replace(/^\./, ''); @@ -661,7 +661,7 @@ export class InternetModule extends ModuleBase { // First remove simple accents etc result = result .normalize('NFKD') //for example è decomposes to as e + ̀ - .replace(/[\u0300-\u036F]/g, ''); // removes combining marks + .replaceAll(/[\u0300-\u036F]/g, ''); // removes combining marks result = [...result] .map((char) => { @@ -681,8 +681,8 @@ export class InternetModule extends ModuleBase { return charCode.toString(36); }) .join(''); - result = result.toString().replace(/'/g, ''); - result = result.replace(/ /g, ''); + result = result.toString().replaceAll("'", ''); + result = result.replaceAll(' ', ''); return result; } @@ -844,8 +844,8 @@ export class InternetModule extends ModuleBase { break; } - result = result.toString().replace(/'/g, ''); - result = result.replace(/ /g, ''); + result = result.toString().replaceAll("'", ''); + result = result.replaceAll(' ', ''); return result; } diff --git a/src/modules/location/index.ts b/src/modules/location/index.ts index 0c94317bed0..54766d8a7ec 100644 --- a/src/modules/location/index.ts +++ b/src/modules/location/index.ts @@ -178,7 +178,7 @@ export class LocationModule extends ModuleBase { buildingNumber(): string { return this.faker.helpers .arrayElement(this.faker.definitions.location.building_number) - .replace(/#+/g, (m) => + .replaceAll(/#+/g, (m) => this.faker.string.numeric({ length: m.length, allowLeadingZeros: false, @@ -274,7 +274,7 @@ export class LocationModule extends ModuleBase { secondaryAddress(): string { return this.faker.helpers .arrayElement(this.faker.definitions.location.secondary_address) - .replace(/#+/g, (m) => + .replaceAll(/#+/g, (m) => this.faker.string.numeric({ length: m.length, allowLeadingZeros: false, diff --git a/src/modules/string/index.ts b/src/modules/string/index.ts index 6428153d5aa..8b3cbd267e9 100644 --- a/src/modules/string/index.ts +++ b/src/modules/string/index.ts @@ -692,8 +692,8 @@ export class StringModule extends SimpleModuleBase { */ uuid(): string { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx' - .replace(/x/g, () => this.faker.number.hex({ min: 0x0, max: 0xf })) - .replace(/y/g, () => this.faker.number.hex({ min: 0x8, max: 0xb })); + .replaceAll('x', () => this.faker.number.hex({ min: 0x0, max: 0xf })) + .replaceAll('y', () => this.faker.number.hex({ min: 0x8, max: 0xb })); } /** diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts index 52fc14f5bca..ee0d2600fb7 100644 --- a/src/modules/system/index.ts +++ b/src/modules/system/index.ts @@ -72,7 +72,10 @@ export class SystemModule extends ModuleBase { ): string { const { extensionCount = 1 } = options; - const baseName = this.faker.word.words().toLowerCase().replace(/\W/g, '_'); + const baseName = this.faker.word + .words() + .toLowerCase() + .replaceAll(/\W/g, '_'); const extensionsStr = this.faker.helpers .multiple(() => this.fileExt(), { count: extensionCount }) diff --git a/test/modules/finance.spec.ts b/test/modules/finance.spec.ts index 322e3ba91df..e3ec6dd8496 100644 --- a/test/modules/finance.spec.ts +++ b/test/modules/finance.spec.ts @@ -427,7 +427,7 @@ describe('finance', () => { describe('creditCardNumber()', () => { it('should return a random credit card number', () => { let number = faker.finance.creditCardNumber(); - number = number.replace(/\D/g, ''); // remove formatting + number = number.replaceAll(/\D/g, ''); // remove formatting expect(number.length).toBeGreaterThanOrEqual(13); expect(number.length).toBeLessThanOrEqual(20); diff --git a/test/modules/system.spec.ts b/test/modules/system.spec.ts index 70a53266000..7b0f1086f30 100644 --- a/test/modules/system.spec.ts +++ b/test/modules/system.spec.ts @@ -384,7 +384,7 @@ describe('system', () => { 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}|\*)?/; - const regexElements = regex.toString().replace(/\//g, '').split(' '); + const regexElements = regex.toString().replaceAll('/', '').split(' '); it.each([ [{}, 5], diff --git a/test/scripts/apidoc/verify-jsdoc-tags.spec.ts b/test/scripts/apidoc/verify-jsdoc-tags.spec.ts index 7d6d8554a72..42882474ccf 100644 --- a/test/scripts/apidoc/verify-jsdoc-tags.spec.ts +++ b/test/scripts/apidoc/verify-jsdoc-tags.spec.ts @@ -253,7 +253,7 @@ describe('verify JSDoc tags', () => { ).parameters) { const { name, description } = param; const plainDescription = description - .replace(/<[^>]+>/g, '') + .replaceAll(/<[^>]+>/g, '') .trim(); expect( plainDescription,