« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/common-util/sugar.js36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/common-util/sugar.js b/src/common-util/sugar.js
index 354cf5cc..4dd34785 100644
--- a/src/common-util/sugar.js
+++ b/src/common-util/sugar.js
@@ -417,6 +417,42 @@ export function escapeRegex(string) {
   return string.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
 }
 
+// Adapted from here: https://emnudge.dev/notes/multiline-regex/
+export function re(...args) {
+  let flags = '';
+
+  const fn = (strings, ...substitutions) => {
+    strings = strings
+      .map(str => str.replace(/(?:\/\/.+)/gm, ''))
+      .map(str => str.replace(/\s+/g, ''));
+
+    substitutions = substitutions
+      .map(sub => [sub].flat(Infinity))
+      .map(sub => sub
+        .map(item =>
+          (item instanceof RegExp
+            ? item.source
+            : item.toString())))
+      .map(sub => sub.join(''));
+
+    const source =
+      String.raw({raw: strings}, ...substitutions);
+
+    return new RegExp(source, flags);
+  };
+
+  if (
+    args.length === 1 &&
+    typeof args[0] === 'string' &&
+    args[0].match(/^[a-z]+$/)
+  ) {
+    flags = args[0];
+    return fn;
+  } else {
+    return fn(...args);
+  }
+}
+
 export function splitKeys(key) {
   return key.split(/(?<=(?<!\\)(?:\\\\)*)\./);
 }