paseo-plugin-genui 0.1.3 → 0.1.4
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.
- package/package.json +1 -1
- package/shared/core/catalog.ts +2 -0
- package/shared/core/evaluate.ts +23 -1
- package/shared/core/validate.ts +8 -5
- package/shared/genui.test.ts +15 -0
- package/shared/instructions.ts +1 -1
package/package.json
CHANGED
package/shared/core/catalog.ts
CHANGED
package/shared/core/evaluate.ts
CHANGED
|
@@ -287,7 +287,7 @@ const Interpreter = defineClass(
|
|
|
287
287
|
case "arrow":
|
|
288
288
|
throw new RuntimeError(
|
|
289
289
|
expr.pos,
|
|
290
|
-
"arrow functions can only be declared as a named function (`share = p => ...`) or passed to map, filter, find, some or
|
|
290
|
+
"arrow functions can only be declared as a named function (`share = p => ...`) or passed to map, filter, find, some, every or sort",
|
|
291
291
|
);
|
|
292
292
|
}
|
|
293
293
|
},
|
|
@@ -653,6 +653,7 @@ const Interpreter = defineClass(
|
|
|
653
653
|
method(call: Extract<Expr, { type: "method" }>, scope: Scope | null): Value {
|
|
654
654
|
const receiver = this.expr(call.object, scope);
|
|
655
655
|
if (isList(receiver) && LAMBDA_METHODS.has(call.name)) return this.lambdaMethod(receiver, call, scope);
|
|
656
|
+
if (isList(receiver) && call.name === "sort") return this.sort(receiver, call, scope);
|
|
656
657
|
const args = call.args.map((arg) => this.expr(arg, scope));
|
|
657
658
|
const label = `.${call.name}()`;
|
|
658
659
|
const textAt = (index: number) => this.text(args[index], call.args[index].pos, label);
|
|
@@ -670,6 +671,8 @@ const Interpreter = defineClass(
|
|
|
670
671
|
return receiver.includes(args[0]);
|
|
671
672
|
case "indexOf":
|
|
672
673
|
return receiver.indexOf(args[0]);
|
|
674
|
+
case "concat":
|
|
675
|
+
return checkLength(receiver.concat(...args), call.pos);
|
|
673
676
|
}
|
|
674
677
|
} else if (typeof receiver === "string") {
|
|
675
678
|
switch (call.name) {
|
|
@@ -697,6 +700,25 @@ const Interpreter = defineClass(
|
|
|
697
700
|
throw new RuntimeError(call.pos, `\`${label}\` is not available on ${describe(receiver)}`);
|
|
698
701
|
},
|
|
699
702
|
|
|
703
|
+
/** A sorted copy; without a comparator, numbers sort numerically and anything else as text. */
|
|
704
|
+
sort(list: readonly Value[], call: Extract<Expr, { type: "method" }>, scope: Scope | null): Value {
|
|
705
|
+
checkLength(list, call.pos);
|
|
706
|
+
const fn = call.args[0];
|
|
707
|
+
if (fn === undefined) {
|
|
708
|
+
return [...list].sort((a, b) =>
|
|
709
|
+
typeof a === "number" && typeof b === "number" ? a - b : toText(a, call.pos).localeCompare(toText(b, call.pos)),
|
|
710
|
+
);
|
|
711
|
+
}
|
|
712
|
+
if (fn.type !== "arrow") throw new RuntimeError(call.pos, "`.sort()` takes a function, as in `(a, b) => a.price - b.price`");
|
|
713
|
+
const [aName, bName] = fn.params;
|
|
714
|
+
return [...list].sort((a, b) => {
|
|
715
|
+
let inner = scope;
|
|
716
|
+
if (aName !== undefined) inner = { name: aName, value: a, parent: inner };
|
|
717
|
+
if (bName !== undefined) inner = { name: bName, value: b, parent: inner };
|
|
718
|
+
return this.number(this.expr(fn.body, inner), fn.body.pos, ".sort()");
|
|
719
|
+
});
|
|
720
|
+
},
|
|
721
|
+
|
|
700
722
|
lambdaMethod(list: readonly Value[], call: Extract<Expr, { type: "method" }>, scope: Scope | null): Value {
|
|
701
723
|
checkLength(list, call.pos);
|
|
702
724
|
const fn = call.args[0];
|
package/shared/core/validate.ts
CHANGED
|
@@ -31,7 +31,7 @@ type Arity = readonly [min: number, max: number];
|
|
|
31
31
|
const METHOD_ARITY: ReadonlyMap<string, Arity> = new Map(
|
|
32
32
|
Object.entries({
|
|
33
33
|
map: [1, 1], filter: [1, 1], find: [1, 1], some: [1, 1], every: [1, 1],
|
|
34
|
-
join: [0, 1], slice: [0, 2], includes: [1, 1], indexOf: [1, 1],
|
|
34
|
+
join: [0, 1], slice: [0, 2], includes: [1, 1], indexOf: [1, 1], concat: [1, Infinity], sort: [0, 1],
|
|
35
35
|
split: [1, 1], trim: [0, 0], toUpperCase: [0, 0], toLowerCase: [0, 0],
|
|
36
36
|
startsWith: [1, 1], endsWith: [1, 1], toFixed: [0, 1],
|
|
37
37
|
} satisfies Record<MethodName, Arity>),
|
|
@@ -46,7 +46,7 @@ const FUNCTION_ARITY: ReadonlyMap<string, Arity> = new Map(
|
|
|
46
46
|
|
|
47
47
|
const ACTION_LIST = [...ACTION_SPECS].map(([name, spec]) => signature(name, spec)).join(", ");
|
|
48
48
|
const ARROW_ONLY =
|
|
49
|
-
"arrow functions can only be declared as a named function (`share = p => ...`) or passed to map, filter, find, some or
|
|
49
|
+
"arrow functions can only be declared as a named function (`share = p => ...`) or passed to map, filter, find, some, every or sort";
|
|
50
50
|
|
|
51
51
|
/** The type of a value, as named in diagnostics. */
|
|
52
52
|
export type Kind = "null" | "boolean" | "number" | "string" | "list" | "object" | "node" | "action" | "binding";
|
|
@@ -432,13 +432,16 @@ const Validator = defineClass(
|
|
|
432
432
|
this.diagnostics.report(expr.pos, `\`.${expr.name}()\` takes ${arityText(arity)}`);
|
|
433
433
|
}
|
|
434
434
|
const [first, ...rest] = expr.args;
|
|
435
|
-
|
|
435
|
+
const sort = expr.name === "sort";
|
|
436
|
+
if ((LAMBDA_METHODS.has(expr.name) || sort) && first !== undefined) {
|
|
436
437
|
if (first.type !== "arrow") {
|
|
437
|
-
|
|
438
|
+
const example = sort ? "(a, b) => a.price - b.price" : "x => x.price";
|
|
439
|
+
this.diagnostics.report(first.pos, `\`.${expr.name}()\` takes a function, as in \`${example}\``);
|
|
438
440
|
this.check(first, params, refs);
|
|
439
441
|
} else {
|
|
440
442
|
if (first.params.length > 2) {
|
|
441
|
-
|
|
443
|
+
const passes = sort ? "(a, b)" : "(item, index)";
|
|
444
|
+
this.diagnostics.report(first.pos, `\`.${expr.name}()\` passes ${passes}; use at most 2 parameters`);
|
|
442
445
|
}
|
|
443
446
|
this.check(first.body, [...params, ...first.params], refs);
|
|
444
447
|
}
|
package/shared/genui.test.ts
CHANGED
|
@@ -45,6 +45,21 @@ describe("paseo-plugin-genui core, instructions, and gallery", () => {
|
|
|
45
45
|
}
|
|
46
46
|
});
|
|
47
47
|
|
|
48
|
+
it("supports .concat() and .sort() on lists", () => {
|
|
49
|
+
const compiled = compile(
|
|
50
|
+
[
|
|
51
|
+
"a = [{n: \"x\", v: 3}, {n: \"y\", v: 1}]",
|
|
52
|
+
"all = a.concat([{n: \"z\", v: 2}])",
|
|
53
|
+
"Text(all.sort((p, q) => q.v - p.v).map(p => p.n).join(\",\"))",
|
|
54
|
+
"Text([10, 9, 1].sort().join(\",\"))",
|
|
55
|
+
"Text(a.map(p => p.n).join(\",\"))",
|
|
56
|
+
].join("\n"),
|
|
57
|
+
);
|
|
58
|
+
expect(compiled.diagnostics).toEqual([]);
|
|
59
|
+
const { roots } = evaluate(compiled.program!, initialState(compiled.program!));
|
|
60
|
+
expect(roots.map((root) => root.props.text)).toEqual(["x,z,y", "1,9,10", "x,y"]);
|
|
61
|
+
});
|
|
62
|
+
|
|
48
63
|
it("covers all 86 catalog components in the gallery and compiles + evaluates every gallery spec", async () => {
|
|
49
64
|
const files = await listGallery();
|
|
50
65
|
expect(files.length).toBeGreaterThanOrEqual(170);
|
package/shared/instructions.ts
CHANGED
|
@@ -24,7 +24,7 @@ export const REFERENCE_MORE_GROUPS: readonly ComponentGroup[] = ["Inputs", "Widg
|
|
|
24
24
|
|
|
25
25
|
export const UI_REFERENCE = `# Inline UI reference
|
|
26
26
|
The \`\`\`ui language used by the Inline UI section below.
|
|
27
|
-
Syntax: one statement per line. \`$x = 8\` is state inputs bind to; \`name = expr\` names a value and \`f = (a, b) => expr\` a function; each bare expression renders. JS expressions incl. ** ?: ?? \`\${x}\` [lists] {objects} x.y map/filter/find/join/length, plus round(x, d), fmt(x, d), sum, min, max, abs, entries, Load(path). Named args go last: Slider($n, 0, 10, label: "N").
|
|
27
|
+
Syntax: one statement per line. \`$x = 8\` is state inputs bind to; \`name = expr\` names a value and \`f = (a, b) => expr\` a function; each bare expression renders. JS expressions incl. ** ?: ?? \`\${x}\` [lists] {objects} x.y map/filter/find/sort/concat/join/length, plus round(x, d), fmt(x, d), sum, min, max, abs, entries, Load(path). Named args go last: Slider($n, 0, 10, label: "N").
|
|
28
28
|
Components (? optional; tone: neutral|good|warn|bad|info if meaningful; more in the next section):
|
|
29
29
|
${componentLines(REFERENCE_GROUPS)}
|
|
30
30
|
Actions: ${actionList}; a list runs in order; Send posts as the user.
|