« get me outta code hell

data: move accent-parsing regex out of parseContributors - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-11-20 19:31:05 -0400
committer(quasar) nebula <qznebula@protonmail.com>2023-11-21 07:50:14 -0400
commit765e039ef94f7cd1365ba9f211bd686beab5e8ec (patch)
tree35a74e8f206c61aca5c69624475a31240fc9ab76
parent453b0e9845d41c7a1049d4f0982b66121626766a (diff)
data: move accent-parsing regex out of parseContributors
Also use named capturing groups (and non-capturing groups) for
generally better regex form.
-rw-r--r--src/data/yaml.js26
1 files changed, 14 insertions, 12 deletions
diff --git a/src/data/yaml.js b/src/data/yaml.js
index 1d35bae8..67cd8db7 100644
--- a/src/data/yaml.js
+++ b/src/data/yaml.js
@@ -717,26 +717,28 @@ export function parseAdditionalFiles(array) {
   }));
 }
 
-export function parseContributors(contributors) {
+const extractAccentRegex =
+  /^(?<main>.*?)(?: \((?<accent>.*)\))?$/;
+
+export function parseContributors(contributionStrings) {
   // If this isn't something we can parse, just return it as-is.
   // The Thing object's validators will handle the data error better
   // than we're able to here.
-  if (!Array.isArray(contributors)) {
-    return contributors;
+  if (!Array.isArray(contributionStrings)) {
+    return contributionStrings;
   }
 
-  contributors = contributors.map((contrib) => {
-    if (typeof contrib !== 'string') return contrib;
+  return contributionStrings.map(contribString => {
+    if (typeof contribString !== 'string') return contribString;
 
-    const match = contrib.match(/^(.*?)( \((.*)\))?$/);
-    if (!match) return contrib;
+    const match = contribString.match(extractAccentRegex);
+    if (!match) return contribString;
 
-    const who = match[1];
-    const what = match[3] || null;
-    return {who, what};
+    return {
+      who: match.groups.main,
+      what: match.groups.accent ?? null,
+    };
   });
-
-  return contributors;
 }
 
 function parseDimensions(string) {