Skip to content

Commit

Permalink
Correctly handle zero-length ANSI string (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin940726 committed Mar 17, 2021
1 parent 47a0be0 commit 32de93a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ const stringLength = string => {
return 0;
}

return stripAnsi(string).match(charRegex()).length;
const strippedString = stripAnsi(string);

if (strippedString === '') {
return 0;
}

return strippedString.match(charRegex()).length;
};

module.exports = stringLength;
1 change: 1 addition & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const stringLength = require('.');

test('get the real length of a string', t => {
t.is(stringLength(''), 0);
t.is(stringLength('\u001B[1m\u001B[22m'), 0);
t.is(stringLength('𠀔'), 1);
t.is(stringLength('foo𠁐bar𠀃'), 8);
t.is(stringLength('あ'), 1);
Expand Down

0 comments on commit 32de93a

Please sign in to comment.