From 15aa3f36817321bf058397b05864b6ec3db5bf25 Mon Sep 17 00:00:00 2001 From: ademar_sj Date: Sun, 12 Feb 2023 22:46:16 -0300 Subject: [PATCH 1/3] feat: dont print escape backslash on strings containing escaped backslashes --- lib/utils.js | 4 ++++ test/lib/utils.public.test.js | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/lib/utils.js b/lib/utils.js index d8aa3cc2..05a8a7b5 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -403,6 +403,8 @@ function prettifyObject ({ // Stringify the entire object as a single JSON line if (Object.keys(plain).length > 0) { result += colorizer.greyMessage(stringifySafe(plain)) + // Avoid printing the escape character on escapting backslash + ?.replaceAll('\\\\', '\\') } result += eol } else { @@ -412,6 +414,8 @@ function prettifyObject ({ const lines = typeof customPrettifiers[keyName] === 'function' ? keyValue : stringifySafe(keyValue, null, 2) + // Avoid printing the escape character on escapting backslash + ?.replaceAll('\\\\', '\\') if (lines === undefined) return diff --git a/test/lib/utils.public.test.js b/test/lib/utils.public.test.js index ee7b0fa1..192b405a 100644 --- a/test/lib/utils.public.test.js +++ b/test/lib/utils.public.test.js @@ -270,6 +270,11 @@ tap.test('prettifyObject', t => { t.equal(str, ' foo: "bar"\n') }) + t.test('ignores escape backslashs in string values', async t => { + const str = prettifyObject({ input: { foo_regexp: '\\[^\\w\\s]\\' } }) + t.equal(str, ' foo_regexp: "\\[^\\w\\s]\\"\n') + }) + t.test('works with error props', async t => { const err = Error('Something went wrong') const serializedError = { From 739846916b764408aa3f1634396f9472334b56af Mon Sep 17 00:00:00 2001 From: ademarsj Date: Mon, 13 Feb 2023 13:20:25 -0300 Subject: [PATCH 2/3] fix: changing replace to work on node 14 --- lib/utils.js | 10 +++++----- test/lib/utils.public.test.js | 5 +++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 05a8a7b5..28cc7f7d 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -403,21 +403,21 @@ function prettifyObject ({ // Stringify the entire object as a single JSON line if (Object.keys(plain).length > 0) { result += colorizer.greyMessage(stringifySafe(plain)) - // Avoid printing the escape character on escapting backslash - ?.replaceAll('\\\\', '\\') } + // Avoid printing the escape character on escapting backslash result += eol + result = result.replace(/\\\\/gi, '\\') } else { // Put each object entry on its own line Object.entries(plain).forEach(([keyName, keyValue]) => { // custom prettifiers are already applied above, so we can skip it now - const lines = typeof customPrettifiers[keyName] === 'function' + let lines = typeof customPrettifiers[keyName] === 'function' ? keyValue : stringifySafe(keyValue, null, 2) - // Avoid printing the escape character on escapting backslash - ?.replaceAll('\\\\', '\\') if (lines === undefined) return + // Avoid printing the escape character on escapting backslash + lines = lines.replace(/\\\\/gi, '\\') const joinedLines = joinLinesWithIndentation({ input: lines, ident, eol }) result += `${ident}${keyName}:${joinedLines.startsWith(eol) ? '' : ' '}${joinedLines}${eol}` diff --git a/test/lib/utils.public.test.js b/test/lib/utils.public.test.js index 192b405a..2c47c7db 100644 --- a/test/lib/utils.public.test.js +++ b/test/lib/utils.public.test.js @@ -275,6 +275,11 @@ tap.test('prettifyObject', t => { t.equal(str, ' foo_regexp: "\\[^\\w\\s]\\"\n') }) + t.test('ignores escape backslashs in string values (singleLine option)', async t => { + const str = prettifyObject({ input: { foo_regexp: '\\[^\\w\\s]\\' }, singleLine: true }) + t.equal(str, '{"foo_regexp":"\\[^\\w\\s]\\"}\n') + }) + t.test('works with error props', async t => { const err = Error('Something went wrong') const serializedError = { From e3a750a673b922cb2c3c9edd917e49f890f6532a Mon Sep 17 00:00:00 2001 From: ademar_sj Date: Wed, 15 Feb 2023 21:45:36 -0300 Subject: [PATCH 3/3] fix: grammar issues --- lib/utils.js | 5 +++-- test/lib/utils.public.test.js | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 28cc7f7d..01335136 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -404,8 +404,8 @@ function prettifyObject ({ if (Object.keys(plain).length > 0) { result += colorizer.greyMessage(stringifySafe(plain)) } - // Avoid printing the escape character on escapting backslash result += eol + // Avoid printing the escape character on escaped backslashes. result = result.replace(/\\\\/gi, '\\') } else { // Put each object entry on its own line @@ -416,7 +416,8 @@ function prettifyObject ({ : stringifySafe(keyValue, null, 2) if (lines === undefined) return - // Avoid printing the escape character on escapting backslash + + // Avoid printing the escape character on escaped backslashes. lines = lines.replace(/\\\\/gi, '\\') const joinedLines = joinLinesWithIndentation({ input: lines, ident, eol }) diff --git a/test/lib/utils.public.test.js b/test/lib/utils.public.test.js index 2c47c7db..c6b20e2c 100644 --- a/test/lib/utils.public.test.js +++ b/test/lib/utils.public.test.js @@ -270,12 +270,12 @@ tap.test('prettifyObject', t => { t.equal(str, ' foo: "bar"\n') }) - t.test('ignores escape backslashs in string values', async t => { + t.test('ignores escaped backslashes in string values', async t => { const str = prettifyObject({ input: { foo_regexp: '\\[^\\w\\s]\\' } }) t.equal(str, ' foo_regexp: "\\[^\\w\\s]\\"\n') }) - t.test('ignores escape backslashs in string values (singleLine option)', async t => { + t.test('ignores escaped backslashes in string values (singleLine option)', async t => { const str = prettifyObject({ input: { foo_regexp: '\\[^\\w\\s]\\' }, singleLine: true }) t.equal(str, '{"foo_regexp":"\\[^\\w\\s]\\"}\n') })