Skip to content

Commit 38aaa2a

Browse files
PgBiellpil
authored andcommitted
fix matching split returning list with Nil on JS
1 parent 3fde1cd commit 38aaa2a

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

src/gleam/regex.gleam

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,19 +127,10 @@ pub fn split(with regex: Regex, content string: String) -> List(String) {
127127
do_split(regex, string)
128128
}
129129

130-
@target(erlang)
131130
@external(erlang, "gleam_stdlib", "regex_split")
131+
@external(javascript, "../gleam_stdlib.mjs", "regex_split")
132132
fn do_split(a: Regex, b: String) -> List(String)
133133

134-
@target(javascript)
135-
fn do_split(regex, string) -> List(String) {
136-
js_split(string, regex)
137-
}
138-
139-
@target(javascript)
140-
@external(javascript, "../gleam_stdlib.mjs", "split")
141-
fn js_split(a: String, b: Regex) -> List(String)
142-
143134
/// Collects all matches of the regular expression.
144135
///
145136
/// ## Examples

src/gleam_stdlib.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,10 @@ export function compile_regex(pattern, options) {
402402
}
403403
}
404404

405+
export function regex_split(regex, string) {
406+
return List.fromArray(string.split(regex).map(item => item === undefined ? "" : item));
407+
}
408+
405409
export function regex_scan(regex, string) {
406410
const matches = Array.from(string.matchAll(regex)).map((match) => {
407411
const content = match[0];

test/gleam/regex_test.gleam

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ pub fn split_test() {
7373
|> should.equal(["foo", "32", "4", "9", "0"])
7474
}
7575

76+
pub fn matching_split_test() {
77+
let assert Ok(re) = regex.from_string("([+-])( *)(d)*")
78+
79+
regex.split(re, "abc+ def+ghi+ abc")
80+
|> should.equal([
81+
"abc", "+", " ", "d", "ef", "+", "", "", "ghi", "+", " ", "", "abc",
82+
])
83+
}
84+
7685
pub fn scan_test() {
7786
let assert Ok(re) = regex.from_string("Gl\\w+")
7887

0 commit comments

Comments
 (0)