Page MenuHomeSealhub

No OneTemporary

diff --git a/src/component-arguments.test.ts b/src/component-arguments.test.ts
index 3610749..7f23a76 100644
--- a/src/component-arguments.test.ts
+++ b/src/component-arguments.test.ts
@@ -1,10 +1,14 @@
-import { List, ShortText, Structured } from "./component-arguments";
+import { Enum, List, ShortText, Structured } from "./component-arguments";
describe("component arguments", () => {
it("handles nesting objects and arrays", () => {
new List(new ShortText());
new List(
new Structured({ name: new ShortText(), surname: new ShortText() })
);
});
+
+ it("handles enums", () => {
+ new Enum(<const>["hamster", "dentist"]);
+ });
});
diff --git a/src/component-arguments.ts b/src/component-arguments.ts
index 2d0609b..ad38b99 100644
--- a/src/component-arguments.ts
+++ b/src/component-arguments.ts
@@ -1,115 +1,117 @@
export type ExtractComponentArgumentValue<C> = C extends ComponentArgument<
infer T
>
? T
: never;
export type ExtractStructuredComponentArgumentsValues<C> = {
[property in keyof C]: ExtractComponentArgumentValue<C[property]>;
};
export type T = ExtractComponentArgumentValue<Structured<any>>;
export abstract class ComponentArgument<ARG extends any> {
ARG!: ARG; // this is for some reason necessary to make the ExtractComponentArgumentValue work. It seems like this argument has to appear somewhere in the structure of the class to make it work. Possibly it can be made into some function argument or smth
abstract getTypeName(): string;
abstract getEmptyValue(): ARG;
}
export class ShortText extends ComponentArgument<string> {
getTypeName() {
return "short-text";
}
getEmptyValue() {
return "";
}
}
export class MarkdownArgument extends ComponentArgument<string> {
getTypeName() {
return "markdown";
}
getEmptyValue() {
return "";
}
}
export class List<
T extends ComponentArgument<unknown>
> extends ComponentArgument<Array<ExtractComponentArgumentValue<T>>> {
constructor(public item_type: T) {
super();
}
getTypeName() {
return "list";
}
getEmptyValue() {
return [];
}
}
export class Structured<
T extends Record<string, ComponentArgument<unknown>>
> extends ComponentArgument<{
[property in keyof T]: ExtractComponentArgumentValue<T[property]>;
}> {
constructor(public structure: T) {
super();
}
getTypeName() {
return "structured";
}
getEmptyValue() {
return Object.fromEntries(
Object.entries(this.structure).map(([name, arg]) => [
name,
arg.getEmptyValue(),
])
) as {
[property in keyof T]: ExtractComponentArgumentValue<T[property]>;
};
}
}
export class Enum<T extends string> extends ComponentArgument<T> {
- constructor(public readonly values: T[]) {
+ public readonly values: Readonly<T[]>;
+ constructor(values: Readonly<T[]>) {
super();
+ this.values = values;
}
getTypeName() {
return "enum";
}
getEmptyValue() {
return this.values[0];
}
}
export class Image extends ComponentArgument<string> {
constructor(public path_to_image_on_fs: string) {
super();
}
getTypeName() {
return "image";
}
getEmptyValue() {
return "";
}
}
export const ComponentArguments = {
Image,
Structured,
List,
Markdown: MarkdownArgument,
ShortText,
Enum,
};

File Metadata

Mime Type
text/x-diff
Expires
Mon, Dec 23, 08:43 (21 h, 32 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
556187
Default Alt Text
(3 KB)

Event Timeline