Page MenuHomeSealhub

No OneTemporary

diff --git a/src/component-arguments.ts b/src/component-arguments.ts
index 3fd16f7..ad5909d 100644
--- a/src/component-arguments.ts
+++ b/src/component-arguments.ts
@@ -1,212 +1,212 @@
export type ExtractComponentArgumentValue<C> = C extends ComponentArgument<
infer T
>
? T
: never;
export type ExtractStructuredComponentArgumentsValues<C> = {
[property in keyof C]: ExtractComponentArgumentValue<C[property]>;
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type T = ExtractComponentArgumentValue<Structured<any>>;
export abstract class ComponentArgument<ARG> {
public example_values: ARG[] = [];
abstract getTypeName(): string;
abstract getEmptyValue(): ARG;
abstract countWords(value: ARG): number;
getExampleValue() {
return this.example_values[
Math.floor(this.example_values.length * Math.random())
];
}
setExampleValues(values: ARG[]): this {
this.example_values = values;
return this;
}
}
export class ShortText extends ComponentArgument<string> {
example_values = [
"Lorem ipsum set doloris",
- "Hippopotomonstrosesquip delos muertos",
+ "Main hippopotomonstrosesquip delos muertos",
"Consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore",
"Ipsum",
];
getTypeName() {
return "short-text";
}
getEmptyValue() {
return "";
}
countWords(value: string): number {
return value.toString().split(" ").length;
}
}
export class MarkdownArgument extends ComponentArgument<string> {
getTypeName() {
return "markdown";
}
getEmptyValue() {
return "";
}
countWords(value: string): number {
return value.toString().split(" ").length;
}
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;
}
countWords(value: ExtractComponentArgumentValue<T>[]): number {
return value.reduce(
(acc, item) => acc + this.item_type.countWords(item),
0
);
}
}
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";
}
countWords(value: {
[property in keyof T]: ExtractComponentArgumentValue<T[property]>;
}): number {
return Object.entries(value).reduce((acc, [key, val]) => {
if (!this.structure[key]) {
console.warn(`Key ${key} doesn't exist in structured argument`);
return acc + 0;
}
return acc + this.structure[key].countWords(val);
}, 0);
}
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)];
}
countWords(): number {
return 0;
}
}
export class Image extends ComponentArgument<string> {
constructor(public path_to_image_on_fs: string) {
super();
}
getTypeName() {
return "image";
}
getEmptyValue() {
return "";
}
getExampleValue() {
return "";
}
countWords(): number {
return 0;
}
}
export const ComponentArguments = {
Image,
Structured,
List,
Markdown: MarkdownArgument,
ShortText,
Enum,
};

File Metadata

Mime Type
text/x-diff
Expires
Mon, Dec 23, 06:58 (1 d, 2 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
556843
Default Alt Text
(5 KB)

Event Timeline