From 213d831ae3403402d55d3d084b2434c3ba8da70f Mon Sep 17 00:00:00 2001 From: sevenwithawp Date: Sat, 9 Jul 2022 21:28:02 +0300 Subject: refactor(ext) Decrease of StringPrototypeReplace recurrent usage (#15058) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bartek IwaƄczuk --- ext/console/02_console.js | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) (limited to 'ext/console/02_console.js') 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, -- cgit v1.2.3