Skip to content

Commit

Permalink
Handle empty strings (#8)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
hsablonniere and sindresorhus committed Mar 19, 2020
1 parent ad032d3 commit cb19fd4
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 @@ -2,6 +2,12 @@
const stripAnsi = require('strip-ansi');
const charRegex = require('char-regex');

const stringLength = string => stripAnsi(string).match(charRegex()).length;
const stringLength = string => {
if (string === '') {
return 0;
}

return stripAnsi(string).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 @@ -2,6 +2,7 @@ const test = require('ava');
const stringLength = require('.');

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

0 comments on commit cb19fd4

Please sign in to comment.