form0-core 0.3.1 → 0.4.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.
Files changed (60) hide show
  1. package/README.md +75 -0
  2. package/package.json +5 -2
  3. package/src/authoring/form-authoring.js +124 -13
  4. package/src/builtins/builtin-definitions.js +320 -0
  5. package/src/builtins/choice/choicelabel.js +2 -17
  6. package/src/builtins/choice/choicelabels.js +2 -17
  7. package/src/builtins/choice/choicevalue.js +2 -20
  8. package/src/builtins/choice/choicevalues.js +2 -17
  9. package/src/builtins/choice/hasother.js +2 -20
  10. package/src/builtins/choice/other.js +2 -20
  11. package/src/builtins/control/eval.js +78 -31
  12. package/src/builtins/control/setresult.js +29 -17
  13. package/src/builtins/date/dateadd.js +24 -0
  14. package/src/builtins/date/days.js +22 -0
  15. package/src/builtins/event/control/off.js +3 -27
  16. package/src/builtins/event/control/on.js +3 -19
  17. package/src/builtins/event/event-operations-collector.js +33 -9
  18. package/src/builtins/event/field/setvalue.js +3 -32
  19. package/src/builtins/event/ui/alert.js +3 -11
  20. package/src/builtins/logical/and.js +2 -17
  21. package/src/builtins/logical/array.js +2 -35
  22. package/src/builtins/logical/count.js +2 -20
  23. package/src/builtins/logical/counta.js +2 -20
  24. package/src/builtins/logical/countblank.js +2 -20
  25. package/src/builtins/logical/if.js +2 -20
  26. package/src/builtins/logical/or.js +2 -17
  27. package/src/builtins/math/abs.js +2 -20
  28. package/src/builtins/math/ceiling.js +2 -20
  29. package/src/builtins/math/cos.js +2 -20
  30. package/src/builtins/math/round.js +2 -23
  31. package/src/builtins/math/sin.js +2 -20
  32. package/src/builtins/registry.js +23 -40
  33. package/src/builtins/schema/datanames.js +9 -20
  34. package/src/builtins/schema/form.js +2 -20
  35. package/src/builtins/string/upper.js +2 -20
  36. package/src/engine/calculation-diagnostics.js +107 -0
  37. package/src/engine/calculation.js +65 -32
  38. package/src/engine/evaluator.js +56 -18
  39. package/src/engine/event-operation-scope.js +61 -0
  40. package/src/engine/event-source.js +164 -0
  41. package/src/engine/events.js +105 -19
  42. package/src/engine/form-engine.js +61 -16
  43. package/src/engine/warning-system.js +7 -5
  44. package/src/schema/field-specs.js +2 -2
  45. package/src/security/validation.js +10 -8
  46. package/src/utilities/calculation-editor.js +42 -6
  47. package/src/utilities/date-utils.js +72 -0
  48. package/src/builtins/docs/choice/choicelabel.mdx +0 -110
  49. package/src/builtins/docs/choice/choicelabels.mdx +0 -230
  50. package/src/builtins/docs/choice/choicevalue.mdx +0 -109
  51. package/src/builtins/docs/choice/choicevalues.mdx +0 -196
  52. package/src/builtins/docs/choice/hasother.mdx +0 -133
  53. package/src/builtins/docs/choice/other.mdx +0 -150
  54. package/src/builtins/docs/control/setresult.mdx +0 -203
  55. package/src/builtins/docs/event/field/setvalue.mdx +0 -239
  56. package/src/builtins/docs/index.mdx +0 -234
  57. package/src/builtins/docs/logical/and.mdx +0 -141
  58. package/src/builtins/docs/logical/if.mdx +0 -94
  59. package/src/builtins/docs/logical/or.mdx +0 -191
  60. package/src/builtins/docs/overview.mdx +0 -258
@@ -1,150 +0,0 @@
1
- ---
2
- title: "OTHER Builtin"
3
- description: "Retrieve the other option value from choice field (SingleChoiceField or MultiChoiceField)"
4
- category: "choice"
5
- tags: ["choice", "other", "custom", "multichoice"]
6
- ---
7
-
8
- # OTHER Function
9
-
10
- The `OTHER` builtin retrieves the other label if the user entered an other option in a choice field, returning `null` if no other option is present. Works with both SingleChoiceField (single selection) and MultiChoiceField (multiple selections).
11
-
12
- ## Syntax
13
-
14
- ```javascript
15
- OTHER(choiceField)
16
- ```
17
-
18
- ## Parameters
19
-
20
- | Parameter | Type | Description |
21
- |-----------|------|-------------|
22
- | `choiceField` | `Object` | The choice field object (SingleChoiceField with choice array or MultiChoiceField with choices array) and other array |
23
-
24
- ## Return Value
25
-
26
- Returns the other label as a string if user entered an other option, `null` otherwise.
27
-
28
- ## Examples
29
-
30
- ### Basic Usage - SingleChoiceField
31
-
32
- ```javascript
33
- // Get the other value from single choice field
34
- OTHER($city)
35
- ```
36
-
37
- ### Basic Usage - MultiChoiceField
38
-
39
- ```javascript
40
- // Get the other value from multi choice field
41
- OTHER($colors)
42
- ```
43
-
44
- ### Conditional Logic
45
-
46
- ```javascript
47
- // Use in conditional expressions for single choice
48
- IF(HASOTHER($city), "Custom city: " + OTHER($city), "No custom city entered")
49
-
50
- // Use in conditional expressions for multi choice
51
- IF(HASOTHER($colors), "Custom color: " + OTHER($colors), "No custom color entered")
52
- ```
53
-
54
- ### Display Logic
55
-
56
- ```javascript
57
- // Show other value in display
58
- "Additional info: " + OTHER($comments)
59
- ```
60
-
61
- ### Validation
62
-
63
- ```javascript
64
- // Validate other input length
65
- OTHER($reason).length > 10
66
- ```
67
-
68
- ## Common Use Cases
69
-
70
- - **Custom input display**: Show user-entered custom values
71
- - **Conditional logic**: Make decisions based on other option content
72
- - **Validation**: Validate the content of other options
73
- - **Data processing**: Extract custom values for further processing
74
- - **Multi-choice support**: Works seamlessly with both single and multiple choice fields
75
-
76
- ## Best Practices
77
-
78
- 1. **Check with HASOTHER**: Always use `HASOTHER()` to check if other option exists before using `OTHER()`
79
- 2. **Handle null values**: Always handle the case where `OTHER()` returns null
80
- 3. **Validation**: Validate other inputs for length, format, etc.
81
- 4. **User feedback**: Provide clear feedback about other option values
82
- 5. **Multi-choice compatibility**: Works with both SingleChoiceField and MultiChoiceField
83
-
84
- ## Related Functions
85
-
86
- - [`HASOTHER`](./hasother.mdx) - Check if other option is selected
87
- - [`CHOICEVALUE`](./choicevalue.mdx) - Get the selected choice value (SingleChoiceField)
88
- - [`CHOICELABEL`](./choicelabel.mdx) - Get the selected choice label (SingleChoiceField)
89
- - [`CHOICEVALUES`](./choicevalues.mdx) - Get all selected choice values (MultiChoiceField)
90
- - [`CHOICELABELS`](./choicelabels.mdx) - Get all selected choice labels (MultiChoiceField)
91
-
92
- ## Examples by Use Case
93
-
94
- ### Custom Input Display - SingleChoiceField
95
-
96
- ```javascript
97
- // Display custom input with fallback
98
- IF(HASOTHER($industry), "Industry: " + OTHER($industry), "Industry: " + CHOICELABEL($industry))
99
- ```
100
-
101
- ### Custom Input Display - MultiChoiceField
102
-
103
- ```javascript
104
- // Display custom input with fallback for multi-choice
105
- IF(HASOTHER($skills), "Custom skill: " + OTHER($skills), "Skills: " + CHOICELABELS($skills).join(", "))
106
- ```
107
-
108
- ### Validation Rules
109
-
110
- ```javascript
111
- // Ensure other input meets requirements
112
- IF(HASOTHER($reason), OTHER($reason).length >= 5, true)
113
- ```
114
-
115
- ### Data Processing - SingleChoiceField
116
-
117
- ```javascript
118
- // Process other values differently
119
- IF(HASOTHER($category), "CUSTOM: " + OTHER($category), "STANDARD: " + CHOICEVALUE($category))
120
- ```
121
-
122
- ### Data Processing - MultiChoiceField
123
-
124
- ```javascript
125
- // Process other values differently for multi-choice
126
- IF(HASOTHER($categories), "CUSTOM: " + OTHER($categories), "STANDARD: " + CHOICEVALUES($categories).join(", "))
127
- ```
128
-
129
- ### Form Summary
130
-
131
- ```javascript
132
- // Include other values in form summary
133
- "Additional comments: " + (HASOTHER($comments) ? OTHER($comments) : "None")
134
- ```
135
-
136
- ## Null Handling
137
-
138
- Always handle the null case when using `OTHER()`:
139
-
140
- ```javascript
141
- // Safe usage with null check
142
- IF(HASOTHER($field), OTHER($field) || "No value", "Standard selection")
143
- ```
144
-
145
- ## Field Type Compatibility
146
-
147
- - **SingleChoiceField**: Single selection with optional other input
148
- - **MultiChoiceField**: Multiple selections with optional other input
149
- - **Note**: User can only add 1 other option regardless of field type
150
- - **Return value**: Always returns a single string (the other label) or null
@@ -1,203 +0,0 @@
1
- ---
2
- title: "SETRESULT Builtin"
3
- description: "Set calculation results in form0 expressions"
4
- category: "control"
5
- tags: ["calculation", "result", "output"]
6
- ---
7
-
8
- # SETRESULT Function
9
-
10
- The `SETRESULT` builtin sets a result value that can be consumed by the form0 engine, typically used in calculations to define the final output value.
11
-
12
- ## Syntax
13
-
14
- ```javascript
15
- SETRESULT(value)
16
- ```
17
-
18
- ## Parameters
19
-
20
- | Parameter | Type | Description |
21
- |-----------|------|-------------|
22
- | `value` | `any` | The value to set as the result |
23
-
24
- ## Return Value
25
-
26
- Returns the same value that was passed in, while internally storing it as the calculation result.
27
-
28
- ## Examples
29
-
30
- ### Basic Calculation
31
-
32
- ```javascript
33
- // Calculate total price with tax
34
- SETRESULT(price * quantity * (1 + taxRate))
35
- ```
36
-
37
- ### Conditional Results
38
-
39
- ```javascript
40
- // Set discount amount based on user type
41
- SETRESULT(IF(user.isPremium, price * 0.2, price * 0.05))
42
- ```
43
-
44
- ### Complex Business Logic
45
-
46
- ```javascript
47
- // Calculate shipping cost with multiple factors
48
- SETRESULT(
49
- IF(country === "US",
50
- IF(orderValue > 50, 0, 5.99),
51
- IF(orderValue > 100, 10, 15.99)
52
- )
53
- )
54
- ```
55
-
56
- ### Score Calculations
57
-
58
- ```javascript
59
- // Calculate user credit score
60
- SETRESULT(
61
- (creditHistory * 0.35) +
62
- (paymentHistory * 0.35) +
63
- (creditUtilization * 0.30)
64
- )
65
- ```
66
-
67
- ### Dynamic Field Values
68
-
69
- ```javascript
70
- // Set field value based on other fields
71
- SETRESULT(
72
- IF(AND(firstName.length > 0, lastName.length > 0),
73
- firstName + " " + lastName,
74
- "Anonymous User"
75
- )
76
- )
77
- ```
78
-
79
- ## How It Works
80
-
81
- 1. **Calculation Execution**: The form0 engine executes your calculation expression
82
- 2. **Result Storage**: `SETRESULT` stores the value internally
83
- 3. **Result Consumption**: The engine retrieves the stored result
84
- 4. **Value Return**: The function also returns the value for use in expressions
85
-
86
- ## Common Use Cases
87
-
88
- - **Price calculations**: Total cost, discounts, taxes
89
- - **Score computations**: Credit scores, ratings, assessments
90
- - **Field aggregations**: Combining multiple fields into one result
91
- - **Conditional outputs**: Different results based on conditions
92
- - **Business rule results**: Complex logic outcomes
93
-
94
- ## Best Practices
95
-
96
- 1. **Single result per calculation**: Use only one `SETRESULT` per expression
97
- 2. **Clear value types**: Ensure the result type matches expectations
98
- 3. **Handle edge cases**: Consider null/undefined values
99
- 4. **Document complex logic**: Add comments for complex calculations
100
-
101
- ## Important Notes
102
-
103
- - **Single use**: Only one `SETRESULT` should be called per calculation
104
- - **Last wins**: If called multiple times, the last value is used
105
- - **Pass-through**: The function returns the value, so it can be used in expressions
106
- - **Type preservation**: The result maintains its original type
107
-
108
- ## Related Functions
109
-
110
- - [`IF`](../logical/if.mdx) - Conditional logic for result selection
111
- - [`AND`](../logical/and.mdx) - Multiple condition checks
112
- - [`OR`](../logical/or.mdx) - Alternative condition checks
113
-
114
- ## Examples by Use Case
115
-
116
- ### E-commerce Pricing
117
-
118
- ```javascript
119
- // Calculate final price with all factors
120
- SETRESULT(
121
- (basePrice * quantity) +
122
- shippingCost +
123
- (taxableAmount * taxRate) -
124
- discountAmount
125
- )
126
- ```
127
-
128
- ### User Scoring
129
-
130
- ```javascript
131
- // Calculate engagement score
132
- SETRESULT(
133
- (loginFrequency * 0.3) +
134
- (activityLevel * 0.4) +
135
- (socialSharing * 0.3)
136
- )
137
- ```
138
-
139
- ### Form Validation Results
140
-
141
- ```javascript
142
- // Set validation status
143
- SETRESULT(
144
- IF(AND(email.isValid, phone.isValid, name.length > 0),
145
- "valid",
146
- "invalid"
147
- )
148
- )
149
- ```
150
-
151
- ### Financial Calculations
152
-
153
- ```javascript
154
- // Calculate loan eligibility
155
- SETRESULT(
156
- IF(AND(creditScore > 650, income > 50000, debtRatio < 0.4),
157
- "approved",
158
- "denied"
159
- )
160
- )
161
- ```
162
-
163
- ### Progress Tracking
164
-
165
- ```javascript
166
- // Calculate completion percentage
167
- SETRESULT(
168
- Math.round(
169
- (completedSteps / totalSteps) * 100
170
- )
171
- )
172
- ```
173
-
174
- ## Advanced Examples
175
-
176
- ### Multi-step Calculation
177
-
178
- ```javascript
179
- // Calculate insurance premium
180
- SETRESULT(
181
- IF(age < 25,
182
- basePremium * 1.5,
183
- IF(age > 65,
184
- basePremium * 1.2,
185
- basePremium
186
- )
187
- ) *
188
- IF(hasAccidents, 1.3, 1.0) *
189
- IF(location === "urban", 1.1, 0.9)
190
- )
191
- ```
192
-
193
- ### Data Aggregation
194
-
195
- ```javascript
196
- // Calculate average rating
197
- SETRESULT(
198
- IF(reviewCount > 0,
199
- (rating1 + rating2 + rating3 + rating4 + rating5) / reviewCount,
200
- 0
201
- )
202
- )
203
- ```
@@ -1,239 +0,0 @@
1
- ---
2
- title: "SETVALUE Builtin"
3
- description: "Set field values in form0 event contexts"
4
- category: "event"
5
- tags: ["event", "field", "value", "manipulation"]
6
- ---
7
-
8
- # SETVALUE Function
9
-
10
- The `SETVALUE` builtin sets a field value within event context, automatically collecting the operation for platform execution. This function supports multiple field types including TextField, NumericField, SingleChoiceField, and MultiChoiceField.
11
-
12
- ## Syntax
13
-
14
- ```javascript
15
- SETVALUE('field_dataname', value)
16
- ```
17
-
18
- ## Parameters
19
-
20
- | Parameter | Type | Description |
21
- |-----------|------|-------------|
22
- | `fieldDataName` | `string` | The data name of the field to set |
23
- | `valueToSet` | `any` | The value to set (string, number, array, etc.) |
24
-
25
- ## Return Value
26
-
27
- Returns an operation descriptor for platform execution (for backward compatibility), while internally collecting the operation for automatic execution.
28
-
29
- ## Examples
30
-
31
- ### TextField Value
32
-
33
- ```javascript
34
- // Set text field value
35
- SETVALUE('field_dataname', 'value_to_set')
36
- ```
37
-
38
- ### NumericField Value
39
-
40
- ```javascript
41
- // Set numeric field value
42
- SETVALUE('field_dataname', 12)
43
- ```
44
-
45
- ### SingleChoiceField Value
46
-
47
- ```javascript
48
- // Set single choice field value
49
- SETVALUE('field_dataname', 'choicefield_value_to_set')
50
- ```
51
-
52
- ### MultiChoiceField Value
53
-
54
- ```javascript
55
- // Set multiple choice field value
56
- SETVALUE('field_dataname', ['value1', 'value2', 'value3'])
57
- ```
58
-
59
- ### Conditional Value Setting
60
-
61
- ```javascript
62
- // Set field value based on condition
63
- SETVALUE('discount_field', IF(user.isPremium, 20, 10))
64
- ```
65
-
66
- ### Dynamic Value Setting
67
-
68
- ```javascript
69
- // Set field value based on other fields
70
- SETVALUE('full_name', firstName + " " + lastName)
71
- ```
72
-
73
- ## Field Type Compatibility
74
-
75
- The `SETVALUE` function supports different value formats for different field types:
76
-
77
- | Field Type | Value Format | Example |
78
- |------------|-------------|---------|
79
- | `TextField` | String | `'text value'` |
80
- | `NumericField` | Number | `42` or `3.14` |
81
- | `SingleChoiceField` | String choice value | `'option1'` |
82
- | `MultiChoiceField` | Array of strings | `['option1', 'option2']` |
83
-
84
- ## How It Works
85
-
86
- 1. **Operation Creation**: Creates a field operation descriptor with type 'FIELD_OPERATION'
87
- 2. **Operation Collection**: Uses `__collectEventOperation` to collect the operation
88
- 3. **Platform Execution**: The platform retrieves and executes collected operations
89
- 4. **Value Formatting**: The platform handles appropriate value formatting for each field type
90
- 5. **Field Update**: The field value is updated in the form state
91
-
92
- ## Common Use Cases
93
-
94
- - **Field auto-population**: Setting field values based on other field changes
95
- - **Form validation responses**: Setting fields based on validation results
96
- - **Business rule applications**: Applying complex business logic to field values
97
- - **User interaction responses**: Updating fields based on user actions
98
- - **Data calculations**: Setting calculated values in fields
99
- - **Conditional field updates**: Setting different values based on conditions
100
-
101
- ## Best Practices
102
-
103
- 1. **Use correct field data names**: Ensure the field data name exists in your form
104
- 2. **Match field types**: Use appropriate value formats for each field type
105
- 3. **Handle null/undefined**: Consider edge cases in your value assignments
106
- 4. **Event context only**: This function is only available in event contexts
107
- 5. **Platform dependency**: The actual field update depends on platform implementation
108
-
109
- ## Important Notes
110
-
111
- - **Event context only**: This builtin is only available in event contexts, not in calculations
112
- - **Platform execution**: The actual field value setting is handled by the platform
113
- - **Operation collection**: Operations are automatically collected and executed
114
- - **Type handling**: The platform handles value formatting for different field types
115
- - **Asynchronous execution**: Field updates may occur asynchronously after event completion
116
-
117
- ## Related Functions
118
-
119
- - [`ALERT`](../ui/alert.mdx) - Display alert messages in event contexts
120
- - [`IF`](../../logical/if.mdx) - Conditional logic for value selection
121
- - [`AND`](../../logical/and.mdx) - Multiple condition checks
122
- - [`OR`](../../logical/or.mdx) - Alternative condition checks
123
-
124
- ## Examples by Use Case
125
-
126
- ### Auto-calculation Fields
127
-
128
- ```javascript
129
- // Calculate and set total price
130
- SETVALUE('total_price', price * quantity * (1 + taxRate))
131
- ```
132
-
133
- ### Form Validation Responses
134
-
135
- ```javascript
136
- // Set error message field based on validation
137
- SETVALUE('error_message',
138
- IF(email.isValid, '', 'Please enter a valid email address')
139
- )
140
- ```
141
-
142
- ### User Type Based Updates
143
-
144
- ```javascript
145
- // Set discount based on user type
146
- SETVALUE('discount_amount',
147
- IF(user.isPremium, orderTotal * 0.2, orderTotal * 0.05)
148
- )
149
- ```
150
-
151
- ### Multi-field Calculations
152
-
153
- ```javascript
154
- // Calculate and set BMI
155
- SETVALUE('bmi', weight / (height * height))
156
- ```
157
-
158
- ### Choice Field Updates
159
-
160
- ```javascript
161
- // Set available options based on category
162
- SETVALUE('subcategory_options',
163
- IF(category === 'electronics',
164
- ['phone', 'laptop', 'tablet'],
165
- ['shirt', 'pants', 'shoes']
166
- )
167
- )
168
- ```
169
-
170
- ### Data Aggregation
171
-
172
- ```javascript
173
- // Set summary field
174
- SETVALUE('order_summary',
175
- 'Items: ' + itemCount + ', Total: $' + totalAmount
176
- )
177
- ```
178
-
179
- ## Advanced Examples
180
-
181
- ### Complex Business Logic
182
-
183
- ```javascript
184
- // Calculate shipping cost with multiple factors
185
- SETVALUE('shipping_cost',
186
- IF(country === "US",
187
- IF(orderValue > 50, 0, 5.99),
188
- IF(orderValue > 100, 10, 15.99)
189
- )
190
- )
191
- ```
192
-
193
- ### Multi-step Field Updates
194
-
195
- ```javascript
196
- // Update multiple related fields
197
- SETVALUE('first_name', extractedFirstName)
198
- SETVALUE('last_name', extractedLastName)
199
- SETVALUE('full_name', extractedFirstName + " " + extractedLastName)
200
- ```
201
-
202
- ### Conditional Multi-choice Updates
203
-
204
- ```javascript
205
- // Set permissions based on user role
206
- SETVALUE('user_permissions',
207
- IF(userRole === 'admin',
208
- ['read', 'write', 'delete', 'admin'],
209
- IF(userRole === 'editor',
210
- ['read', 'write'],
211
- ['read']
212
- )
213
- )
214
- )
215
- ```
216
-
217
- ### Dynamic Field Population
218
-
219
- ```javascript
220
- // Set field based on external data
221
- SETVALUE('user_profile',
222
- IF(userId.length > 0,
223
- lookupUserProfile(userId),
224
- 'No user selected'
225
- )
226
- )
227
- ```
228
-
229
- ### Form State Management
230
-
231
- ```javascript
232
- // Update form status field
233
- SETVALUE('form_status',
234
- IF(allFieldsValid,
235
- 'ready_for_submission',
236
- 'incomplete'
237
- )
238
- )
239
- ```