Browse Source

feat: generic type StringGen for *very* implicit string conversion

Sv443 7 months ago
parent
commit
ff15e1e312
3 changed files with 19 additions and 6 deletions
  1. 6 4
      src/components/MarkdownDialog.ts
  2. 7 1
      src/types.ts
  3. 6 1
      src/utils/misc.ts

+ 6 - 4
src/components/MarkdownDialog.ts

@@ -1,10 +1,12 @@
-import { setInnerHtml } from "src/utils/dom.js";
-import { BytmDialog, type BytmDialogOptions } from "./BytmDialog.js";
 import { marked } from "marked";
+import { setInnerHtml } from "../utils/dom.js";
+import { consumeStringGen } from "../utils/misc.js";
+import { BytmDialog, type BytmDialogOptions } from "./BytmDialog.js";
+import type { StringGen } from "../types.js";
 
 type MarkdownDialogOptions = Omit<BytmDialogOptions, "renderBody"> & {
   /** The markdown to render */
-  body: string | (() => string | Promise<string>);
+  body: StringGen;
 };
 
 export class MarkdownDialog extends BytmDialog {
@@ -36,7 +38,7 @@ export class MarkdownDialog extends BytmDialog {
 
     const mdCont = typeof this.opts.body === "string"
       ? this.opts.body
-      : await this.opts.body();
+      : await consumeStringGen(this.opts.body);
 
     const markdownEl = document.createElement("div");
     markdownEl.classList.add("bytm-markdown-dialog-content", "bytm-markdown-container");

+ 7 - 1
src/types.ts

@@ -1,4 +1,4 @@
-import { NanoEmitter } from "@sv443-network/userutils";
+import { NanoEmitter, type Stringifiable } from "@sv443-network/userutils";
 import type * as consts from "./constants.js";
 import type { scriptInfo } from "./constants.js";
 import type { addSelectorListener } from "./observers.js";
@@ -14,6 +14,12 @@ import type { showPrompt } from "./dialogs/prompt.js";
 
 //#region other
 
+/**
+ * Value that is either a string (or stringifiable value) or a sync or async function that returns a string (or a stringifiable value)  
+ * Use `await consumeStringGen(strGen)` to get the actual string value from this type
+ */
+export type StringGen = Stringifiable | (() => Stringifiable | Promise<Stringifiable>);
+
 /** Custom CLI args passed to rollup */
 export type RollupArgs = Partial<{
   "config-mode": "development" | "production";

+ 6 - 1
src/utils/misc.ts

@@ -1,7 +1,7 @@
 import { compress, decompress, fetchAdvanced, openInNewTab, pauseFor, randomId } from "@sv443-network/userutils";
 import { marked } from "marked";
 import { branch, compressionFormat, repo, sessionStorageAvailable } from "../constants.js";
-import { type Domain, type ResourceKey } from "../types.js";
+import { type Domain, type ResourceKey, type StringGen } from "../types.js";
 import { error, type TrLocale, warn, sendRequest } from "./index.js";
 import langMapping from "../../assets/locales.json" with { type: "json" };
 
@@ -191,6 +191,11 @@ export function getOS() {
   return "other";
 }
 
+/** Turns the passed StringGen (either a string, stringifiable object or a sync or async function returning a string or stringifiable object) into a string */
+export async function consumeStringGen(strGen: StringGen): Promise<string> {
+  return String(typeof strGen === "function" ? await strGen() : strGen);
+}
+
 //#region resources
 
 /**