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

Debug.log for Regexes does not show the regex #16

Open
jfmengels opened this issue Feb 28, 2022 · 0 comments
Open

Debug.log for Regexes does not show the regex #16

jfmengels opened this issue Feb 28, 2022 · 0 comments

Comments

@jfmengels
Copy link

(I imagine that this fix would likely be made in elm/core, but I thought it would be more appropriate here. Feel free to move the issue)

Problem

I was just in the middle of debugging why a string didn't match a regex, and I tried Debug.logging the regex, which gave me {}, which I found to be very unhelpful.

Creating a regex inside the Elm REPL gives the same behavior:

> import Regex
> Regex.fromString "hello"
Just {} : Maybe Regex.Regex

Expectation

I expected to see some kind of information in the log message to see what the regex is. The Node.js REPL gives the following feedback, which I believe is more helpful:

> /hello/
/hello/
> new RegExp("hello", "ig")
/hello/gi

Suggested solution

I suggest that elm/core's _Debug_toAnsiString function adds a special-case for regexes to print the regex in a useful way.

I feel like the format of the stringified version aims to be somewhat copy-pastable into Elm code (seeing examples like Dict.fromList), which makes this format a tiny bit tricky. Ideally, the format of the regex should probably be the "JavaScript regex format" (/hello/gi), but that wouldn't be valid Elm code.

A second option would be to print the code needed to create the Regex from Elm code (Regex.fromString "hello") but that would have 2 drawbacks:

  • The code would not be copy-pastable, because there is no function that directly creates a regex without wrapping it in a Maybe.
  • You'd need to add backslashes (/"hello"/ -> Regex.fromString "\"hello\""), which makes the regex harder to read, which might be counterproductive when the intent of the Debug.logging was to make it clearer what the regex is.
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

No branches or pull requests

1 participant