@questify/core 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ashish Kumar
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,174 @@
1
+ # @questify/core
2
+
3
+ **Questify** — headless, zero-dependency questionnaire engine for the web.
4
+
5
+ [![npm version](https://img.shields.io/npm/v/%40questify%2Fcore?style=flat-square)](https://www.npmjs.com/package/@questify/core)
6
+ [![bundle size](https://img.shields.io/bundlephobia/minzip/%40questify%2Fcore?style=flat-square)](https://bundlephobia.com/package/@questify/core)
7
+ [![license](https://img.shields.io/npm/l/%40questify%2Fcore?style=flat-square)](./LICENSE)
8
+ [![TypeScript](https://img.shields.io/badge/TypeScript-ready-blue?style=flat-square)](./src)
9
+
10
+ **Live demo → [questify.renderlog.in](https://questify.renderlog.in)**
11
+
12
+ ---
13
+
14
+ This package is a pure TypeScript state machine for multi-step forms and surveys.
15
+ It ships **zero runtime dependencies** and **zero UI** — you control every pixel.
16
+ Framework adapters for React and Vue are included.
17
+
18
+ ## Install
19
+
20
+ ```bash
21
+ npm install @questify/core
22
+ ```
23
+
24
+ ## React
25
+
26
+ ```tsx
27
+ import { useQuestionnaire } from '@questify/core/react';
28
+
29
+ const questions = [
30
+ { id: 'name', text: "What's your name?", type: 'text', required: true },
31
+ { id: 'rating', text: 'Rate us 1–5', type: 'rating', required: true },
32
+ {
33
+ id: 'feedback',
34
+ text: 'What could be improved?',
35
+ type: 'text',
36
+ showIf: { questionId: 'rating', value: 3, operator: 'lte' },
37
+ },
38
+ ];
39
+
40
+ function Survey() {
41
+ const { question, answer, next, back, progress, isComplete, canGoNext, canGoBack } =
42
+ useQuestionnaire({ questions });
43
+
44
+ if (!question) return null;
45
+
46
+ return (
47
+ <div>
48
+ <progress value={progress} max={1} />
49
+ <h2>{question.text}</h2>
50
+
51
+ {/* render your own inputs — questify ships no UI */}
52
+ {question.type === 'text' && (
53
+ <input onChange={(e) => answer(e.target.value)} />
54
+ )}
55
+
56
+ <button onClick={back} disabled={!canGoBack}>Back</button>
57
+ {canGoNext
58
+ ? <button onClick={next}>Next</button>
59
+ : <button disabled={!isComplete}>Submit</button>
60
+ }
61
+ </div>
62
+ );
63
+ }
64
+ ```
65
+
66
+ ## Vue 3
67
+
68
+ ```vue
69
+ <script setup lang="ts">
70
+ import { useQuestionnaire } from '@questify/core/vue';
71
+ const { question, answer, next, back, progress, isComplete } =
72
+ useQuestionnaire({ questions });
73
+ </script>
74
+
75
+ <template>
76
+ <div v-if="question">
77
+ <progress :value="progress" :max="1" />
78
+ <h2>{{ question.text }}</h2>
79
+ <input v-if="question.type === 'text'" @input="e => answer(e.target.value)" />
80
+ <button @click="back">Back</button>
81
+ <button @click="next">Next</button>
82
+ </div>
83
+ </template>
84
+ ```
85
+
86
+ ## Vanilla JS
87
+
88
+ ```ts
89
+ import { Questionnaire } from '@questify/core';
90
+
91
+ const q = new Questionnaire({ questions });
92
+
93
+ const unsubscribe = q.subscribe((state) => {
94
+ console.log(state.question?.text, state.progress);
95
+ });
96
+
97
+ q.answer('Alice');
98
+ q.next();
99
+ unsubscribe();
100
+ ```
101
+
102
+ ## Question types
103
+
104
+ `text` · `email` · `number` · `boolean` · `single` · `multi` · `rating` · `date`
105
+
106
+ ## Conditional logic — `showIf`
107
+
108
+ ```ts
109
+ // Simple
110
+ showIf: { questionId: 'smoker', value: true }
111
+
112
+ // With operator
113
+ showIf: { questionId: 'rating', value: 3, operator: 'lte' }
114
+
115
+ // Compound AND
116
+ showIf: {
117
+ and: [
118
+ { questionId: 'age', value: 18, operator: 'gte' },
119
+ { questionId: 'smoker', value: true },
120
+ ],
121
+ }
122
+
123
+ // Compound OR — nestable to any depth
124
+ showIf: {
125
+ or: [
126
+ { questionId: 'urgent', value: true },
127
+ { and: [{ questionId: 'score', value: 5, operator: 'lte' }] },
128
+ ],
129
+ }
130
+ ```
131
+
132
+ Operators: `eq` · `neq` · `gt` · `lt` · `gte` · `lte` · `includes`
133
+
134
+ ## API
135
+
136
+ ### Hook / composable return value
137
+
138
+ | Property | Type | Description |
139
+ |---|---|---|
140
+ | `question` | `Question \| null` | Current question (step mode) |
141
+ | `visibleQuestions` | `Question[]` | All visible questions after resolving `showIf` |
142
+ | `progress` | `number` (0–1) | Fraction of visible questions answered |
143
+ | `responses` | `Record<string, unknown>` | All answers so far |
144
+ | `errors` | `Record<string, string>` | Validation errors for visible questions |
145
+ | `isComplete` | `boolean` | All required visible questions answered & valid |
146
+ | `answer(value)` | `fn` | Answer current question (step mode) |
147
+ | `answerById(id, value)` | `fn` | Answer any question by id (all mode) |
148
+ | `next()` | `fn` | Advance (validates required first) |
149
+ | `back()` | `fn` | Go back one step |
150
+ | `jumpTo(index)` | `fn` | Jump to a step by index |
151
+ | `validate()` | `fn` | Validate all visible fields, returns errors |
152
+ | `reset()` | `fn` | Clear all answers and restart |
153
+ | `getSubmittableResponses()` | `fn` | Responses for visible questions only — safe for submission |
154
+
155
+ ### Modes
156
+
157
+ | Mode | Use case |
158
+ |---|---|
159
+ | `step` (default) | Wizard — one question at a time |
160
+ | `all` | Show all questions simultaneously (accordion, long-form) |
161
+
162
+ ### Built-in validation
163
+
164
+ - `required` — whitespace-only treated as empty
165
+ - `min` / `max` — for `number` and `rating`
166
+ - `minLength` / `maxLength` — for `text` and `email`
167
+ - `regex` — for `text`
168
+ - `email` format — always validated automatically for `type: "email"`
169
+ - `date` format — `YYYY-MM-DD` enforced automatically for `type: "date"`
170
+ - `number` — rejects `NaN` and `±Infinity`
171
+
172
+ ## License
173
+
174
+ MIT © [Ashish Kumar](https://github.com/ashishcumar)
@@ -0,0 +1,132 @@
1
+ type QuestionType = "text" | "number" | "boolean" | "single" | "multi" | "date" | "rating" | "email";
2
+ type ConditionOperator = "eq" | "neq" | "gt" | "lt" | "gte" | "lte" | "includes";
3
+ /** A single condition against one question's response. */
4
+ interface ShowIfCondition {
5
+ questionId: string;
6
+ value: unknown;
7
+ operator?: ConditionOperator;
8
+ }
9
+ /**
10
+ * Compound condition — nest `and`/`or` arrays for full branching logic.
11
+ *
12
+ * @example
13
+ * // Show only when user has diabetes AND is on insulin
14
+ * showIf: {
15
+ * and: [
16
+ * { questionId: 'conditions', value: 'diabetes', operator: 'includes' },
17
+ * { questionId: 'on_insulin', value: true },
18
+ * ]
19
+ * }
20
+ */
21
+ interface ShowIfCompound {
22
+ and?: ShowIfRule[];
23
+ or?: ShowIfRule[];
24
+ }
25
+ /** A single condition OR a compound AND/OR tree — fully nestable. */
26
+ type ShowIfRule = ShowIfCondition | ShowIfCompound;
27
+ interface QuestionValidation {
28
+ min?: number;
29
+ max?: number;
30
+ minLength?: number;
31
+ maxLength?: number;
32
+ regex?: string;
33
+ message?: string;
34
+ }
35
+ interface QuestionOption {
36
+ label: string;
37
+ value: string | number;
38
+ }
39
+ interface Question {
40
+ id: string;
41
+ text: string;
42
+ type: QuestionType;
43
+ description?: string;
44
+ required?: boolean;
45
+ placeholder?: string;
46
+ options?: QuestionOption[];
47
+ showIf?: ShowIfRule;
48
+ validation?: QuestionValidation;
49
+ }
50
+ type DisplayMode = "step" | "all";
51
+ interface QuestionnaireConfig {
52
+ questions: Question[];
53
+ mode?: DisplayMode;
54
+ initialResponses?: Record<string, unknown>;
55
+ }
56
+ interface QuestionnaireState {
57
+ /** Current question (step mode). In 'all' mode, always the first visible question. */
58
+ question: Question | null;
59
+ /** All currently visible questions (after applying showIf conditions). */
60
+ visibleQuestions: Question[];
61
+ /** Zero-based index of the current question in the visible questions array. */
62
+ questionIndex: number;
63
+ /** Total number of currently visible questions. */
64
+ totalQuestions: number;
65
+ /** Progress from 0 to 1 based on how many questions have valid answers. */
66
+ progress: number;
67
+ /** All responses keyed by question id. */
68
+ responses: Record<string, unknown>;
69
+ /** True when all required visible questions have valid responses. */
70
+ isComplete: boolean;
71
+ /** Validation errors keyed by question id. */
72
+ errors: Record<string, string>;
73
+ /** Whether the user can navigate to the previous question. */
74
+ canGoBack: boolean;
75
+ /** Whether the user can navigate to the next question. */
76
+ canGoNext: boolean;
77
+ }
78
+
79
+ declare function validateAnswer(question: Question, value: unknown): string | null;
80
+ type Subscriber = (state: QuestionnaireState) => void;
81
+ declare class Questionnaire {
82
+ private config;
83
+ private responses;
84
+ private questionIndex;
85
+ private errors;
86
+ private subscribers;
87
+ private cachedState;
88
+ constructor(config: QuestionnaireConfig);
89
+ private recompute;
90
+ private notify;
91
+ getState(): QuestionnaireState;
92
+ /**
93
+ * Returns only responses for currently visible questions.
94
+ * Use this for form submission — avoids sending hidden-branch data to the server.
95
+ *
96
+ * `state.responses` retains all answers for back-navigation; this method prunes them.
97
+ */
98
+ getSubmittableResponses(): Record<string, unknown>;
99
+ subscribe(callback: Subscriber): () => void;
100
+ /**
101
+ * Answer the current question (step mode).
102
+ * In `mode: "all"` prefer `answerById()`.
103
+ */
104
+ answer(value: unknown): void;
105
+ /**
106
+ * Answer any visible question by its id — the right API for `mode: "all"`.
107
+ * Safe to call in step mode too (allows answering any step by id).
108
+ */
109
+ answerById(questionId: string, value: unknown): void;
110
+ private _setAnswer;
111
+ /**
112
+ * Advance to the next step (step mode).
113
+ * Validates the current required question first; populates `errors` if invalid.
114
+ */
115
+ next(): void;
116
+ /** Go to the previous step. No-op on step 0. */
117
+ back(): void;
118
+ /**
119
+ * Jump to a specific step index (0-based, within visible questions).
120
+ * Out-of-range indices are silently ignored.
121
+ */
122
+ jumpTo(index: number): void;
123
+ /**
124
+ * Validate all visible required questions and return a map of errors.
125
+ * Useful for "submit" gates in `mode: "all"`.
126
+ */
127
+ validate(): Record<string, string>;
128
+ /** Reset all responses and errors, restart from step 0. */
129
+ reset(): void;
130
+ }
131
+
132
+ export { type ConditionOperator, type DisplayMode, type Question, type QuestionOption, type QuestionType, type QuestionValidation, Questionnaire, type QuestionnaireConfig, type QuestionnaireState, type ShowIfCompound, type ShowIfCondition, type ShowIfRule, validateAnswer };
@@ -0,0 +1,132 @@
1
+ type QuestionType = "text" | "number" | "boolean" | "single" | "multi" | "date" | "rating" | "email";
2
+ type ConditionOperator = "eq" | "neq" | "gt" | "lt" | "gte" | "lte" | "includes";
3
+ /** A single condition against one question's response. */
4
+ interface ShowIfCondition {
5
+ questionId: string;
6
+ value: unknown;
7
+ operator?: ConditionOperator;
8
+ }
9
+ /**
10
+ * Compound condition — nest `and`/`or` arrays for full branching logic.
11
+ *
12
+ * @example
13
+ * // Show only when user has diabetes AND is on insulin
14
+ * showIf: {
15
+ * and: [
16
+ * { questionId: 'conditions', value: 'diabetes', operator: 'includes' },
17
+ * { questionId: 'on_insulin', value: true },
18
+ * ]
19
+ * }
20
+ */
21
+ interface ShowIfCompound {
22
+ and?: ShowIfRule[];
23
+ or?: ShowIfRule[];
24
+ }
25
+ /** A single condition OR a compound AND/OR tree — fully nestable. */
26
+ type ShowIfRule = ShowIfCondition | ShowIfCompound;
27
+ interface QuestionValidation {
28
+ min?: number;
29
+ max?: number;
30
+ minLength?: number;
31
+ maxLength?: number;
32
+ regex?: string;
33
+ message?: string;
34
+ }
35
+ interface QuestionOption {
36
+ label: string;
37
+ value: string | number;
38
+ }
39
+ interface Question {
40
+ id: string;
41
+ text: string;
42
+ type: QuestionType;
43
+ description?: string;
44
+ required?: boolean;
45
+ placeholder?: string;
46
+ options?: QuestionOption[];
47
+ showIf?: ShowIfRule;
48
+ validation?: QuestionValidation;
49
+ }
50
+ type DisplayMode = "step" | "all";
51
+ interface QuestionnaireConfig {
52
+ questions: Question[];
53
+ mode?: DisplayMode;
54
+ initialResponses?: Record<string, unknown>;
55
+ }
56
+ interface QuestionnaireState {
57
+ /** Current question (step mode). In 'all' mode, always the first visible question. */
58
+ question: Question | null;
59
+ /** All currently visible questions (after applying showIf conditions). */
60
+ visibleQuestions: Question[];
61
+ /** Zero-based index of the current question in the visible questions array. */
62
+ questionIndex: number;
63
+ /** Total number of currently visible questions. */
64
+ totalQuestions: number;
65
+ /** Progress from 0 to 1 based on how many questions have valid answers. */
66
+ progress: number;
67
+ /** All responses keyed by question id. */
68
+ responses: Record<string, unknown>;
69
+ /** True when all required visible questions have valid responses. */
70
+ isComplete: boolean;
71
+ /** Validation errors keyed by question id. */
72
+ errors: Record<string, string>;
73
+ /** Whether the user can navigate to the previous question. */
74
+ canGoBack: boolean;
75
+ /** Whether the user can navigate to the next question. */
76
+ canGoNext: boolean;
77
+ }
78
+
79
+ declare function validateAnswer(question: Question, value: unknown): string | null;
80
+ type Subscriber = (state: QuestionnaireState) => void;
81
+ declare class Questionnaire {
82
+ private config;
83
+ private responses;
84
+ private questionIndex;
85
+ private errors;
86
+ private subscribers;
87
+ private cachedState;
88
+ constructor(config: QuestionnaireConfig);
89
+ private recompute;
90
+ private notify;
91
+ getState(): QuestionnaireState;
92
+ /**
93
+ * Returns only responses for currently visible questions.
94
+ * Use this for form submission — avoids sending hidden-branch data to the server.
95
+ *
96
+ * `state.responses` retains all answers for back-navigation; this method prunes them.
97
+ */
98
+ getSubmittableResponses(): Record<string, unknown>;
99
+ subscribe(callback: Subscriber): () => void;
100
+ /**
101
+ * Answer the current question (step mode).
102
+ * In `mode: "all"` prefer `answerById()`.
103
+ */
104
+ answer(value: unknown): void;
105
+ /**
106
+ * Answer any visible question by its id — the right API for `mode: "all"`.
107
+ * Safe to call in step mode too (allows answering any step by id).
108
+ */
109
+ answerById(questionId: string, value: unknown): void;
110
+ private _setAnswer;
111
+ /**
112
+ * Advance to the next step (step mode).
113
+ * Validates the current required question first; populates `errors` if invalid.
114
+ */
115
+ next(): void;
116
+ /** Go to the previous step. No-op on step 0. */
117
+ back(): void;
118
+ /**
119
+ * Jump to a specific step index (0-based, within visible questions).
120
+ * Out-of-range indices are silently ignored.
121
+ */
122
+ jumpTo(index: number): void;
123
+ /**
124
+ * Validate all visible required questions and return a map of errors.
125
+ * Useful for "submit" gates in `mode: "all"`.
126
+ */
127
+ validate(): Record<string, string>;
128
+ /** Reset all responses and errors, restart from step 0. */
129
+ reset(): void;
130
+ }
131
+
132
+ export { type ConditionOperator, type DisplayMode, type Question, type QuestionOption, type QuestionType, type QuestionValidation, Questionnaire, type QuestionnaireConfig, type QuestionnaireState, type ShowIfCompound, type ShowIfCondition, type ShowIfRule, validateAnswer };