Skip to content

Commit

Permalink
Remove some moot code (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyten authored May 14, 2024
1 parent 348d960 commit 1cc358b
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 4 deletions.
5 changes: 2 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const format = (open, close) => {

const openCode = `\u001B[${open}m`;
const closeCode = `\u001B[${close}m`;
const resetCode = `${closeCode}${openCode}`;

return input => {
const string = input + ''; // eslint-disable-line no-implicit-coercion -- This is faster.
Expand All @@ -25,13 +24,13 @@ const format = (open, close) => {
// Handle nested colors.

// We could have done this, but it's too slow (as of Node.js 22).
// return openCode + string.replaceAll(closeCode, resetCode) + closeCode;
// return openCode + string.replaceAll(closeCode, openCode) + closeCode;

let result = openCode;
let lastIndex = 0;

while (index !== -1) {
result += string.slice(lastIndex, index) + resetCode;
result += string.slice(lastIndex, index) + openCode;
lastIndex = index + closeCode.length;
index = string.indexOf(closeCode, lastIndex);
}
Expand Down
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ test('Is noop when no colors are supported', async t => {

test('Nested colors are handled properly', t => {
const redText = colors.red(`Error: ${colors.yellow('Warning')} continues in red`);
t.is(redText, '\u001B[31mError: \u001B[33mWarning\u001B[39m\u001B[31m continues in red\u001B[39m');
t.is(redText, '\u001B[31mError: \u001B[33mWarning\u001B[31m continues in red\u001B[39m');
});

0 comments on commit 1cc358b

Please sign in to comment.