@pi9/ask 0.1.0 → 0.3.0

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/src/validation.ts DELETED
@@ -1,48 +0,0 @@
1
- import type { AskOption, AskParams, ValidatedAskParams } from "./types.js";
2
-
3
- export function validateAskParams(params: AskParams): ValidatedAskParams {
4
- const question = params.question.trim();
5
- if (!question) throw new Error("Ask question must not be empty.");
6
-
7
- const context = trimOptional(params.context);
8
- const options = params.options.map(normalizeOption);
9
- const labels = new Set<string>();
10
- for (const option of options) {
11
- if (labels.has(option.label)) throw new Error(`Ask options contain duplicate label: ${option.label}.`);
12
- labels.add(option.label);
13
- }
14
-
15
- const allowFreeform = params.allowFreeform ?? true;
16
- if (options.length === 0) throw new Error("Ask needs at least one option.");
17
-
18
- return {
19
- question,
20
- ...(context === undefined ? {} : { context }),
21
- options,
22
- allowMultiple: params.allowMultiple ?? false,
23
- allowFreeform,
24
- ...(params.timeout === undefined ? {} : { timeout: params.timeout }),
25
- };
26
- }
27
-
28
- function normalizeOption(option: AskOption): AskOption {
29
- const label = option.label.trim();
30
- if (!label) throw new Error("Ask option label must not be empty.");
31
- const description = trimOptional(option.description);
32
- const preview = preserveOptional(option.preview);
33
- return {
34
- label,
35
- ...(description === undefined ? {} : { description }),
36
- ...(preview === undefined ? {} : { preview }),
37
- };
38
- }
39
-
40
- function trimOptional(value: string | undefined): string | undefined {
41
- if (value === undefined) return undefined;
42
- const trimmed = value.trim();
43
- return trimmed || undefined;
44
- }
45
-
46
- function preserveOptional(value: string | undefined): string | undefined {
47
- return value?.trim() ? value : undefined;
48
- }