Skip to content
Advertisement

Typescript switch/case on string not maching when it should

I have a strange behavior on a switch statement that is supposed to check for possible values of a string.

I’m trying to develop a little parser and using TDD I managed to write (and test) a function that parses a single line at a time, resulting in the expected result for all my cases.

Now I’m developing a bigger function that parses a bunch of lines at once so what it does is essentially split these lines and call the function that parses one line at a time.

The strange behaviour happens when I’m checking a value:

JavaScript

This function is tested and should be working properly with strings like 'C - 3 - 4' (this input was tested and passed) but when the following function makes a call, it does not work anymore and instead it triggers the default statement:

JavaScript

For reference, here are the tests I use:

JavaScript

Advertisement

Answer

As pointed out by @VLAZ, I had an invisible character of zero width when printed in my string which was causing this bug. Simply removing this character in the first place solved the problem.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement