Skip to content

Commit 75cabbe

Browse files
committed
fix: handle 8-bit CSI introducer in fast path guard
1 parent d019f7f commit 75cabbe

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

‎index.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ export default function stripAnsi(string) {
77
throw new TypeError(`Expected a \`string\`, got \`${typeof string}\``);
88
}
99

10-
// Fast path: if no ESC byte exists, there are no ANSI codes
11-
if (!string.includes('\u001B')) {
10+
// Fast path: ANSI codes require ESC (7-bit) or CSI (8-bit) introducer
11+
if (!string.includes('\u001B') && !string.includes('\u009B')) {
1212
return string;
1313
}
1414

‎test.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,15 @@ test('strip reset;setfg;setbg;italics;strike;underline sequence from string', t
1616
test('strip link from terminal link', t => {
1717
t.is(stripAnsi('\u001B]8;;https://github.com\u0007click\u001B]8;;\u0007'), 'click');
1818
});
19+
20+
test('strip color from string using 8-bit CSI introducer', t => {
21+
t.is(stripAnsi('\u009B31mfoo\u009B39m'), 'foo');
22+
});
23+
24+
test('return string as-is if no ANSI codes', t => {
25+
t.is(stripAnsi('foo bar'), 'foo bar');
26+
});
27+
28+
test('return empty string as-is', t => {
29+
t.is(stripAnsi(''), '');
30+
});

0 commit comments

Comments
 (0)