Skip to content

Commit

Permalink
logline: Report plain separated values as is
Browse files Browse the repository at this point in the history
  • Loading branch information
surol committed Aug 2, 2021
1 parent e759732 commit 992ea4a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 40 deletions.
12 changes: 12 additions & 0 deletions src/loggable/logline.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
});
Expand Down
85 changes: 45 additions & 40 deletions src/loggable/logline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

/**
Expand Down

0 comments on commit 992ea4a

Please sign in to comment.