Skip to content

Commit 32de93a

Browse files
authored
Correctly handle zero-length ANSI string (#15)
1 parent 47a0be0 commit 32de93a

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ const stringLength = string => {
77
return 0;
88
}
99

10-
return stripAnsi(string).match(charRegex()).length;
10+
const strippedString = stripAnsi(string);
11+
12+
if (strippedString === '') {
13+
return 0;
14+
}
15+
16+
return strippedString.match(charRegex()).length;
1117
};
1218

1319
module.exports = stringLength;

test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const stringLength = require('.');
33

44
test('get the real length of a string', t => {
55
t.is(stringLength(''), 0);
6+
t.is(stringLength('\u001B[1m\u001B[22m'), 0);
67
t.is(stringLength('𠀔'), 1);
78
t.is(stringLength('foo𠁐bar𠀃'), 8);
89
t.is(stringLength('あ'), 1);

0 commit comments

Comments
 (0)