Page MenuHomeSealhub

component-arguments.ts
No OneTemporary

component-arguments.ts

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> {
public example_values: ARG[] = [];
abstract getTypeName(): string;
abstract getEmptyValue(): ARG;
getExampleValue() {
return this.example_values[
Math.floor(this.example_values.length * Math.random())
];
}
setExampleValues(values: ARG[]) {
this.example_values = values;
}
}
export class ShortText extends ComponentArgument<string> {
example_values = [
"Lorem ipsum set doloris",
"Hippopotomonstrosesquip delos muertos",
"Consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore",
"Ipsum",
];
getTypeName() {
return "short-text";
}
getEmptyValue() {
return "";
}
}
export class MarkdownArgument extends ComponentArgument<string> {
getTypeName() {
return "markdown";
}
getEmptyValue() {
return "";
}
example_values = [
`*Lorem ipsum* dolor sit amet, **consectetur adipiscing elit**, sed [do eiusmod](#) tempor incididunt ut labore et dolore magna aliqua. Posuere lorem ipsum dolor sit amet consectetur adipiscing elit. Porttitor massa id neque aliquam vestibulum. Turpis egestas pretium aenean pharetra.
Ullamcorper dignissim cras tincidunt lobortis feugiat vivamus at augue. Tellus cras adipiscing enim eu turpis egestas. Sagittis id consectetur purus ut faucibus pulvinar elementum. Pulvinar etiam non quam lacus suspendisse. Viverra adipiscing at in tellus integer feugiat scelerisque. Ultricies leo integer malesuada nunc vel risus commodo. Morbi leo urna molestie at elementum. Justo laoreet sit amet cursus sit amet. `,
`**Duis ut diam quam nulla**.
Lectus mauris *ultrices* eros [in cursus turpis massa](#).
Pulvinar neque laoreet suspendisse interdum consectetur. Orci phasellus egestas tellus rutrum tellus.`,
];
}
export class List<
T extends ComponentArgument<unknown>
> extends ComponentArgument<Array<ExtractComponentArgumentValue<T>>> {
constructor(public item_type: T) {
super();
}
getTypeName() {
return "list";
}
getEmptyValue() {
return [];
}
getExampleValue() {
const count = Math.floor(Math.random() * 5);
const result = [] as Array<ExtractComponentArgumentValue<T>>;
for (let i = 0; i < count; i++) {
result.push(
this.item_type.getExampleValue() as ExtractComponentArgumentValue<T>
);
}
return result;
}
}
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]>;
};
}
getExampleValue() {
return Object.fromEntries(
Object.entries(this.structure).map(([name, arg]) => [
name,
arg.getExampleValue(),
])
) as {
[property in keyof T]: ExtractComponentArgumentValue<T[property]>;
};
}
}
export class Enum<T extends string> extends ComponentArgument<T> {
public readonly values: Readonly<T[]>;
constructor(values: Readonly<T[]>) {
super();
this.values = values;
}
getTypeName() {
return "enum";
}
getEmptyValue() {
return this.values[0];
}
getExampleValue() {
return this.values[Math.floor(Math.random() * this.values.length)];
}
}
export class Image extends ComponentArgument<string> {
constructor(public path_to_image_on_fs: string) {
super();
}
getTypeName() {
return "image";
}
getEmptyValue() {
return "";
}
getExampleValue() {
return "";
}
}
export const ComponentArguments = {
Image,
Structured,
List,
Markdown: MarkdownArgument,
ShortText,
Enum,
};

File Metadata

Mime Type
text/plain
Expires
Fri, Nov 28, 15:17 (5 h, 39 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1083858
Default Alt Text
component-arguments.ts (3 KB)

Event Timeline