diff --git a/src/ts/Redaction/Redactor.ts b/src/ts/Redaction/Redactor.ts index f647531..7aebebd 100644 --- a/src/ts/Redaction/Redactor.ts +++ b/src/ts/Redaction/Redactor.ts @@ -1,7 +1,11 @@ import { parseProfile, type Profile } from "./Profile"; class Redactor { + public unredactedProfile: Profile | undefined = undefined; + async TryGetUnredacter(): Promise { + if (!!this.unredactedProfile) return this.unredactedProfile; + const storedKey = localStorage.getItem("key"); if (!storedKey) throw new Error("Missing key"); @@ -30,6 +34,21 @@ class Redactor { const data = await res.json(); return parseProfile(data); } + + // TS/JS + async t(unredactPath: string, fallback: string): Promise { + try { + if (!this.unredactedProfile) await this.TryGetUnredacter(); + const src = this.unredactedProfile ?? {}; + + // safe dot-path lookup + const value = unredactPath.split(".").reduce((o, k) => o?.[k], src); + + return (value ?? "") !== "" ? String(value) : fallback; + } catch { + return fallback; + } + } } export let re = new Redactor();