aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShinigami <[email protected]>2024-02-18 23:56:40 +0100
committerGitHub <[email protected]>2024-02-18 23:56:40 +0100
commit52b8992cbf960e001336d59b83c610845ff35860 (patch)
treec71db76f3f955a74be4cbcbaa56df77375cb4531
parentec5609b18c79ea9fc8183ae78e917801e6a8a3f4 (diff)
downloadfaker-52b8992cbf960e001336d59b83c610845ff35860.tar.xz
faker-52b8992cbf960e001336d59b83c610845ff35860.zip
infra(unicorn): prefer-string-replace-all (#2653)
-rw-r--r--.eslintrc.cjs2
-rw-r--r--docs/guide/upgrading_v9/2653.md7
-rw-r--r--scripts/apidoc/markdown.ts14
-rw-r--r--scripts/apidoc/utils.ts4
-rw-r--r--scripts/apidoc/writer.ts2
-rw-r--r--scripts/generate-locales.ts2
-rw-r--r--src/modules/color/index.ts2
-rw-r--r--src/modules/finance/iban.ts2
-rw-r--r--src/modules/finance/index.ts2
-rw-r--r--src/modules/git/index.ts2
-rw-r--r--src/modules/helpers/index.ts8
-rw-r--r--src/modules/helpers/luhn-check.ts2
-rw-r--r--src/modules/internet/index.ts14
-rw-r--r--src/modules/location/index.ts4
-rw-r--r--src/modules/string/index.ts4
-rw-r--r--src/modules/system/index.ts5
-rw-r--r--test/modules/finance.spec.ts2
-rw-r--r--test/modules/system.spec.ts2
-rw-r--r--test/scripts/apidoc/verify-jsdoc-tags.spec.ts2
19 files changed, 45 insertions, 37 deletions
diff --git a/.eslintrc.cjs b/.eslintrc.cjs
index bf6789c6..287c1fb4 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 00000000..628427a7
--- /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 870b4201..0b1e3b40 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(/&gt;/g, '>')
- .replace(/&lt;/g, '<')
- .replace(/&amp;/g, '&')
- .replace(/&quot;/g, '"')
- .replace(/=""/g, '')
- .replace(/ /g, '');
+ .replaceAll('&gt;', '>')
+ .replaceAll('&lt;', '<')
+ .replaceAll('&amp;', '&')
+ .replaceAll('&quot;', '"')
+ .replaceAll('=""', '')
+ .replaceAll(' ', '');
}
/**
diff --git a/scripts/apidoc/utils.ts b/scripts/apidoc/utils.ts
index f26c6cdd..b23f3568 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<TInput extends { name: string }, TValue>(
@@ -65,7 +65,7 @@ export function mapByName<TInput extends { name: string }, TValue>(
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 67bf1d7a..b03cfe85 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 c1aea203..bebfea5f 100644
--- a/scripts/generate-locales.ts
+++ b/scripts/generate-locales.ts
@@ -460,7 +460,7 @@ async function main(): Promise<void> {
localizationLocales = await formatMarkdown(localizationLocales);
let localizationContent = readFileSync(pathDocsGuideLocalization, 'utf8');
- localizationContent = localizationContent.replace(
+ localizationContent = localizationContent.replaceAll(
/(^<!-- LOCALES-AUTO-GENERATED-START -->$).*(^<!-- LOCALES-AUTO-GENERATED-END -->$)/gms,
`$1\n\n<!-- Run '${scriptCommand}' to update. -->\n\n${localizationLocales}\n$2`
);
diff --git a/src/modules/color/index.ts b/src/modules/color/index.ts
index a9b39377..67344597 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 d639404c..b730d365 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 a19595d3..4013f99f 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 89ab4799..15cd21a3 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 635b3e09..73728f78 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 42fbe1a5..fd7d54cc 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 94851a53..86b03098 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 0c94317b..54766d8a 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 6428153d..8b3cbd26 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 52fc14f5..ee0d2600 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 322e3ba9..e3ec6dd8 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 70a53266..7b0f1086 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 7d6d8554..42882474 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,