diff options
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | src/gleam_stdlib.mjs | 1 | ||||
-rw-r--r-- | test/gleam/regex_test.gleam | 24 |
3 files changed, 26 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ce51e9..a5ce2ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- Fixed a bug on target JavaScript where `regex.check` would not correctly execute + while using the same regular expression in consecutive calls. - The `zip` function's second argument in the `list` module gains the `with` label. - The `strict_zip` function's second argument in the `list` module gains the `with` label. diff --git a/src/gleam_stdlib.mjs b/src/gleam_stdlib.mjs index 7c41cdd..1163c6f 100644 --- a/src/gleam_stdlib.mjs +++ b/src/gleam_stdlib.mjs @@ -371,6 +371,7 @@ export function utf_codepoint_to_int(utf_codepoint) { } export function regex_check(regex, string) { + regex.lastIndex = 0; return regex.test(string); } diff --git a/test/gleam/regex_test.gleam b/test/gleam/regex_test.gleam index eb84013..d97c8fa 100644 --- a/test/gleam/regex_test.gleam +++ b/test/gleam/regex_test.gleam @@ -27,7 +27,7 @@ pub fn compile_test() { regex.check(re, "abc\n123") |> should.be_true - // For Erlang: This test will only passes if unicode and ucp flags are set + // On target Erlang this test will only pass if unicode and ucp flags are set let assert Ok(re) = regex.compile("\\s", options) // Em space == U+2003 == " " == used below regex.check(re, " ") @@ -42,6 +42,28 @@ pub fn check_test() { regex.check(re, "boo") |> should.be_false + + re + |> regex.check(content: "foo") + |> should.be_true + + "boo" + |> regex.check(with: re) + |> should.be_false + + // On target JavaScript internal `RegExp` objects are stateful when they + // have the global or sticky flags set (e.g., /foo/g or /foo/y). + // These following tests make sure that our implementation circumvents this. + let assert Ok(re) = regex.from_string("^-*[0-9]+") + + regex.check(re, "1") + |> should.be_true + + regex.check(re, "12") + |> should.be_true + + regex.check(re, "123") + |> should.be_true } pub fn split_test() { |