« get me outta code hell

cli, thumbs: factor out stringifyCache - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/util
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2025-01-21 07:26:41 -0400
committer(quasar) nebula <qznebula@protonmail.com>2025-01-21 07:26:41 -0400
commit35b3b4f4876804fed9542d8d04e93425a96e1fa1 (patch)
tree45bcac379e22068bc1b8065a6e633e26419bc60d /src/util
parent288715112687d33c3193ebae469aaf7d01a52b6d (diff)
cli, thumbs: factor out stringifyCache
Diffstat (limited to 'src/util')
-rw-r--r--src/util/cli.js24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/util/cli.js b/src/util/cli.js
index 5d314dc4..be59b35e 100644
--- a/src/util/cli.js
+++ b/src/util/cli.js
@@ -493,3 +493,27 @@ export async function logicalPathTo(target) {
   const cwd = await logicalCWD();
   return relative(cwd, target);
 }
+
+export function stringifyCache(cache) {
+  cache ??= {};
+
+  if (Object.keys(cache).length === 0) {
+    return `{}`;
+  }
+
+  const entries = Object.entries(cache);
+  sortByName(entries, {getName: entry => entry[0]});
+
+  return [
+    `{`,
+    entries
+      .map(([key, value]) => [JSON.stringify(key), JSON.stringify(value)])
+      .map(([key, value]) => `${key}: ${value}`)
+      .map((line, index, array) =>
+        (index < array.length - 1
+          ? `${line},`
+          : line))
+      .map(line => `  ${line}`),
+    `}`,
+  ].flat().join('\n');
+}