Skip to content

Conversation

@kevin940726
Copy link
Contributor

Currently, counting length of strings with only ansi escape codes will throw an error:

stringLength('\u001B[1m\u001B[22m');

// TypeError: Cannot read property 'length' of null

This PR fixes that by checking the length of the stripped string first.

The fix is quite straightforward. Alternatively, we can change it to something like (stripAnsi(string).match(charRegex()) || '').length (or millions of other ways).

@sindresorhus
Copy link
Owner

The initial if (string === '') { check should be preserved. It's an optimization. See recent commits in main branch.

@kevin940726
Copy link
Contributor Author

@sindresorhus Do you mean to check it twice, one for string and the other for strippedString? Something like...

if (string === '') return 0;
const strippedString = stripAnsi(string);
if (strippedString === '') return 0;
// ...

@papb
Copy link

papb commented Mar 13, 2021

@kevin940726 Yes I think that's what he means

@sindresorhus sindresorhus changed the title Handle zero-length ansi string Correctly handle zero-length ANSI string Mar 17, 2021
@sindresorhus sindresorhus merged commit 32de93a into sindresorhus:main Mar 17, 2021
@sindresorhus
Copy link
Owner

Yes, thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants