@bardsballad/cadence 0.1.0 → 0.1.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/LICENSE +21 -0
- package/README.md +193 -0
- package/package.json +1 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 BardsBallad
|
|
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,193 @@
|
|
|
1
|
+
# Cadence
|
|
2
|
+
|
|
3
|
+
A safe, deterministic expression engine for user-generated content.
|
|
4
|
+
|
|
5
|
+
Cadence is a lightweight, sandboxed expression evaluator designed for TTRPG systems and other applications that need to safely execute user-defined calculations without the risks of arbitrary code execution.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- **Deterministic Evaluation**: Consistent results for the same inputs every time
|
|
10
|
+
- **Safe Sandbox**: No file system access, no arbitrary function execution, no side effects
|
|
11
|
+
- **Type-Safe**: Built with TypeScript for robust type checking
|
|
12
|
+
- **Array Operations**: Built-in helpers for sum, count, min, max, avg, any, all
|
|
13
|
+
- **Math Functions**: Support for floor, ceil, abs, round, and more
|
|
14
|
+
- **Conditional Logic**: Ternary operators and boolean operations
|
|
15
|
+
- **Variable Binding**: Assign intermediate results with named variables
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @bardsballad/cadence
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Quick Start
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { runCadence } from '@bardsballad/cadence';
|
|
27
|
+
|
|
28
|
+
const program = `
|
|
29
|
+
floor((score - 10) / 2) [modifier];
|
|
30
|
+
modifier < 0 ? "-" : "+" [sign];
|
|
31
|
+
sign + abs(modifier)
|
|
32
|
+
`;
|
|
33
|
+
|
|
34
|
+
const result = runCadence(program, { score: 14 });
|
|
35
|
+
console.log(result); // "+2"
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Syntax
|
|
39
|
+
|
|
40
|
+
### Basic Expressions
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
// Arithmetic
|
|
44
|
+
2 + 3 * 4
|
|
45
|
+
10 - 5
|
|
46
|
+
20 / 4
|
|
47
|
+
3 * 3
|
|
48
|
+
|
|
49
|
+
// Comparison
|
|
50
|
+
x > 5
|
|
51
|
+
y <= 10
|
|
52
|
+
a === b
|
|
53
|
+
c !== d
|
|
54
|
+
|
|
55
|
+
// Boolean Logic
|
|
56
|
+
true && false
|
|
57
|
+
true || false
|
|
58
|
+
!condition
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Variable Binding
|
|
62
|
+
|
|
63
|
+
Use square brackets to bind intermediate results:
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
score - 10 [adjusted];
|
|
67
|
+
adjusted / 2 [halved];
|
|
68
|
+
floor(halved)
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Conditionals
|
|
72
|
+
|
|
73
|
+
Ternary operator for conditional evaluation:
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
x > 5 ? "big" : "small"
|
|
77
|
+
score >= 20 ? 10 : score >= 10 ? 5 : 0
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Array Operations
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
// Available helpers
|
|
84
|
+
sum([1, 2, 3]) // 6
|
|
85
|
+
count([a, b, c]) // 3
|
|
86
|
+
min([5, 2, 8]) // 2
|
|
87
|
+
max([5, 2, 8]) // 8
|
|
88
|
+
avg([10, 20, 30]) // 20
|
|
89
|
+
any([false, false, true]) // true
|
|
90
|
+
all([true, true, false]) // false
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Math Functions
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
floor(3.7) // 3
|
|
97
|
+
ceil(3.2) // 4
|
|
98
|
+
round(3.5) // 4 (banker's rounding)
|
|
99
|
+
abs(-5) // 5
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## API
|
|
103
|
+
|
|
104
|
+
### `runCadence(program: string, input: Record<string, any>): any`
|
|
105
|
+
|
|
106
|
+
Executes a Cadence program with the provided input variables.
|
|
107
|
+
|
|
108
|
+
**Parameters:**
|
|
109
|
+
- `program`: The expression string to evaluate
|
|
110
|
+
- `input`: An object containing variables available to the program
|
|
111
|
+
|
|
112
|
+
**Returns:** The result of evaluating the final expression
|
|
113
|
+
|
|
114
|
+
**Example:**
|
|
115
|
+
|
|
116
|
+
```typescript
|
|
117
|
+
const damage = runCadence(
|
|
118
|
+
`base_damage + (strength_modifier > 0 ? strength_modifier : 0)`,
|
|
119
|
+
{ base_damage: 8, strength_modifier: 3 }
|
|
120
|
+
); // 11
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## Examples
|
|
124
|
+
|
|
125
|
+
### D&D Ability Modifier Calculation
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
import { runCadence } from '@bardsballad/cadence';
|
|
129
|
+
|
|
130
|
+
const modifier = runCadence(
|
|
131
|
+
`floor((ability_score - 10) / 2)`,
|
|
132
|
+
{ ability_score: 16 }
|
|
133
|
+
); // 3
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### Damage Calculation with Modifiers
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
const damage = runCadence(
|
|
140
|
+
`base_damage [d];
|
|
141
|
+
d + strength_mod + (is_critical ? d : 0)`,
|
|
142
|
+
{ base_damage: 6, strength_mod: 2, is_critical: true }
|
|
143
|
+
); // 14
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### Complex Conditional Pricing
|
|
147
|
+
|
|
148
|
+
```typescript
|
|
149
|
+
const price = runCadence(
|
|
150
|
+
`base_price [b];
|
|
151
|
+
quantity > 100 ? b * 0.9 : quantity > 10 ? b * 0.95 : b`,
|
|
152
|
+
{ base_price: 100, quantity: 50 }
|
|
153
|
+
); // 95
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## Safety
|
|
157
|
+
|
|
158
|
+
Cadence enforces strict safety boundaries:
|
|
159
|
+
|
|
160
|
+
- **No Global Access**: Variables must be explicitly passed via the `input` object
|
|
161
|
+
- **No Function Definition**: Users cannot define custom functions
|
|
162
|
+
- **No Mutations**: All operations are pure and side-effect free
|
|
163
|
+
- **No External Calls**: No file system, network, or environment access
|
|
164
|
+
- **Type Validation**: Helper functions validate argument types
|
|
165
|
+
|
|
166
|
+
This makes Cadence suitable for user-facing expression editors where you need to prevent malicious or accidental code execution.
|
|
167
|
+
|
|
168
|
+
## Development
|
|
169
|
+
|
|
170
|
+
### Build
|
|
171
|
+
|
|
172
|
+
```bash
|
|
173
|
+
npm run build
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### Test
|
|
177
|
+
|
|
178
|
+
```bash
|
|
179
|
+
npm test
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
## License
|
|
183
|
+
|
|
184
|
+
MIT
|
|
185
|
+
|
|
186
|
+
## Contributing
|
|
187
|
+
|
|
188
|
+
Contributions welcome! Please ensure all tests pass before submitting pull requests.
|
|
189
|
+
|
|
190
|
+
```bash
|
|
191
|
+
npm test
|
|
192
|
+
npm run build
|
|
193
|
+
```
|