progress
All checks were successful
Rebuild signaller for deprived.dev to rebuild site / test_service (push) Successful in 15s
All checks were successful
Rebuild signaller for deprived.dev to rebuild site / test_service (push) Successful in 15s
This commit is contained in:
parent
5e1cf0a83b
commit
ff613a2798
1 changed files with 19 additions and 0 deletions
|
|
@ -1,7 +1,11 @@
|
|||
import { parseProfile, type Profile } from "./Profile";
|
||||
|
||||
class Redactor {
|
||||
public unredactedProfile: Profile | undefined = undefined;
|
||||
|
||||
async TryGetUnredacter(): Promise<Profile> {
|
||||
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<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();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue