Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F969722
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/component-arguments/image.ts b/src/component-arguments/image.ts
index f7c72de..addbbbf 100644
--- a/src/component-arguments/image.ts
+++ b/src/component-arguments/image.ts
@@ -1,32 +1,42 @@
import _locreq from "locreq";
import { JDDContext } from "..";
import { File as FileArgument } from "./file";
const locreq = _locreq(__dirname);
-let example_image_promises: Record<string, Promise<string> | null> = {
- "image1.jpg": null,
- "image3.jpg": null,
- "image4.jpg": null,
- "image5.jpg": null,
-};
+const default_example_image_promises: Record<string, Promise<string> | null> =
+ Object.fromEntries(
+ ["image1.jpg", "image3.jpg", "image4.jpg", "image5.jpg"].map((key) => [
+ locreq.resolve(`resources/${key}`),
+ null,
+ ])
+ );
export class Image extends FileArgument {
+ private example_image_promises = default_example_image_promises;
+
getTypeName() {
return "image";
}
+ setExampleImages(paths: string[]) {
+ this.example_image_promises = Object.fromEntries(
+ paths.map((path) => [path, null])
+ );
+ return this;
+ }
+
async getExampleValue(context: JDDContext) {
- const keys = Object.keys(example_image_promises);
+ const keys = Object.keys(this.example_image_promises);
const key = keys[Math.floor(Math.random() * keys.length)];
- if (!example_image_promises[key]) {
- example_image_promises[key] = context.encode_file(
+ if (!this.example_image_promises[key]) {
+ this.example_image_promises[key] = context.encode_file(
{
type: "path",
- path: locreq.resolve(`resources/${key}`),
+ path: key,
},
true
);
}
- return example_image_promises[key] as Promise<string>;
+ return this.example_image_promises[key] as Promise<string>;
}
}
diff --git a/src/component-arguments/list.ts b/src/component-arguments/list.ts
index f9e45ba..6a3ddc1 100644
--- a/src/component-arguments/list.ts
+++ b/src/component-arguments/list.ts
@@ -1,70 +1,78 @@
import { is, predicates } from "@sealcode/ts-predicates";
import { JDDContext } from "..";
import { MaybePromise } from "../utils/util-types";
-import {
- ComponentArgument,
- ExtractComponentArgumentValue,
-} from "./component-argument";
+import { ComponentArgument } from "./component-argument";
export class List<T> extends ComponentArgument<Array<T>> {
- constructor(public item_type: ComponentArgument<T>) {
+ constructor(
+ public item_type: ComponentArgument<T>,
+ public example_count: number | null = null
+ ) {
super();
item_type.parent_argument = this;
}
getTypeName() {
return "list";
}
getEmptyValue() {
return [];
}
+ getExampleCount() {
+ if (this.example_count === null) {
+ return Math.floor(Math.random() * 5);
+ } else {
+ return this.example_count;
+ }
+ }
+
async getExampleValue(context: JDDContext) {
if (this.example_values.length) {
return super.getExampleValue(context);
} else {
- const count = Math.floor(Math.random() * 5);
+ const count = this.getExampleCount();
const result = [] as Array<MaybePromise<T>>;
for (let i = 0; i < count; i++) {
result.push(this.item_type.getExampleValue(context));
}
return (await Promise.all(result)) as Array<T>;
}
}
countWords(value: Array<T>): number {
return value.reduce(
(acc, item) => acc + this.item_type.countWords(item),
0
);
}
async parseFormInput(
context: JDDContext,
input: unknown,
arg_name: string
): Promise<T[] | null> {
if (
!is(input, predicates.array(predicates.object)) &&
!is(input, predicates.object)
) {
throw new Error(`$.${arg_name} is not a list or object`);
}
const values = Array.isArray(input) ? input : Object.values(input);
let array_result: Array<T | T[] | null> = await Promise.all(
values.map(async (value, index) => {
const result = await this.item_type.parseFormInput(
context,
value,
`${arg_name}[${index}]`
);
return result;
})
);
if (this.item_type.getTypeName() != "list") {
array_result = array_result.flat() as T[];
}
return array_result.filter((e) => e !== null) as T[];
}
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sat, Nov 23, 12:52 (1 d, 12 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
547454
Default Alt Text
(4 KB)
Attached To
Mode
R130 jdd
Attached
Detach File
Event Timeline
Log In to Comment