dsh-lite 0.0.0-stage → 0.6.1

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.
@@ -0,0 +1,45 @@
1
+ // Answer drafting for question cards — pure and DOM-free so the browser and the
2
+ // Node tests share one implementation. Semantics follow the host's own card
3
+ // (dsh-client-ui-user-questions) and dsh-tool-ask-user's README:
4
+ //
5
+ // skipped -> { id, selected: [] } (no custom)
6
+ // custom + single -> { id, selected: [], custom } (custom replaces the choice)
7
+ // custom + multi -> { id, selected, custom } (custom supplements)
8
+ // custom, no options -> { id, selected: [], custom }
9
+ //
10
+ // A draft is { selected: Set<string>, custom: string, skipped: boolean }.
11
+
12
+ /** Whether the question offers options (as opposed to free text). */
13
+ export function hasOptions(question) {
14
+ return Array.isArray(question?.options) && question.options.length > 0
15
+ }
16
+
17
+ /** A choice or non-blank custom text counts as answered. */
18
+ export function isAnswered(draft) {
19
+ return draft.selected.size > 0 || draft.custom.trim() !== ''
20
+ }
21
+
22
+ /** Answered or explicitly skipped: everything the model may receive. */
23
+ export function isCompleted(draft) {
24
+ return draft.skipped === true || isAnswered(draft)
25
+ }
26
+
27
+ /** Wire answer for one question. */
28
+ export function answerFor(question, draft) {
29
+ if (draft.skipped === true) return { id: question.id, selected: [] }
30
+ const selected = [...draft.selected]
31
+ const custom = draft.custom.trim()
32
+ if (custom.length === 0) return { id: question.id, selected }
33
+ return { id: question.id, selected: question.multiSelect === true ? selected : [], custom }
34
+ }
35
+
36
+ /**
37
+ * Build the whole batch. Returns `{ missing: index }` for the first question
38
+ * that is neither answered nor skipped — earlier answers are kept, nothing is
39
+ * sent — or `{ answers }` when every question is complete.
40
+ */
41
+ export function buildAnswerBatch(questions, drafts) {
42
+ const missing = drafts.findIndex((draft) => !isCompleted(draft))
43
+ if (missing >= 0) return { missing }
44
+ return { answers: questions.map((question, index) => answerFor(question, drafts[index])) }
45
+ }