diff options
author | PgBiel <9021226+PgBiel@users.noreply.github.com> | 2024-04-15 20:29:38 -0300 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2024-04-19 13:17:47 +0100 |
commit | 38aaa2aa6aba8dcaffea28192567c983a5a10853 (patch) | |
tree | 9533a27443fdf5be02264b5d8b8bc305b98ccba7 | |
parent | 3fde1cdf30523354b4dacbc76a856e922dcad2f4 (diff) | |
download | gleam_stdlib-38aaa2aa6aba8dcaffea28192567c983a5a10853.tar.gz gleam_stdlib-38aaa2aa6aba8dcaffea28192567c983a5a10853.zip |
fix matching split returning list with Nil on JS
-rw-r--r-- | src/gleam/regex.gleam | 11 | ||||
-rw-r--r-- | src/gleam_stdlib.mjs | 4 | ||||
-rw-r--r-- | test/gleam/regex_test.gleam | 9 |
3 files changed, 14 insertions, 10 deletions
diff --git a/src/gleam/regex.gleam b/src/gleam/regex.gleam index b72a02e..eecb3ec 100644 --- a/src/gleam/regex.gleam +++ b/src/gleam/regex.gleam @@ -127,19 +127,10 @@ pub fn split(with regex: Regex, content string: String) -> List(String) { do_split(regex, string) } -@target(erlang) @external(erlang, "gleam_stdlib", "regex_split") +@external(javascript, "../gleam_stdlib.mjs", "regex_split") fn do_split(a: Regex, b: String) -> List(String) -@target(javascript) -fn do_split(regex, string) -> List(String) { - js_split(string, regex) -} - -@target(javascript) -@external(javascript, "../gleam_stdlib.mjs", "split") -fn js_split(a: String, b: Regex) -> List(String) - /// Collects all matches of the regular expression. /// /// ## Examples diff --git a/src/gleam_stdlib.mjs b/src/gleam_stdlib.mjs index 40fcc3f..d921c63 100644 --- a/src/gleam_stdlib.mjs +++ b/src/gleam_stdlib.mjs @@ -402,6 +402,10 @@ export function compile_regex(pattern, options) { } } +export function regex_split(regex, string) { + return List.fromArray(string.split(regex).map(item => item === undefined ? "" : item)); +} + export function regex_scan(regex, string) { const matches = Array.from(string.matchAll(regex)).map((match) => { const content = match[0]; diff --git a/test/gleam/regex_test.gleam b/test/gleam/regex_test.gleam index 8c13827..4174125 100644 --- a/test/gleam/regex_test.gleam +++ b/test/gleam/regex_test.gleam @@ -73,6 +73,15 @@ pub fn split_test() { |> should.equal(["foo", "32", "4", "9", "0"]) } +pub fn matching_split_test() { + let assert Ok(re) = regex.from_string("([+-])( *)(d)*") + + regex.split(re, "abc+ def+ghi+ abc") + |> should.equal([ + "abc", "+", " ", "d", "ef", "+", "", "", "ghi", "+", " ", "", "abc", + ]) +} + pub fn scan_test() { let assert Ok(re) = regex.from_string("Gl\\w+") |