From 0844b8888bb7248522dfc15594170ba0b75b1add Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Tue, 2 Apr 2024 23:58:14 +0300 Subject: [PATCH] fix: restore pieces pads in `normalizeMultilinePieces` (#766) --- src/util.ts | 13 +++++++------ test/util.test.js | 8 ++++++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/util.ts b/src/util.ts index 8fcd79a753..f16f17bb04 100644 --- a/src/util.ts +++ b/src/util.ts @@ -24,18 +24,19 @@ export function isString(obj: any) { return typeof obj === 'string' } +const pad = (v: string) => (v === ' ' ? ' ' : '') + export function normalizeMultilinePieces( pieces: TemplateStringsArray ): TemplateStringsArray { return Object.assign( pieces.map((p, i) => p.trim() - ? parseLine(p) - .words.map(({ w, e }) => { - if (w === '\\') return '' - return w.trim() + (p[e + 1] === ' ' ? ' ' : '') - }) - .join(' ') + ? pad(p[0]) + + parseLine(p) + .words.map(({ w }) => (w === '\\' ? '' : w.trim())) + .join(' ') + + pad(p[p.length - 1]) : pieces[i] ), { raw: pieces.raw } diff --git a/test/util.test.js b/test/util.test.js index ab061db682..ae07419c14 100644 --- a/test/util.test.js +++ b/test/util.test.js @@ -24,6 +24,7 @@ import { quote, quotePowerShell, randomId, + normalizeMultilinePieces, getCallerLocationFromString, } from '../build/util.js' @@ -92,6 +93,13 @@ describe('util', () => { "$ \u001b[93m$\u001b[39m\u001b[93m'\u001b[39m\u001b[93m\\\u001b[39m\u001b[93m'\u001b[39m\u001b[93m'\u001b[39m\n" ) }) + + test('normalizeMultilinePieces()', () => { + assert.equal( + normalizeMultilinePieces([' a ', 'b c d', ' e']).join(','), + ' a ,b c d, e' + ) + }) }) test('getCallerLocation: empty', () => {