Skip to content

Commit

Permalink
refactor(ext) Decrease of StringPrototypeReplace recurrent usage (den…
Browse files Browse the repository at this point in the history
…oland#15058)

Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
  • Loading branch information
sevenwithawp and bartlomieju authored Jul 9, 2022
1 parent 132c761 commit 213d831
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 32 deletions.
31 changes: 12 additions & 19 deletions ext/console/02_console.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
25 changes: 12 additions & 13 deletions ext/fetch/21_formdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 213d831

Please sign in to comment.