@sealcode/jdd 0.8.9 → 0.8.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { JDDContext } from "../jdd-context.js";
|
|
2
2
|
import { StringBasedArgument } from "./string-based-argument.js";
|
|
3
3
|
export declare class Enum<T extends string> extends StringBasedArgument<T> {
|
|
4
|
-
readonly
|
|
5
|
-
constructor(values: Readonly<T[]> | ((context: JDDContext) => Readonly<T[]>));
|
|
4
|
+
private readonly innerValueGetter;
|
|
5
|
+
constructor(values: Readonly<T[]> | ((context: JDDContext) => Readonly<T[]>) | ((context: JDDContext) => Promise<Readonly<T[]>>));
|
|
6
|
+
getValues(ctx: JDDContext): Promise<readonly T[]>;
|
|
6
7
|
getTypeName(): string;
|
|
7
|
-
getEmptyValue(jdd_context: JDDContext): T
|
|
8
|
-
getExampleValue(jdd_context: JDDContext): T
|
|
8
|
+
getEmptyValue(jdd_context: JDDContext): Promise<T>;
|
|
9
|
+
getExampleValue(jdd_context: JDDContext): Promise<T>;
|
|
9
10
|
countWords(): number;
|
|
10
|
-
parseFormInput(jdd_context: JDDContext, input: Record<string, unknown>): T | null
|
|
11
|
+
parseFormInput(jdd_context: JDDContext, input: Record<string, unknown>): Promise<T | null>;
|
|
11
12
|
}
|
|
@@ -3,28 +3,33 @@ import { StringBasedArgument } from "./string-based-argument.js";
|
|
|
3
3
|
export class Enum extends StringBasedArgument {
|
|
4
4
|
constructor(values) {
|
|
5
5
|
super();
|
|
6
|
-
this.
|
|
6
|
+
this.innerValueGetter =
|
|
7
|
+
typeof values == "function" ? values : async () => values;
|
|
8
|
+
}
|
|
9
|
+
async getValues(ctx) {
|
|
10
|
+
return this.innerValueGetter(ctx);
|
|
7
11
|
}
|
|
8
12
|
getTypeName() {
|
|
9
13
|
return "enum";
|
|
10
14
|
}
|
|
11
|
-
getEmptyValue(jdd_context) {
|
|
12
|
-
return this.getValues(jdd_context)[0];
|
|
15
|
+
async getEmptyValue(jdd_context) {
|
|
16
|
+
return (await this.getValues(jdd_context))[0];
|
|
13
17
|
}
|
|
14
|
-
getExampleValue(jdd_context) {
|
|
18
|
+
async getExampleValue(jdd_context) {
|
|
15
19
|
const pick_from = this.example_values.length
|
|
16
20
|
? this.example_values
|
|
17
|
-
: this.getValues(jdd_context);
|
|
21
|
+
: await this.getValues(jdd_context);
|
|
18
22
|
return pick_from[Math.floor(Math.random() * pick_from.length)];
|
|
19
23
|
}
|
|
20
24
|
countWords() {
|
|
21
25
|
return 0;
|
|
22
26
|
}
|
|
23
27
|
// TODO: this should be maybe removed or change into receivedToParsed?
|
|
24
|
-
parseFormInput(jdd_context, input) {
|
|
28
|
+
async parseFormInput(jdd_context, input) {
|
|
29
|
+
const values = await this.getValues(jdd_context);
|
|
25
30
|
if (is(input, predicates.string) &&
|
|
26
|
-
(is(input, predicates.enum(
|
|
27
|
-
is(input.trim(), predicates.enum(
|
|
31
|
+
(is(input, predicates.enum(values)) ||
|
|
32
|
+
is(input.trim(), predicates.enum(values)))) {
|
|
28
33
|
return input.trim();
|
|
29
34
|
}
|
|
30
35
|
else {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"enum.js","sourceRoot":"","sources":["../../src/component-arguments/enum.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAEzD,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AAEjE,MAAM,OAAO,IAAuB,SAAQ,mBAAsB;
|
|
1
|
+
{"version":3,"file":"enum.js","sourceRoot":"","sources":["../../src/component-arguments/enum.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAEzD,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AAEjE,MAAM,OAAO,IAAuB,SAAQ,mBAAsB;IAKjE,YAC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K,CAAC,IAAI,EAAO,CAAC;SACzB;aAAM;YACN,OAAO,IAAI,CAAC;SACZ;IACF,CAAC;CACD"}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import assert from "assert";
|
|
2
2
|
import { Enum } from "./enum.js";
|
|
3
3
|
describe("enum argument", () => {
|
|
4
|
-
it("uses only the example values if provided when running getExampleValues", () => {
|
|
4
|
+
it("uses only the example values if provided when running getExampleValues", async () => {
|
|
5
5
|
const e = new Enum(["a", "b"]).setExampleValues(["a"]);
|
|
6
6
|
for (let i = 0; i <= 100; i++) {
|
|
7
|
-
assert.deepStrictEqual(e.getExampleValue({}), "a");
|
|
7
|
+
assert.deepStrictEqual(await e.getExampleValue({}), "a");
|
|
8
8
|
}
|
|
9
9
|
});
|
|
10
10
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"enum.test.js","sourceRoot":"","sources":["../../src/component-arguments/enum.test.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,QAAQ,CAAC,eAAe,EAAE,GAAG,EAAE;IAC9B,EAAE,CAAC,wEAAwE,EAAE,
|
|
1
|
+
{"version":3,"file":"enum.test.js","sourceRoot":"","sources":["../../src/component-arguments/enum.test.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,QAAQ,CAAC,eAAe,EAAE,GAAG,EAAE;IAC9B,EAAE,CAAC,wEAAwE,EAAE,KAAK,IAAI,EAAE;QACvF,MAAM,CAAC,GAAG,IAAI,IAAI,CAAC,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;QACvD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,GAAG,EAAE,CAAC,EAAE,EAAE;YAC9B,MAAM,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC,eAAe,CAAC,EAAS,CAAC,EAAE,GAAG,CAAC,CAAC;SAChE;IACF,CAAC,CAAC,CAAC;AACJ,CAAC,CAAC,CAAC"}
|