diff options
author | Louis Pilfold <louis@lpil.uk> | 2021-08-26 23:13:44 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2021-08-27 00:37:15 +0100 |
commit | 6104a3bcc26a05942b8062ae62a0c7b932ca8cf6 (patch) | |
tree | d32e8a76a710a33b0d204333343bec5f831f9db2 /src/gleam_stdlib.js | |
parent | 70f065415469c65dd7f66dd4ef3f8e651f16b66a (diff) | |
download | gleam_stdlib-6104a3bcc26a05942b8062ae62a0c7b932ca8cf6.tar.gz gleam_stdlib-6104a3bcc26a05942b8062ae62a0c7b932ca8cf6.zip |
Regex scan
Diffstat (limited to 'src/gleam_stdlib.js')
-rw-r--r-- | src/gleam_stdlib.js | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/src/gleam_stdlib.js b/src/gleam_stdlib.js index d2454ba..652b7b5 100644 --- a/src/gleam_stdlib.js +++ b/src/gleam_stdlib.js @@ -7,7 +7,11 @@ import { toBitString, stringBits, } from "./gleam.js"; -import { CompileError as RegexCompileError } from "./gleam/regex.js"; +import { + CompileError as RegexCompileError, + Match as RegexMatch, +} from "./gleam/regex.js"; +import { Some, None } from "./gleam/option.js"; const Nil = undefined; @@ -236,13 +240,21 @@ export function regex_check(regex, string) { export function compile_regex(pattern, options) { try { - let flags = ""; + let flags = "gu"; if (options.case_insensitive) flags += "i"; if (options.multi_line) flags += "m"; return new Ok(new RegExp(pattern, flags)); } catch (error) { - return new Error( - new RegexCompileError(error.message, error.columnNumber || 0) - ); + let number = (error.columnNumber || 0) | 0; + return new Error(new RegexCompileError(error.message, number)); } } + +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())); + return new RegexMatch(content, List.fromArray(submatches)); + }); + return List.fromArray(matches); +} |