refactor: use `replaceAll()` instead of `replace()` when appropriate (#39721)

refactor: use replaceAll() instead of replace() when appropriate
This commit is contained in:
Milan Burda 2023-09-07 08:50:14 +02:00 коммит произвёл GitHub
Родитель 029127a8b6
Коммит f6e8544ef6
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
10 изменённых файлов: 20 добавлений и 20 удалений

Просмотреть файл

@ -33,7 +33,7 @@ const normalizeAccessKey = (text: string) => {
// macOS does not have access keys so remove single ampersands
// and replace double ampersands with a single ampersand
if (process.platform === 'darwin') {
return text.replace(/&(&?)/g, '$1');
return text.replaceAll(/&(&?)/g, '$1');
}
// Linux uses a single underscore as an access key prefix so escape
@ -41,7 +41,7 @@ const normalizeAccessKey = (text: string) => {
// ampersands with a single ampersand, and replace a single ampersand with
// a single underscore
if (process.platform === 'linux') {
return text.replace(/_/g, '__').replace(/&(.?)/g, (match, after) => {
return text.replaceAll('_', '__').replaceAll(/&(.?)/g, (match, after) => {
if (after === '&') return after;
return `_${after}`;
});

Просмотреть файл

@ -63,8 +63,8 @@ if (process.platform === 'win32') {
if (fs.existsSync(updateDotExe)) {
const packageDir = path.dirname(path.resolve(updateDotExe));
const packageName = path.basename(packageDir).replace(/\s/g, '');
const exeName = path.basename(process.execPath).replace(/\.exe$/i, '').replace(/\s/g, '');
const packageName = path.basename(packageDir).replaceAll(/\s/g, '');
const exeName = path.basename(process.execPath).replace(/\.exe$/i, '').replaceAll(/\s/g, '');
app.setAppUserModelId(`com.squirrel.${packageName}.${exeName}`);
}

Просмотреть файл

@ -3425,7 +3425,7 @@ describe('BrowserWindow module', () => {
w.loadURL(pageUrl);
const [, url] = await once(ipcMain, 'answer');
const expectedUrl = process.platform === 'win32'
? 'file:///' + htmlPath.replace(/\\/g, '/')
? 'file:///' + htmlPath.replaceAll('\\', '/')
: pageUrl;
expect(url).to.equal(expectedUrl);
});
@ -3475,7 +3475,7 @@ describe('BrowserWindow module', () => {
w.loadURL(pageUrl);
const [, { url, frameName, options }] = await once(w.webContents, 'did-create-window') as [BrowserWindow, Electron.DidCreateWindowDetails];
const expectedUrl = process.platform === 'win32'
? 'file:///' + htmlPath.replace(/\\/g, '/')
? 'file:///' + htmlPath.replaceAll('\\', '/')
: pageUrl;
expect(url).to.equal(expectedUrl);
expect(frameName).to.equal('popup!');

Просмотреть файл

@ -111,7 +111,7 @@ describe('debugger module', () => {
it('fires message event', async () => {
const url = process.platform !== 'win32'
? `file://${path.join(fixtures, 'pages', 'a.html')}`
: `file:///${path.join(fixtures, 'pages', 'a.html').replace(/\\/g, '/')}`;
: `file:///${path.join(fixtures, 'pages', 'a.html').replaceAll('\\', '/')}`;
w.webContents.loadURL(url);
w.webContents.debugger.attach();
const message = emittedUntil(w.webContents.debugger, 'message',

Просмотреть файл

@ -1128,7 +1128,7 @@ describe('protocol module', () => {
protocol.handle('file', (req) => {
let file;
if (process.platform === 'win32') {
file = `file:///${filePath.replace(/\\/g, '/')}`;
file = `file:///${filePath.replaceAll('\\', '/')}`;
} else {
file = `file://${filePath}`;
}

Просмотреть файл

@ -237,7 +237,7 @@ describe('session module', () => {
appProcess.stdout.on('data', data => { output += data; });
appProcess.on('exit', () => {
resolve(output.replace(/(\r\n|\n|\r)/gm, ''));
resolve(output.replaceAll(/(\r\n|\n|\r)/gm, ''));
});
});
};

Просмотреть файл

@ -181,7 +181,7 @@ describe('webRequest module', () => {
callback({ cancel: true });
});
const fileURL = url.format({
pathname: path.join(fixturesPath, 'blank.html').replace(/\\/g, '/'),
pathname: path.join(fixturesPath, 'blank.html').replaceAll('\\', '/'),
protocol: 'file',
slashes: true
});
@ -395,7 +395,7 @@ describe('webRequest module', () => {
onSendHeadersCalled = true;
});
await ajax(url.format({
pathname: path.join(fixturesPath, 'blank.html').replace(/\\/g, '/'),
pathname: path.join(fixturesPath, 'blank.html').replaceAll('\\', '/'),
protocol: 'file',
slashes: true
}));

Просмотреть файл

@ -90,7 +90,7 @@ describe('asar package', () => {
await w.loadFile(path.join(fixtures, 'workers', 'load_worker.html'));
const workerUrl = url.format({
pathname: path.resolve(fixtures, 'workers', 'workers.asar', 'worker.js').replace(/\\/g, '/'),
pathname: path.resolve(fixtures, 'workers', 'workers.asar', 'worker.js').replaceAll('\\', '/'),
protocol: 'file',
slashes: true
});
@ -103,7 +103,7 @@ describe('asar package', () => {
await w.loadFile(path.join(fixtures, 'workers', 'load_shared_worker.html'));
const workerUrl = url.format({
pathname: path.resolve(fixtures, 'workers', 'workers.asar', 'shared_worker.js').replace(/\\/g, '/'),
pathname: path.resolve(fixtures, 'workers', 'workers.asar', 'shared_worker.js').replaceAll('\\', '/'),
protocol: 'file',
slashes: true
});
@ -1243,7 +1243,7 @@ describe('asar package', function () {
const echo = path.join(asarDir, 'echo.asar', 'echo');
const stdout = await promisify(require(childProcess).exec)('echo ' + echo + ' foo bar');
expect(stdout.toString().replace(/\r/g, '')).to.equal(echo + ' foo bar\n');
expect(stdout.toString().replaceAll('\r', '')).to.equal(echo + ' foo bar\n');
}, [childProcess]);
});
@ -1252,7 +1252,7 @@ describe('asar package', function () {
const echo = path.join(asarDir, 'echo.asar', 'echo');
const stdout = require(childProcess).execSync('echo ' + echo + ' foo bar');
expect(stdout.toString().replace(/\r/g, '')).to.equal(echo + ' foo bar\n');
expect(stdout.toString().replaceAll('\r', '')).to.equal(echo + ' foo bar\n');
}, [childProcess]);
});

Просмотреть файл

@ -284,7 +284,7 @@ describe('web security', () => {
describe('accessing file://', () => {
async function loadFile (w: BrowserWindow) {
const thisFile = url.format({
pathname: __filename.replace(/\\/g, '/'),
pathname: __filename.replaceAll('\\', '/'),
protocol: 'file',
slashes: true
});
@ -461,7 +461,7 @@ describe('command line switches', () => {
throw new Error(`Process exited with code "${code}" signal "${signal}" output "${output}" stderr "${stderr}"`);
}
output = output.replace(/(\r\n|\n|\r)/gm, '');
output = output.replaceAll(/(\r\n|\n|\r)/gm, '');
expect(output).to.equal(result);
};
@ -1050,7 +1050,7 @@ describe('chromium features', () => {
it('defines a window.location getter', async () => {
let targetURL: string;
if (process.platform === 'win32') {
targetURL = `file:///${fixturesPath.replace(/\\/g, '/')}/pages/base-page.html`;
targetURL = `file:///${fixturesPath.replaceAll('\\', '/')}/pages/base-page.html`;
} else {
targetURL = `file://${fixturesPath}/pages/base-page.html`;
}
@ -1977,7 +1977,7 @@ describe('chromium features', () => {
ifdescribe(features.isPDFViewerEnabled())('PDF Viewer', () => {
const pdfSource = url.format({
pathname: path.join(__dirname, 'fixtures', 'cat.pdf').replace(/\\/g, '/'),
pathname: path.join(__dirname, 'fixtures', 'cat.pdf').replaceAll('\\', '/'),
protocol: 'file',
slashes: true
});

Просмотреть файл

@ -249,7 +249,7 @@ describe('<webview> tag', function () {
});
await w.loadURL('about:blank');
const src = url.format({
pathname: `${fixtures.replace(/\\/g, '/')}/pages/theme-color.html`,
pathname: `${fixtures.replaceAll('\\', '/')}/pages/theme-color.html`,
protocol: 'file',
slashes: true
});