Page MenuHomeSealhub

No OneTemporary

diff --git a/src/component-arguments.test.ts b/src/component-arguments.test.ts
index d385951..fcd7d08 100644
--- a/src/component-arguments.test.ts
+++ b/src/component-arguments.test.ts
@@ -1,54 +1,63 @@
import {
ComponentArgument,
Enum,
List,
MarkdownArgument,
ShortText,
Structured,
} from "./component-arguments";
import { Component } from "./component";
import assert from "assert";
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"]);
});
it("Gives a nice example", () => {
new MarkdownArgument().getExampleValue();
const args = <const>{
text: new ShortText(),
markdown: new MarkdownArgument(),
};
const component = new (class testComponent extends Component<
typeof args
> {
getArguments() {
return args;
}
toHTML() {
return "";
}
})();
const example = component.getExampleValues();
assert(example.text.length > 0);
assert(example.markdown.length > 0);
});
it("allows for chaining methods when setting example values", () => {
const the_argument = new MarkdownArgument().setExampleValues([
"abc1",
"abc2",
]);
assert((the_argument as any) instanceof ComponentArgument);
});
+
+ describe("List argument", () => {
+ it("Generates example values from provided examples, if they are provided", () => {
+ const list = new List(new ShortText()).setExampleValues([
+ ["a", "b", "c"],
+ ]);
+ assert.deepStrictEqual(list.getExampleValue(), ["a", "b", "c"]);
+ });
+ });
});
diff --git a/src/component-arguments.ts b/src/component-arguments.ts
index 567113f..f064636 100644
--- a/src/component-arguments.ts
+++ b/src/component-arguments.ts
@@ -1,174 +1,178 @@
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 {
this.example_values = values;
return this;
}
}
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>
- );
+ if (this.example_values.length) {
+ return super.getExampleValue();
+ } else {
+ 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;
}
- 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/x-diff
Expires
Mon, Dec 23, 08:11 (1 d, 3 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
555364
Default Alt Text
(6 KB)

Event Timeline