From 992ea4af5a7dcefb6f4fb95b2decaa90f14fe8a9 Mon Sep 17 00:00:00 2001 From: Ruslan Lopatin Date: Mon, 2 Aug 2021 15:55:44 +0700 Subject: [PATCH] `logline`: Report plain separated values as is --- src/loggable/logline.spec.ts | 12 +++++ src/loggable/logline.ts | 85 +++++++++++++++++++----------------- 2 files changed, 57 insertions(+), 40 deletions(-) diff --git a/src/loggable/logline.spec.ts b/src/loggable/logline.spec.ts index 514d8a0..45ef66b 100644 --- a/src/loggable/logline.spec.ts +++ b/src/loggable/logline.spec.ts @@ -10,6 +10,18 @@ describe('logline', () => { toLog: () => [], }; + it('reports single value as is', () => { + expect(log(logline`${1}`)).toEqual([1]); + }); + it('reports leading value as is', () => { + expect(log(logline`${1} suffix`)).toEqual([1, 'suffix']); + }); + it('reports trailing value as is', () => { + expect(log(logline`prefix ${1}`)).toEqual(['prefix', 1]); + }); + it('reports separated values as is', () => { + expect(log(logline`${1} ${2} ${3} ${4}`)).toEqual([1, 2, 3, 4]); + }); it('joins non-separate values', () => { expect(log(logline`1${2}3${4}5`)).toEqual(['12345']); }); diff --git a/src/loggable/logline.ts b/src/loggable/logline.ts index 981d12e..0afd968 100644 --- a/src/loggable/logline.ts +++ b/src/loggable/logline.ts @@ -12,51 +12,56 @@ const logline$append = (result: unknown[], str: string): void => { } }; const logline$join = (result: unknown[], joins: unknown[]): void => { - if (joins.length) { - - const loggable: Loggable = { - toLog(target: DueLog): string | void { - - // Remember the containing log line and position. - const { on = 'out', line, index } = target; - - // The loggable to apply after joining. - const endJoin: Loggable = { - toLog(target) { - // Read the join results. - joins = target.line.slice(0, -1); // The last element is this loggable. + if (!joins.length) { + return; + } + if (joins.length === 1) { + result.push(joins[0]); + return; + } - if (!joins.length) { - // Nothing left to join. - line.splice(index, 1); - // Restore containing log line with `logline` removed. - target.line = line; - // Go on from the same position. - target.index = index; + const loggable: Loggable = { + toLog(target: DueLog): string | void { - return; - } + // Remember the containing log line and position. + const { on = 'out', line, index } = target; - if (on === 'out') { - // Finally join into a string. - line[index] = joins.join(''); - } + // The loggable to apply after joining. + const endJoin: Loggable = { + toLog(target) { + // Read the join results. + joins = target.line.slice(0, -1); // The last element is this loggable. - // Restore containing log line. + if (!joins.length) { + // Nothing left to join. + line.splice(index, 1); + // Restore containing log line with `logline` removed. target.line = line; - // Go on from the next element to avoid immediate re-processing. - target.index = index + 1; - }, - }; - - // Initiate joining by replacing a log line to process. - target.line = [...joins, /* resumes normal processing */ endJoin]; - target.index = 0; - }, - }; - - result.push(loggable); - } + // Go on from the same position. + target.index = index; + + return; + } + + if (on === 'out') { + // Finally join into a string. + line[index] = joins.join(''); + } + + // Restore containing log line. + target.line = line; + // Go on from the next element to avoid immediate re-processing. + target.index = index + 1; + }, + }; + + // Initiate joining by replacing a log line to process. + target.line = [...joins, /* resumes normal processing */ endJoin]; + target.index = 0; + }, + }; + + result.push(loggable); }; /**