Commit 61fed00
committed
feat(woql): Add vars_unique() for generating unique variable names fixes #261
Implements a new WOQL.vars_unique() function that creates variables with
guaranteed unique names using an incrementing counter. This solves the problem
where vars() generates generic variables that can collide across scopes.
## Implementation:
**VarUnique Constructor (woqlDoc.js):**
- Global counter for unique variable generation
- Appends incrementing counter to base name (e.g., 'x' -> 'x_1', 'x_2')
- Stores baseName, name, and counter properties
- Generates proper JSON with unique variable names
**WOQL.vars_unique() Function (woql.js):**
- Maps input names to VarUnique instances
- Works identically to vars() but with uniqueness guarantee
- Useful with select() to firewall local variables from other scopes
**Convert Function Update:**
- Handles VarUnique instances like Var instances
- Ensures proper JSON serialization
## Benefits:
✅ **Guaranteed Uniqueness:** Variables are unique across all scopes, even with same input names
✅ **Scope Isolation:** Works with select() to prevent variable collision
✅ **Backward Compatible:** Original vars() unchanged and works exactly as before
✅ **Well Documented:** Comprehensive JSDoc with usage examples
## Testing:
**6 New Comprehensive Tests:**
1. ✅ Creates VarUnique instances
2. ✅ Unique names within single call
3. ✅ Unique names across multiple calls
4. ✅ Incrementing counter validation
5. ✅ Correct JSON generation
6. ✅ Original vars() unchanged
**All 156 tests passing**
## Usage Examples:
```javascript
// Basic usage
const [a, b, c] = WOQL.vars_unique('a', 'b', 'c')
// Creates: a_1, b_2, c_3
// Guaranteed uniqueness even with same names
const [x1] = WOQL.vars_unique('x') // x_4
const [x2] = WOQL.vars_unique('x') // x_5
// Scope isolation with select()
const [localVar] = WOQL.vars_unique('x')
WOQL.select(localVar, WOQL.triple(localVar, 'rdf:type', 'Person'))
// localVar is unique and won't conflict with other 'x' variables
```1 parent ed94e33 commit 61fed00
3 files changed
+154
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
43 | 49 | | |
44 | 50 | | |
45 | 51 | | |
| |||
80 | 86 | | |
81 | 87 | | |
82 | 88 | | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
83 | 113 | | |
84 | 114 | | |
85 | 115 | | |
| |||
105 | 135 | | |
106 | 136 | | |
107 | 137 | | |
108 | | - | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
8 | 10 | | |
9 | 11 | | |
10 | 12 | | |
| |||
1352 | 1354 | | |
1353 | 1355 | | |
1354 | 1356 | | |
| 1357 | + | |
| 1358 | + | |
| 1359 | + | |
| 1360 | + | |
| 1361 | + | |
| 1362 | + | |
| 1363 | + | |
| 1364 | + | |
| 1365 | + | |
| 1366 | + | |
| 1367 | + | |
| 1368 | + | |
| 1369 | + | |
| 1370 | + | |
| 1371 | + | |
| 1372 | + | |
| 1373 | + | |
| 1374 | + | |
| 1375 | + | |
| 1376 | + | |
| 1377 | + | |
| 1378 | + | |
| 1379 | + | |
| 1380 | + | |
| 1381 | + | |
| 1382 | + | |
| 1383 | + | |
1355 | 1384 | | |
1356 | 1385 | | |
1357 | 1386 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| |||
376 | 376 | | |
377 | 377 | | |
378 | 378 | | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
379 | 469 | | |
380 | 470 | | |
381 | 471 | | |
| |||
0 commit comments