summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsevenwithawp <sevenwithawp@gmail.com>2022-07-09 21:28:02 +0300
committerGitHub <noreply@github.com>2022-07-09 14:28:02 -0400
commit213d831ae3403402d55d3d084b2434c3ba8da70f (patch)
treed693407f283d97ec2311621a1906bf08ec13226a
parent132c761e87679aec52b9ed8324ee2a4b00705620 (diff)
refactor(ext) Decrease of StringPrototypeReplace recurrent usage (#15058)
Co-authored-by: Bartek IwaƄczuk <biwanczuk@gmail.com>
-rw-r--r--ext/console/02_console.js31
-rw-r--r--ext/fetch/21_formdata.js25
2 files changed, 24 insertions, 32 deletions
diff --git a/ext/console/02_console.js b/ext/console/02_console.js
index b98a4a1ba..0f7b1a8db 100644
--- a/ext/console/02_console.js
+++ b/ext/console/02_console.js
@@ -720,27 +720,20 @@
// Replace escape sequences that can modify output.
function replaceEscapeSequences(string) {
+ const escapeMap = {
+ "\b": "\\b",
+ "\f": "\\f",
+ "\n": "\\n",
+ "\r": "\\r",
+ "\t": "\\t",
+ "\v": "\\v",
+ };
+
return StringPrototypeReplace(
StringPrototypeReplace(
- StringPrototypeReplace(
- StringPrototypeReplace(
- StringPrototypeReplace(
- StringPrototypeReplace(
- StringPrototypeReplace(string, /[\b]/g, "\\b"),
- /\f/g,
- "\\f",
- ),
- /\n/g,
- "\\n",
- ),
- /\r/g,
- "\\r",
- ),
- /\t/g,
- "\\t",
- ),
- /\v/g,
- "\\v",
+ string,
+ /([\b\f\n\r\t\v])/g,
+ (c) => escapeMap[c],
),
// deno-lint-ignore no-control-regex
/[\x00-\x1f\x7f-\x9f]/g,
diff --git a/ext/fetch/21_formdata.js b/ext/fetch/21_formdata.js
index a134fe5f7..72f83a860 100644
--- a/ext/fetch/21_formdata.js
+++ b/ext/fetch/21_formdata.js
@@ -271,20 +271,19 @@
webidl.configurePrototype(FormData);
const FormDataPrototype = FormData.prototype;
- const escape = (str, isFilename) =>
- StringPrototypeReplace(
- StringPrototypeReplace(
- StringPrototypeReplace(
- isFilename ? str : StringPrototypeReplace(str, /\r?\n|\r/g, "\r\n"),
- /\n/g,
- "%0A",
- ),
- /\r/g,
- "%0D",
- ),
- /"/g,
- "%22",
+ const escape = (str, isFilename) => {
+ const escapeMap = {
+ "\n": "%0A",
+ "\r": "%0D",
+ '"': "%22",
+ };
+
+ return StringPrototypeReplace(
+ isFilename ? str : StringPrototypeReplace(str, /\r?\n|\r/g, "\r\n"),
+ /([\n\r"])/g,
+ (c) => escapeMap[c],
);
+ };
/**
* convert FormData to a Blob synchronous without reading all of the files