rastrillo / fiesta Public

Clone
git clone https://amadan.net/rastrillo/fiesta

Plain git — no account needed to clone.

Download

Download this file

1// select_node — drives select.js's pure half in plain Node: how a query
2// ranks a select's options, what leaving the box after a search commits,
3// and which blanks are prompts. Everything that touches a page stays
4// behind select.js's `document` guard, which Node never takes.
5//
6// check/select_test.go is the only caller. It pipes {options, queries}
7// on stdin and reads back each query's values, best first, and the
8// value leaving the box would settle on; or {blanks}, and reads back
9// what the box shows and lists for each blank option.
10//
11// This file lives under check/node/ beside the other Node drivers, so
12// `go test` in check/ finds it by name; it is never embedded, and
13// nothing in fiesta.go reaches for *.mjs.
14import sel from "../../js/select.js";
15
16const { order, settle, isPrompt, offered } = sel;
17
18let raw = "";
19for await (const chunk of process.stdin) raw += chunk;
20const { options, queries, blanks } = JSON.parse(raw);
21if (blanks) {
22 process.stdout.write(JSON.stringify(blanks.map((b) => ({
23 prompt: isPrompt(b.value, b.marked, b.required, b.disabled),
24 offered: offered(b.value, b.marked, b.required, b.searching, b.disabled),
25 }))));
26} else {
27 const out = {};
28 const settled = {};
29 for (const q of queries) {
30 const hits = order(options, q);
31 out[q] = hits.map((o) => o.value);
32 const s = settle(hits, q);
33 settled[q] = s ? s.value : "";
34 }
35 process.stdout.write(JSON.stringify({ order: out, settled }));
36}
37