Debian Codegraph @

frontend/src/entries/metadata.ts

raw
1 import { is_empty } from "../lib/objects";
2 import { ok } from "../lib/result";
3 import type { SafeValidator, Validator } from "../lib/validation";
4 import { record } from "../lib/validation";
5
6 export type MetadataValue = string | boolean | number;
7
8 const entry_meta_item: SafeValidator<MetadataValue> = (json) => {
9 if (
10 typeof json == "boolean" ||
11 typeof json == "number" ||
12 typeof json == "string"
13 ) {
14 return ok(json);
15 }
16 return ok("Unsupported metadata value");
17 };
18
19 /**
20 * Convert a metadata value to a string ready for an input field.
21 * @param value - the metadata value.
22 */
23 function meta_value_to_string(value: MetadataValue): string {
24 if (typeof value === "boolean") {
25 return value ? "TRUE" : "FALSE";
26 }
27 return typeof value === "string" ? value : value.toString();
28 }
29
30 /**
31 * Convert a string to a metadata value.
32 * @param s - The string that the user entered.
33 */
34 function string_to_meta_value(s: string): MetadataValue {
35 if (s === "TRUE") {
36 return true;
37 }
38 if (s === "FALSE") {
39 return false;
40 }
41 return s;
42 }
43
44 /** Metadate of a entry or posting. */
45 export class EntryMetadata {
46 #meta: Readonly<Record<string, MetadataValue>>;
47
48 constructor(meta?: Record<string, MetadataValue>) {
49 this.#meta = meta ?? {};
50 }
51
52 /** Whether the metadata is empty. */
53 is_empty(): boolean {
54 return is_empty(this.#meta);
55 }
56
57 /** Get the filename, falling back to an empty string if missing. */
58 get filename(): string {
59 return this.#meta.filename?.toString() ?? "";
60 }
61
62 /** Get the line number as a string, falling back to an empty string if missing. */
63 get lineno(): string {
64 return this.#meta.lineno?.toString() ?? "";
65 }
66
67 toJSON(): Record<string, MetadataValue> {
68 return this.#meta;
69 }
70
71 /** Delete a key from the metadata and return an updated copy. */
72 delete(key: string): EntryMetadata {
73 // eslint-disable-next-line @typescript-eslint/no-unused-vars
74 const { [key]: ignored, ...rest } = this.#meta;
75 return new EntryMetadata(rest);
76 }
77
78 /** All metadata entries (values as strings), filtering out hidden ones. */
79 entries(): [key: string, value: string][] {
80 return Object.entries(this.#meta)
81 .filter(
82 ([key]) =>
83 !key.startsWith("_") && key !== "filename" && key !== "lineno",
84 )
85 .map(([key, value]) => [key, meta_value_to_string(value)]);
86 }
87
88 /** Get the value for a key. */
89 get(key: string): MetadataValue | undefined {
90 return this.#meta[key];
91 }
92
93 /** Set the value for a key and return an updated copy. */
94 set(key: string, value: MetadataValue): EntryMetadata {
95 return new EntryMetadata({ ...this.#meta, [key]: value });
96 }
97
98 /** Set the value for a key from a string and return an updated copy. */
99 set_string(key: string, value: string): EntryMetadata {
100 return this.set(key, string_to_meta_value(value));
101 }
102
103 /** Add a new empty key and value and return an updated copy. */
104 add(): EntryMetadata {
105 return this.set("", "");
106 }
107
108 /** Change a key and a return an updated copy. */
109 update_key(current_key: string, new_key: string): EntryMetadata {
110 return new EntryMetadata(
111 Object.fromEntries(
112 Object.entries(this.#meta).map(([key, value]) => [
113 key === current_key ? new_key : key,
114 value,
115 ]),
116 ),
117 );
118 }
119
120 private static raw_validator = record(entry_meta_item);
121
122 static validator: Validator<EntryMetadata> = (json) =>
123 EntryMetadata.raw_validator(json).map((meta) => new EntryMetadata(meta));
124 }