diff options
author | shayan javani <massivefermion@protonmail.com> | 2023-03-14 16:38:43 +0330 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-14 13:08:43 +0000 |
commit | a4805b21cf42241cc2951947231e76679bd99d07 (patch) | |
tree | 33524eed2216e1dcf90f19d88fdd511cadd3aef5 /src/gleam_stdlib.mjs | |
parent | a37d5dddbb3a29b896e5d5351830223c9cd9e933 (diff) | |
download | gleam_stdlib-a4805b21cf42241cc2951947231e76679bd99d07.tar.gz gleam_stdlib-a4805b21cf42241cc2951947231e76679bd99d07.zip |
make `regex.scan`'s behavior consistent across targets (#423)
Diffstat (limited to 'src/gleam_stdlib.mjs')
-rw-r--r-- | src/gleam_stdlib.mjs | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/src/gleam_stdlib.mjs b/src/gleam_stdlib.mjs index 37c3ea2..632c668 100644 --- a/src/gleam_stdlib.mjs +++ b/src/gleam_stdlib.mjs @@ -388,8 +388,17 @@ export function compile_regex(pattern, options) { export function regex_scan(regex, string) { let matches = Array.from(string.matchAll(regex)).map((match) => { - let content = match.shift(); - let submatches = match.map((x) => (x ? new Some(x) : new None())); + const content = match[0]; + const submatches = []; + for (let n = match.length - 1; n > 0; n--) { + if (match[n]) { + submatches[n-1] = new Some(match[n]) + continue + } + if(submatches.length > 0) { + submatches[n-1] = new None() + } + } return new RegexMatch(content, List.fromArray(submatches)); }); return List.fromArray(matches); |