form0-core 0.3.0 → 0.4.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/README.md +63 -0
- package/package.json +5 -2
- 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/conditions.js +69 -3
- 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 +111 -23
- 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 -255
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: "OR Builtin"
|
|
3
|
-
description: "Logical OR operation for form0 expressions"
|
|
4
|
-
category: "logical"
|
|
5
|
-
tags: ["logical", "boolean", "validation"]
|
|
6
|
-
---
|
|
7
|
-
|
|
8
|
-
# OR Function
|
|
9
|
-
|
|
10
|
-
The `OR` builtin performs logical OR operations, returning `true` if at least one of the provided arguments is truthy.
|
|
11
|
-
|
|
12
|
-
## Syntax
|
|
13
|
-
|
|
14
|
-
```javascript
|
|
15
|
-
OR(...args)
|
|
16
|
-
```
|
|
17
|
-
|
|
18
|
-
## Parameters
|
|
19
|
-
|
|
20
|
-
| Parameter | Type | Description |
|
|
21
|
-
|-----------|------|-------------|
|
|
22
|
-
| `...args` | `any` | Any number of arguments to evaluate |
|
|
23
|
-
|
|
24
|
-
## Return Value
|
|
25
|
-
|
|
26
|
-
Returns `true` if at least one argument is truthy, `false` if all arguments are falsy.
|
|
27
|
-
|
|
28
|
-
## Examples
|
|
29
|
-
|
|
30
|
-
### Basic Usage
|
|
31
|
-
|
|
32
|
-
```javascript
|
|
33
|
-
// Check if user has any premium feature
|
|
34
|
-
OR(user.isPremium, user.isVip, user.hasSubscription)
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
### Form Validation
|
|
38
|
-
|
|
39
|
-
```javascript
|
|
40
|
-
// Require either email or phone
|
|
41
|
-
OR(email.length > 0, phone.length > 0)
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
### Multiple Authentication Methods
|
|
45
|
-
|
|
46
|
-
```javascript
|
|
47
|
-
// Check if user is authenticated via any method
|
|
48
|
-
OR(user.googleAuth, user.facebookAuth, user.localAuth)
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
### Field Visibility
|
|
52
|
-
|
|
53
|
-
```javascript
|
|
54
|
-
// Show field if any condition is met
|
|
55
|
-
OR(user.isAdmin, user.isOwner, feature.showToAll)
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
### Fallback Values
|
|
59
|
-
|
|
60
|
-
```javascript
|
|
61
|
-
// Use first available contact method
|
|
62
|
-
OR(user.email, user.phone, user.address)
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
## Truthiness Rules
|
|
66
|
-
|
|
67
|
-
The `OR` function uses JavaScript's standard truthiness evaluation:
|
|
68
|
-
|
|
69
|
-
**Falsy values:**
|
|
70
|
-
- `false`
|
|
71
|
-
- `0`
|
|
72
|
-
- `""` (empty string)
|
|
73
|
-
- `null`
|
|
74
|
-
- `undefined`
|
|
75
|
-
- `NaN`
|
|
76
|
-
|
|
77
|
-
**Truthy values:**
|
|
78
|
-
- `true`
|
|
79
|
-
- Any non-zero number
|
|
80
|
-
- Any non-empty string
|
|
81
|
-
- Objects and arrays
|
|
82
|
-
- Functions
|
|
83
|
-
|
|
84
|
-
## Common Use Cases
|
|
85
|
-
|
|
86
|
-
- **Alternative requirements**: Accept multiple ways to satisfy a condition
|
|
87
|
-
- **Feature detection**: Check if any of several features are available
|
|
88
|
-
- **Fallback logic**: Provide alternatives when primary options fail
|
|
89
|
-
- **Flexible validation**: Allow multiple valid formats or values
|
|
90
|
-
- **Permission systems**: Grant access if user has any qualifying permission
|
|
91
|
-
|
|
92
|
-
## Best Practices
|
|
93
|
-
|
|
94
|
-
1. **Order by likelihood**: Put most likely-to-succeed conditions first
|
|
95
|
-
2. **Use for alternatives**: Perfect for "either/or" scenarios
|
|
96
|
-
3. **Combine with AND**: Create complex logical expressions
|
|
97
|
-
4. **Keep conditions related**: All arguments should be logical alternatives
|
|
98
|
-
|
|
99
|
-
## Performance Notes
|
|
100
|
-
|
|
101
|
-
- **Short-circuit evaluation**: Stops checking as soon as a truthy value is found
|
|
102
|
-
- **Evaluation order**: Arguments are evaluated left to right
|
|
103
|
-
- **Lazy evaluation**: Later arguments won't be evaluated if earlier ones are truthy
|
|
104
|
-
|
|
105
|
-
## Related Functions
|
|
106
|
-
|
|
107
|
-
- [`AND`](./and.mdx) - Logical AND operations
|
|
108
|
-
- [`IF`](./if.mdx) - Conditional logic
|
|
109
|
-
|
|
110
|
-
## Examples by Use Case
|
|
111
|
-
|
|
112
|
-
### Contact Information Validation
|
|
113
|
-
|
|
114
|
-
```javascript
|
|
115
|
-
// Require at least one contact method
|
|
116
|
-
OR(
|
|
117
|
-
email.includes("@"),
|
|
118
|
-
phone.length >= 10,
|
|
119
|
-
address.length > 0
|
|
120
|
-
)
|
|
121
|
-
```
|
|
122
|
-
|
|
123
|
-
### User Permissions
|
|
124
|
-
|
|
125
|
-
```javascript
|
|
126
|
-
// Check if user has any admin permission
|
|
127
|
-
OR(
|
|
128
|
-
user.role === "admin",
|
|
129
|
-
user.permissions.includes("admin"),
|
|
130
|
-
user.isSuperUser
|
|
131
|
-
)
|
|
132
|
-
```
|
|
133
|
-
|
|
134
|
-
### Payment Methods
|
|
135
|
-
|
|
136
|
-
```javascript
|
|
137
|
-
// Check if any payment method is available
|
|
138
|
-
OR(
|
|
139
|
-
creditCard.isValid,
|
|
140
|
-
paypal.isConnected,
|
|
141
|
-
bankAccount.isVerified,
|
|
142
|
-
wallet.hasBalance
|
|
143
|
-
)
|
|
144
|
-
```
|
|
145
|
-
|
|
146
|
-
### Feature Flags
|
|
147
|
-
|
|
148
|
-
```javascript
|
|
149
|
-
// Enable feature if any condition is met
|
|
150
|
-
OR(
|
|
151
|
-
user.betaOptIn,
|
|
152
|
-
user.isDeveloper,
|
|
153
|
-
feature.enabledForAll,
|
|
154
|
-
environment === "staging"
|
|
155
|
-
)
|
|
156
|
-
```
|
|
157
|
-
|
|
158
|
-
### Emergency Contacts
|
|
159
|
-
|
|
160
|
-
```javascript
|
|
161
|
-
// Show emergency contact section if any contact exists
|
|
162
|
-
OR(
|
|
163
|
-
emergencyContact.phone,
|
|
164
|
-
emergencyContact.email,
|
|
165
|
-
emergencyContact.relationship
|
|
166
|
-
)
|
|
167
|
-
```
|
|
168
|
-
|
|
169
|
-
## Complex Examples
|
|
170
|
-
|
|
171
|
-
### Nested Logic
|
|
172
|
-
|
|
173
|
-
```javascript
|
|
174
|
-
// Complex user eligibility check
|
|
175
|
-
OR(
|
|
176
|
-
AND(user.isPremium, user.accountAge > 30),
|
|
177
|
-
AND(user.referrals >= 5, user.isActive),
|
|
178
|
-
user.specialStatus === "founder"
|
|
179
|
-
)
|
|
180
|
-
```
|
|
181
|
-
|
|
182
|
-
### Multi-step Form Logic
|
|
183
|
-
|
|
184
|
-
```javascript
|
|
185
|
-
// Show next step if user completed any path
|
|
186
|
-
OR(
|
|
187
|
-
AND(step1.email, step1.verified),
|
|
188
|
-
AND(step1.phone, step1.smsVerified),
|
|
189
|
-
step1.socialLogin
|
|
190
|
-
)
|
|
191
|
-
```
|
|
@@ -1,255 +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
|
-
All builtins are designed to be safe for use in form contexts:
|
|
185
|
-
|
|
186
|
-
- **No side effects**: Builtins don't modify external state
|
|
187
|
-
- **Sandboxed execution**: Cannot access system resources
|
|
188
|
-
- **Input validation**: Arguments are validated before processing
|
|
189
|
-
|
|
190
|
-
## Extending Builtins
|
|
191
|
-
|
|
192
|
-
While form0 comes with essential builtins, you can extend functionality by:
|
|
193
|
-
|
|
194
|
-
1. **Custom helpers**: Define additional functions in your form engine configuration
|
|
195
|
-
2. **Domain-specific functions**: Create business logic functions for your use case
|
|
196
|
-
3. **Integration functions**: Connect to external services or APIs
|
|
197
|
-
|
|
198
|
-
## Error Handling
|
|
199
|
-
|
|
200
|
-
Builtins handle errors gracefully:
|
|
201
|
-
|
|
202
|
-
- **Type coercion**: Automatic conversion when sensible
|
|
203
|
-
- **Fallback values**: Sensible defaults for invalid inputs
|
|
204
|
-
- **Error reporting**: Clear error messages for debugging
|
|
205
|
-
|
|
206
|
-
## Examples by Use Case
|
|
207
|
-
|
|
208
|
-
### E-commerce Forms
|
|
209
|
-
|
|
210
|
-
```javascript
|
|
211
|
-
// Dynamic pricing
|
|
212
|
-
SETRESULT(
|
|
213
|
-
basePrice *
|
|
214
|
-
IF(user.isPremium, 0.9, 1.0) *
|
|
215
|
-
IF(quantity > 10, 0.95, 1.0)
|
|
216
|
-
)
|
|
217
|
-
|
|
218
|
-
// Shipping eligibility
|
|
219
|
-
IF(AND(weight < 50, country === "US"), "Standard", "Express Only")
|
|
220
|
-
```
|
|
221
|
-
|
|
222
|
-
### User Registration
|
|
223
|
-
|
|
224
|
-
```javascript
|
|
225
|
-
// Validation rules
|
|
226
|
-
AND(
|
|
227
|
-
email.includes("@"),
|
|
228
|
-
password.length >= 8,
|
|
229
|
-
age >= 13,
|
|
230
|
-
OR(phone.length > 0, alternateContact.length > 0)
|
|
231
|
-
)
|
|
232
|
-
|
|
233
|
-
// Account type determination
|
|
234
|
-
IF(OR(emailDomain === "company.com", hasInviteCode), "Business", "Personal")
|
|
235
|
-
```
|
|
236
|
-
|
|
237
|
-
### Survey Forms
|
|
238
|
-
|
|
239
|
-
```javascript
|
|
240
|
-
// Conditional questions
|
|
241
|
-
IF(satisfaction < 3, "What could we improve?", "")
|
|
242
|
-
|
|
243
|
-
// Score calculation
|
|
244
|
-
SETRESULT(
|
|
245
|
-
(question1 * 0.3) +
|
|
246
|
-
(question2 * 0.4) +
|
|
247
|
-
(question3 * 0.3)
|
|
248
|
-
)
|
|
249
|
-
```
|
|
250
|
-
|
|
251
|
-
## Next Steps
|
|
252
|
-
|
|
253
|
-
- Explore individual builtin documentation for detailed usage
|
|
254
|
-
- Check out the [form0 examples](../examples.mdx) for real-world scenarios
|
|
255
|
-
- Learn about [custom helpers](../custom-helpers.mdx) for extending functionality
|