« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/common-util/sugar.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/common-util/sugar.js')
-rw-r--r--src/common-util/sugar.js25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/common-util/sugar.js b/src/common-util/sugar.js
index 354cf5cc..c988156c 100644
--- a/src/common-util/sugar.js
+++ b/src/common-util/sugar.js
@@ -417,6 +417,31 @@ export function escapeRegex(string) {
   return string.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
 }
 
+// Adapted from here: https://emnudge.dev/notes/multiline-regex/
+// ...with a lot of changes
+export function re(...args) {
+  let flags, parts;
+  if (args.length === 2) {
+    flags = args[0];
+    parts = args[1];
+  } else if (args.length === 1) {
+    flags = '';
+    parts = args[0];
+  } else {
+    throw new Error(`Expected 1 or 2 arguments`);
+  }
+
+  const source = parts
+    .flat(Infinity)
+    .map(item =>
+      (item instanceof RegExp
+        ? item.source
+        : item.toString()))
+    .join('');
+
+  return new RegExp(source, flags);
+}
+
 export function splitKeys(key) {
   return key.split(/(?<=(?<!\\)(?:\\\\)*)\./);
 }