From cb80cf508ac0990038ec6ee6cd90cb61eaeabe5c Mon Sep 17 00:00:00 2001 From: Louis Pilfold Date: Thu, 26 Aug 2021 22:33:03 +0100 Subject: Regex compile and check --- src/gleam_stdlib.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src/gleam_stdlib.js') diff --git a/src/gleam_stdlib.js b/src/gleam_stdlib.js index c7db142..d2454ba 100644 --- a/src/gleam_stdlib.js +++ b/src/gleam_stdlib.js @@ -7,6 +7,7 @@ import { toBitString, stringBits, } from "./gleam.js"; +import { CompileError as RegexCompileError } from "./gleam/regex.js"; const Nil = undefined; @@ -228,3 +229,20 @@ export function bit_string_slice(bits, position, length) { export function codepoint(int) { return new UtfCodepoint(int); } + +export function regex_check(regex, string) { + return regex.test(string); +} + +export function compile_regex(pattern, options) { + try { + let flags = ""; + 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) + ); + } +} -- cgit v1.2.3