redacted my info, into my database #1
1 changed files with 19 additions and 0 deletions
|
|
@ -1,7 +1,11 @@
|
||||||
import { parseProfile, type Profile } from "./Profile";
|
import { parseProfile, type Profile } from "./Profile";
|
||||||
|
|
||||||
class Redactor {
|
class Redactor {
|
||||||
|
public unredactedProfile: Profile | undefined = undefined;
|
||||||
|
|
||||||
async TryGetUnredacter(): Promise<Profile> {
|
async TryGetUnredacter(): Promise<Profile> {
|
||||||
|
if (!!this.unredactedProfile) return this.unredactedProfile;
|
||||||
|
|
||||||
const storedKey = localStorage.getItem("key");
|
const storedKey = localStorage.getItem("key");
|
||||||
if (!storedKey) throw new Error("Missing key");
|
if (!storedKey) throw new Error("Missing key");
|
||||||
|
|
||||||
|
|
@ -30,6 +34,21 @@ class Redactor {
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
return parseProfile(data);
|
return parseProfile(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TS/JS
|
||||||
|
async t(unredactPath: string, fallback: string): Promise<string> {
|
||||||
|
try {
|
||||||
|
if (!this.unredactedProfile) await this.TryGetUnredacter();
|
||||||
|
const src = this.unredactedProfile ?? {};
|
||||||
|
|
||||||
|
// safe dot-path lookup
|
||||||
|
const value = unredactPath.split(".").reduce<any>((o, k) => o?.[k], src);
|
||||||
|
|
||||||
|
return (value ?? "") !== "" ? String(value) : fallback;
|
||||||
|
} catch {
|
||||||
|
return fallback;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export let re = new Redactor();
|
export let re = new Redactor();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue