From 35b3b4f4876804fed9542d8d04e93425a96e1fa1 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Tue, 21 Jan 2025 07:26:41 -0400 Subject: cli, thumbs: factor out stringifyCache --- src/util/cli.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'src/util') 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'); +} -- cgit 1.3.0-6-gf8a5