aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/gleam/regex.gleam11
-rw-r--r--src/gleam_stdlib.mjs4
-rw-r--r--test/gleam/regex_test.gleam9
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+")