Skip to content

Commit cb19fd4

Browse files
Handle empty strings (#8)
Co-authored-by: Sindre Sorhus <[email protected]>
1 parent ad032d3 commit cb19fd4

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
@@ -2,6 +2,12 @@
22
const stripAnsi = require('strip-ansi');
33
const charRegex = require('char-regex');
44

5-
const stringLength = string => stripAnsi(string).match(charRegex()).length;
5+
const stringLength = string => {
6+
if (string === '') {
7+
return 0;
8+
}
9+
10+
return stripAnsi(string).match(charRegex()).length;
11+
};
612

713
module.exports = stringLength;

test.js

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

44
test('get the real length of a string', t => {
5+
t.is(stringLength(''), 0);
56
t.is(stringLength('𠀔'), 1);
67
t.is(stringLength('foo𠁐bar𠀃'), 8);
78
t.is(stringLength('あ'), 1);

0 commit comments

Comments
 (0)