Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ANSI code stripping should be optional #11

Closed
joepie91 opened this issue Oct 30, 2020 · 3 comments · Fixed by #16
Closed

ANSI code stripping should be optional #11

joepie91 opened this issue Oct 30, 2020 · 3 comments · Fixed by #16

Comments

@joepie91
Copy link

Currently, ANSI escape codes are always stripped by this library. However, there are plenty of contexts where one might be interested in the 'real' character count of a string, but where ANSI escape codes do not have a special semantic meaning, and so stripping them would result in an incorrect character count.

IMO, the ANSI code stripping should either be opt-in, or possibly even considered out-of-scope for this module entirely - since one can also just call strip-ansi on the string before passing it in, or whichever stripping function is appropriate for their specific semantic context.

@sindresorhus
Copy link
Owner

The current behavior is like this because my use-cases always required stripping ANSI escape codes. If you provide some good use-cases where stripping ANSI escape codes are not wanted, I'm open to adding an option to not strip such codes.

@joepie91
Copy link
Author

The original usecase for which I was looking at this library (which is for a client), is name length limit validation in a network protocol. There's no terminal involved there, and so 'escape codes' would just be characters (printable or otherwise) like any other.

Another usecase which I'd like to use this library for is my own validation library, as a specifically string-aware equivalent of the has-length-of and has-length-between validators (which currently just look at .length and are agnostic to the exact input type).

In that case it would need to be usecase-agnostic, because people may use the validator for any purpose, including things like form validation, external API response correctness checks, protocol message validation, and so on - all of which are not terminal-related usecases, and so the escape codes have no semantic meaning there, as I understand it anyway.

@sindresorhus
Copy link
Owner

Thanks for elaborating. Makes sense. PR welcome to add such an option. I don't want to do a breaking change now just for this, but we can change the default next time we have to do a breaking change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants