Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F969825
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/component-arguments/enum.ts b/src/component-arguments/enum.ts
index 3d6ad86..3ce5de9 100644
--- a/src/component-arguments/enum.ts
+++ b/src/component-arguments/enum.ts
@@ -1,39 +1,42 @@
import { is, predicates } from "@sealcode/ts-predicates";
import { JDDContext } from "../jdd-context.js";
import { StringBasedArgument } from "./string-based-argument.js";
export class Enum<T extends string> extends StringBasedArgument<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)];
+ const pick_from = this.example_values.length
+ ? this.example_values
+ : this.values;
+ return pick_from[Math.floor(Math.random() * pick_from.length)];
}
countWords(): number {
return 0;
}
parseFormInput(_: JDDContext, input: Record<string, unknown>) {
if (
is(input, predicates.string) &&
(is(input, predicates.enum(this.values)) ||
is(input.trim(), predicates.enum(this.values)))
) {
return input.trim() as T;
} else {
return null;
}
}
}
diff --git a/src/component-arguments/structured.test.ts b/src/component-arguments/structured.test.ts
index 251aa33..9e18f8c 100644
--- a/src/component-arguments/structured.test.ts
+++ b/src/component-arguments/structured.test.ts
@@ -1,57 +1,57 @@
import { FileManager } from "@sealcode/file-manager";
import { makeSimpleEnglishJDDContext } from "../jdd-context.js";
import { ExtractParsed } from "./component-argument.js";
import { List } from "./list.js";
import { ShortText } from "./short-text.js";
import { Structured } from "./structured.js";
import assert from "assert";
describe("structured argument", () => {
it("properly extracts parsed type", () => {
const args = new Structured({
value: new ShortText().setExampleValues([""]),
tags: new List(new ShortText()).setExampleValues([["okazja"]]),
});
const values = {
value: "test",
tags: ["tag1", "tag2"],
} as ExtractParsed<typeof args>;
values.tags.map((tag) => tag.padStart(10)); // if the types are OK, this will typecheck OK
});
it("properly extracts parsed type within a list", () => {
const arg = new List(
new Structured({
value: new ShortText().setExampleValues([""]),
tags: new List(new ShortText()).setExampleValues([["okazja"]]),
})
);
const values = [
{
value: "test",
tags: ["tag1", "tag2"],
},
] as ExtractParsed<typeof arg>;
values[0].tags.map((tag) => tag.padStart(10)); // if the types are OK, this will typecheck OK
});
- it("uses the provided object as an example value if set", () => {
+ it("uses the provided object as an example value if set", async () => {
const arg = new Structured({
value: new ShortText().setExampleValues([""]),
tags: new List(new ShortText()).setExampleValues([["okazja"]]),
}).setExampleValues([{ value: "override", tags: ["override"] }]);
- const example_value = arg.getExampleValue(
+ const example_value = await arg.getExampleValue(
makeSimpleEnglishJDDContext(
new FileManager("/tmp", "/uploaded_files")
)
);
assert.deepStrictEqual(example_value, {
value: "override",
tags: ["override"],
});
});
});
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sun, Nov 24, 01:03 (1 d, 11 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
548048
Default Alt Text
(3 KB)
Attached To
Mode
R130 jdd
Attached
Detach File
Event Timeline
Log In to Comment