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.
- package/README.md +75 -0
- package/package.json +5 -2
- package/src/authoring/form-authoring.js +124 -13
- package/src/builtins/builtin-definitions.js +320 -0
- package/src/builtins/choice/choicelabel.js +2 -17
- package/src/builtins/choice/choicelabels.js +2 -17
- package/src/builtins/choice/choicevalue.js +2 -20
- package/src/builtins/choice/choicevalues.js +2 -17
- package/src/builtins/choice/hasother.js +2 -20
- package/src/builtins/choice/other.js +2 -20
- package/src/builtins/control/eval.js +78 -31
- package/src/builtins/control/setresult.js +29 -17
- package/src/builtins/date/dateadd.js +24 -0
- package/src/builtins/date/days.js +22 -0
- package/src/builtins/event/control/off.js +3 -27
- package/src/builtins/event/control/on.js +3 -19
- package/src/builtins/event/event-operations-collector.js +33 -9
- package/src/builtins/event/field/setvalue.js +3 -32
- package/src/builtins/event/ui/alert.js +3 -11
- package/src/builtins/logical/and.js +2 -17
- package/src/builtins/logical/array.js +2 -35
- package/src/builtins/logical/count.js +2 -20
- package/src/builtins/logical/counta.js +2 -20
- package/src/builtins/logical/countblank.js +2 -20
- package/src/builtins/logical/if.js +2 -20
- package/src/builtins/logical/or.js +2 -17
- package/src/builtins/math/abs.js +2 -20
- package/src/builtins/math/ceiling.js +2 -20
- package/src/builtins/math/cos.js +2 -20
- package/src/builtins/math/round.js +2 -23
- package/src/builtins/math/sin.js +2 -20
- package/src/builtins/registry.js +23 -40
- package/src/builtins/schema/datanames.js +9 -20
- package/src/builtins/schema/form.js +2 -20
- package/src/builtins/string/upper.js +2 -20
- package/src/engine/calculation-diagnostics.js +107 -0
- package/src/engine/calculation.js +65 -32
- package/src/engine/evaluator.js +56 -18
- package/src/engine/event-operation-scope.js +61 -0
- package/src/engine/event-source.js +164 -0
- package/src/engine/events.js +105 -19
- package/src/engine/form-engine.js +61 -16
- package/src/engine/warning-system.js +7 -5
- package/src/schema/field-specs.js +2 -2
- package/src/security/validation.js +10 -8
- package/src/utilities/calculation-editor.js +42 -6
- package/src/utilities/date-utils.js +72 -0
- package/src/builtins/docs/choice/choicelabel.mdx +0 -110
- package/src/builtins/docs/choice/choicelabels.mdx +0 -230
- package/src/builtins/docs/choice/choicevalue.mdx +0 -109
- package/src/builtins/docs/choice/choicevalues.mdx +0 -196
- package/src/builtins/docs/choice/hasother.mdx +0 -133
- package/src/builtins/docs/choice/other.mdx +0 -150
- package/src/builtins/docs/control/setresult.mdx +0 -203
- package/src/builtins/docs/event/field/setvalue.mdx +0 -239
- package/src/builtins/docs/index.mdx +0 -234
- package/src/builtins/docs/logical/and.mdx +0 -141
- package/src/builtins/docs/logical/if.mdx +0 -94
- package/src/builtins/docs/logical/or.mdx +0 -191
- package/src/builtins/docs/overview.mdx +0 -258
|
@@ -1,258 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: "form0 Builtins Overview"
|
|
3
|
-
description: "Built-in functions for form0 expressions and calculations"
|
|
4
|
-
category: "overview"
|
|
5
|
-
tags: ["builtins", "functions", "expressions", "form0"]
|
|
6
|
-
---
|
|
7
|
-
|
|
8
|
-
# form0 Builtins Overview
|
|
9
|
-
|
|
10
|
-
Builtins are pre-defined functions that provide essential functionality for form0 expressions, calculations, and business logic. They are available in all form0 contexts without any imports or setup.
|
|
11
|
-
|
|
12
|
-
## What Are Builtins?
|
|
13
|
-
|
|
14
|
-
Builtins are JavaScript functions that extend the capabilities of form0 expressions beyond basic arithmetic and string operations. They enable:
|
|
15
|
-
|
|
16
|
-
- **Conditional logic** for dynamic form behavior
|
|
17
|
-
- **Boolean operations** for complex validation rules
|
|
18
|
-
- **Result management** for calculations and data processing
|
|
19
|
-
- **Business logic** implementation within form schemas
|
|
20
|
-
|
|
21
|
-
## Categories
|
|
22
|
-
|
|
23
|
-
### Logical Functions
|
|
24
|
-
|
|
25
|
-
Functions that handle boolean logic and conditional operations:
|
|
26
|
-
|
|
27
|
-
- **[`IF`](../logical/if.mdx)** - Conditional logic and branching
|
|
28
|
-
- **[`AND`](../logical/and.mdx)** - Logical AND operations
|
|
29
|
-
- **[`OR`](../logical/or.mdx)** - Logical OR operations
|
|
30
|
-
|
|
31
|
-
### Control Functions
|
|
32
|
-
|
|
33
|
-
Functions that manage calculation flow and results:
|
|
34
|
-
|
|
35
|
-
- **[`SETRESULT`](../control/setresult.mdx)** - Set calculation results
|
|
36
|
-
|
|
37
|
-
## Usage in form0
|
|
38
|
-
|
|
39
|
-
Builtins are used within form0 expressions in several contexts:
|
|
40
|
-
|
|
41
|
-
### 1. Field Calculations
|
|
42
|
-
|
|
43
|
-
```javascript
|
|
44
|
-
// Calculate total price with tax
|
|
45
|
-
{
|
|
46
|
-
"type": "calculation",
|
|
47
|
-
"expression": "SETRESULT(price * quantity * (1 + taxRate))"
|
|
48
|
-
}
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
### 2. Conditional Field Visibility
|
|
52
|
-
|
|
53
|
-
```javascript
|
|
54
|
-
// Show field only for premium users
|
|
55
|
-
{
|
|
56
|
-
"type": "condition",
|
|
57
|
-
"expression": "IF(user.isPremium, true, false)"
|
|
58
|
-
}
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
### 3. Validation Rules
|
|
62
|
-
|
|
63
|
-
```javascript
|
|
64
|
-
// Require either email or phone
|
|
65
|
-
{
|
|
66
|
-
"type": "validation",
|
|
67
|
-
"expression": "OR(email.length > 0, phone.length > 0)"
|
|
68
|
-
}
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
### 4. Dynamic Field Values
|
|
72
|
-
|
|
73
|
-
```javascript
|
|
74
|
-
// Set default value based on conditions
|
|
75
|
-
{
|
|
76
|
-
"type": "default",
|
|
77
|
-
"expression": "IF(user.country === 'US', 'USD', 'EUR')"
|
|
78
|
-
}
|
|
79
|
-
```
|
|
80
|
-
|
|
81
|
-
## Expression Syntax
|
|
82
|
-
|
|
83
|
-
Builtins follow standard JavaScript function syntax:
|
|
84
|
-
|
|
85
|
-
```javascript
|
|
86
|
-
// Function call with arguments
|
|
87
|
-
BUILTIN_NAME(arg1, arg2, ...)
|
|
88
|
-
|
|
89
|
-
// Can be nested
|
|
90
|
-
IF(AND(condition1, condition2), value1, value2)
|
|
91
|
-
|
|
92
|
-
// Can be part of larger expressions
|
|
93
|
-
SETRESULT(basePrice * IF(isPremium, 0.8, 1.0))
|
|
94
|
-
```
|
|
95
|
-
|
|
96
|
-
## Common Patterns
|
|
97
|
-
|
|
98
|
-
### Complex Conditional Logic
|
|
99
|
-
|
|
100
|
-
```javascript
|
|
101
|
-
// Multi-level conditions
|
|
102
|
-
IF(user.type === "premium",
|
|
103
|
-
IF(order.value > 100, "free", "discounted"),
|
|
104
|
-
IF(order.value > 50, "reduced", "standard")
|
|
105
|
-
)
|
|
106
|
-
```
|
|
107
|
-
|
|
108
|
-
### Validation Chains
|
|
109
|
-
|
|
110
|
-
```javascript
|
|
111
|
-
// Multiple validation criteria
|
|
112
|
-
AND(
|
|
113
|
-
email.includes("@"),
|
|
114
|
-
phone.length >= 10,
|
|
115
|
-
age >= 18,
|
|
116
|
-
agreedToTerms === true
|
|
117
|
-
)
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
### Fallback Logic
|
|
121
|
-
|
|
122
|
-
```javascript
|
|
123
|
-
// Provide alternatives
|
|
124
|
-
OR(
|
|
125
|
-
user.preferredContact,
|
|
126
|
-
user.email,
|
|
127
|
-
user.phone,
|
|
128
|
-
"No contact method"
|
|
129
|
-
)
|
|
130
|
-
```
|
|
131
|
-
|
|
132
|
-
## Best Practices
|
|
133
|
-
|
|
134
|
-
### 1. Keep Expressions Readable
|
|
135
|
-
|
|
136
|
-
```javascript
|
|
137
|
-
// Good: Clear and understandable
|
|
138
|
-
IF(user.isPremium, premiumPrice, standardPrice)
|
|
139
|
-
|
|
140
|
-
// Avoid: Too complex in one expression
|
|
141
|
-
IF(AND(OR(user.isPremium, user.isVip), user.age > 21, user.country === "US"),
|
|
142
|
-
IF(order.value > 100, price * 0.7, price * 0.8),
|
|
143
|
-
IF(user.isStudent, price * 0.9, price))
|
|
144
|
-
```
|
|
145
|
-
|
|
146
|
-
### 2. Use Descriptive Field Names
|
|
147
|
-
|
|
148
|
-
```javascript
|
|
149
|
-
// Good: Self-documenting
|
|
150
|
-
IF(hasValidLicense, "Approved", "Pending Review")
|
|
151
|
-
|
|
152
|
-
// Avoid: Cryptic names
|
|
153
|
-
IF(flag1, val1, val2)
|
|
154
|
-
```
|
|
155
|
-
|
|
156
|
-
### 3. Handle Edge Cases
|
|
157
|
-
|
|
158
|
-
```javascript
|
|
159
|
-
// Good: Handles null/undefined
|
|
160
|
-
IF(AND(user.age, user.age >= 18), "Adult", "Minor")
|
|
161
|
-
|
|
162
|
-
// Risky: Might fail with missing data
|
|
163
|
-
IF(user.age >= 18, "Adult", "Minor")
|
|
164
|
-
```
|
|
165
|
-
|
|
166
|
-
### 4. Use Comments in Complex Schemas
|
|
167
|
-
|
|
168
|
-
```json
|
|
169
|
-
{
|
|
170
|
-
"type": "calculation",
|
|
171
|
-
"description": "Calculate shipping cost based on location and order value",
|
|
172
|
-
"expression": "SETRESULT(IF(country === 'US', IF(orderValue > 50, 0, 5.99), 15.99))"
|
|
173
|
-
}
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
## Performance Considerations
|
|
177
|
-
|
|
178
|
-
- **Short-circuit evaluation**: `AND` and `OR` stop evaluating once the result is determined
|
|
179
|
-
- **Lazy evaluation**: Only necessary branches of `IF` statements are evaluated
|
|
180
|
-
- **Minimal overhead**: Builtins are optimized for performance in form contexts
|
|
181
|
-
|
|
182
|
-
## Security
|
|
183
|
-
|
|
184
|
-
Builtins provide defined interfaces for common form operations, and their arguments are validated
|
|
185
|
-
where applicable. Event builtins such as `SETVALUE` and `ALERT` return operations for the host to
|
|
186
|
-
apply instead of mutating application state directly.
|
|
187
|
-
|
|
188
|
-
Using a builtin does not sandbox the JavaScript expression or event script that calls it. Schemas
|
|
189
|
-
containing executable code must come from trusted authors. `SAFE` and `CUSTOM` modes provide
|
|
190
|
-
validation and configuration controls, not isolation. See the project
|
|
191
|
-
[security policy](../../../SECURITY.md) for the trust model and application responsibilities.
|
|
192
|
-
|
|
193
|
-
## Extending Builtins
|
|
194
|
-
|
|
195
|
-
While form0 comes with essential builtins, you can extend functionality by:
|
|
196
|
-
|
|
197
|
-
1. **Custom helpers**: Define additional functions in your form engine configuration
|
|
198
|
-
2. **Domain-specific functions**: Create business logic functions for your use case
|
|
199
|
-
3. **Integration functions**: Connect to external services or APIs
|
|
200
|
-
|
|
201
|
-
## Error Handling
|
|
202
|
-
|
|
203
|
-
Builtins handle errors gracefully:
|
|
204
|
-
|
|
205
|
-
- **Type coercion**: Automatic conversion when sensible
|
|
206
|
-
- **Fallback values**: Sensible defaults for invalid inputs
|
|
207
|
-
- **Error reporting**: Clear error messages for debugging
|
|
208
|
-
|
|
209
|
-
## Examples by Use Case
|
|
210
|
-
|
|
211
|
-
### E-commerce Forms
|
|
212
|
-
|
|
213
|
-
```javascript
|
|
214
|
-
// Dynamic pricing
|
|
215
|
-
SETRESULT(
|
|
216
|
-
basePrice *
|
|
217
|
-
IF(user.isPremium, 0.9, 1.0) *
|
|
218
|
-
IF(quantity > 10, 0.95, 1.0)
|
|
219
|
-
)
|
|
220
|
-
|
|
221
|
-
// Shipping eligibility
|
|
222
|
-
IF(AND(weight < 50, country === "US"), "Standard", "Express Only")
|
|
223
|
-
```
|
|
224
|
-
|
|
225
|
-
### User Registration
|
|
226
|
-
|
|
227
|
-
```javascript
|
|
228
|
-
// Validation rules
|
|
229
|
-
AND(
|
|
230
|
-
email.includes("@"),
|
|
231
|
-
password.length >= 8,
|
|
232
|
-
age >= 13,
|
|
233
|
-
OR(phone.length > 0, alternateContact.length > 0)
|
|
234
|
-
)
|
|
235
|
-
|
|
236
|
-
// Account type determination
|
|
237
|
-
IF(OR(emailDomain === "company.com", hasInviteCode), "Business", "Personal")
|
|
238
|
-
```
|
|
239
|
-
|
|
240
|
-
### Survey Forms
|
|
241
|
-
|
|
242
|
-
```javascript
|
|
243
|
-
// Conditional questions
|
|
244
|
-
IF(satisfaction < 3, "What could we improve?", "")
|
|
245
|
-
|
|
246
|
-
// Score calculation
|
|
247
|
-
SETRESULT(
|
|
248
|
-
(question1 * 0.3) +
|
|
249
|
-
(question2 * 0.4) +
|
|
250
|
-
(question3 * 0.3)
|
|
251
|
-
)
|
|
252
|
-
```
|
|
253
|
-
|
|
254
|
-
## Next Steps
|
|
255
|
-
|
|
256
|
-
- Explore individual builtin documentation for detailed usage
|
|
257
|
-
- Check out the [form0 examples](../examples.mdx) for real-world scenarios
|
|
258
|
-
- Learn about [custom helpers](../custom-helpers.mdx) for extending functionality
|