Current section
Files
Jump to
Current section
Files
src/gossamer/regexp.ffi.mjs
// src/gossamer/regexp.ffi.ts
import * as $dict from "../../gleam_stdlib/gleam/dict.mjs";
import * as $regexp from "./regexp.mjs";
import { flagChar, fromFlagChar } from "./regexp_flag.ffi.mjs";
import { fromArray, toArray } from "../utils/list.ffi.mjs";
import { toOption } from "../utils/option.ffi.mjs";
import { toResult } from "../utils/result.ffi.mjs";
function flagsToString(flags2) {
return toArray(flags2).map(flagChar).sort().join("");
}
function stringToFlags(source2) {
const result = [];
for (const char of source2) {
const flag = fromFlagChar(char);
if (flag !== void 0) result.push(flag);
}
return fromArray(result);
}
var new_ = (pattern) => {
return toResult.fromThrows(() => new RegExp(pattern));
};
var new_with = (pattern, flags2) => {
return toResult.fromThrows(() => new RegExp(pattern, flagsToString(flags2)));
};
var escape = (string) => {
return RegExp.escape(string);
};
var source = (regex) => regex.source;
var flags = (regex) => stringToFlags(regex.flags);
var last_index = (regex) => regex.lastIndex;
var set_last_index = (regex, index) => {
regex.lastIndex = index;
return regex;
};
var is_global = (regex) => regex.global;
var is_ignore_case = (regex) => regex.ignoreCase;
var is_multiline = (regex) => regex.multiline;
var is_dot_all = (regex) => regex.dotAll;
var is_unicode = (regex) => regex.unicode;
var is_unicode_sets = (regex) => regex.unicodeSets;
var is_sticky = (regex) => regex.sticky;
var has_indices = (regex) => regex.hasIndices;
var test_ = (regex, input) => regex.test(input);
var exec = (regex, input) => {
const result = regex.exec(input);
if (result === null) return toResult(null);
const captures = fromArray(result.slice(1).map((c) => toOption(c)));
const namedEntries = [];
if (result.groups) {
for (const [key, value] of Object.entries(result.groups)) {
if (value !== void 0) namedEntries.push([key, value]);
}
}
const namedCaptures = $dict.from_list(fromArray(namedEntries));
return toResult(
$regexp.Match$Match(result[0], captures, namedCaptures, result.index ?? 0)
);
};
export {
escape,
exec,
flags,
has_indices,
is_dot_all,
is_global,
is_ignore_case,
is_multiline,
is_sticky,
is_unicode,
is_unicode_sets,
last_index,
new_,
new_with,
set_last_index,
source,
test_
};