Skip to content

Commit

Permalink
Fix weird null issue (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
schani authored and sindresorhus committed Oct 16, 2018
1 parent c7ef178 commit e41217c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = (str, begin, end) => {
end = typeof end === 'number' ? end : arr.length;

let insideEscape = false;
let escapeCode;
let escapeCode = null;
let visible = 0;
let output = '';

Expand All @@ -44,10 +44,10 @@ module.exports = (str, begin, end) => {

if (visible > begin && visible <= end) {
output += x;
} else if (visible === begin && !insideEscape && escapeCode !== undefined && escapeCode !== END_CODE) {
} else if (visible === begin && !insideEscape && escapeCode !== null && escapeCode !== END_CODE) {
output += wrapAnsi(escapeCode);
} else if (visible >= end) {
if (escapeCode !== undefined) {
if (escapeCode !== null) {
output += wrapAnsi(ansiStyles.codes.get(parseInt(escapeCode, 10)) || END_CODE);
}
break;
Expand Down
6 changes: 6 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,9 @@ test.failing('can slice a normal character before a colored character', t => {
test.failing('can slice a normal character after a colored character', t => {
t.is(m('\u001B[31ma\u001B[39mb', 1, 2), 'b');
});

test('weird null issue', t => {
const s = '\u001B[1mautotune.flipCoin("easy as") ? 🎂 : 🍰 \u001B[33m★\u001B[39m\u001B[22m';
const result = m(s, 38);
t.is(result.search('null'), -1);
});

0 comments on commit e41217c

Please sign in to comment.