Skip to content

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

@jfmengels

Description

@jfmengels

(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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions