Let in/nin fall through to string + docs update
| @@ -529,7 +529,7 @@ export function evalBoolean(rule, a, b) { | ||
| 529 | 529 | if (isFalseBoolean(String(a))) return !resultOnTruthy; |
| 530 | 530 | return !!a ? resultOnTruthy : !resultOnTruthy; |
| 531 | 531 | default: |
| 532 | 532 | throw new Error(`Unknown boolean comparison rule for truthy check. If right-hand sideoperand is not provided, the rule must not provided or be "'not"'. Provided: ${rule}`); |
| 533 | 533 | } |
| 534 | 534 | } |
| 535 | 535 | |
| @@ -554,6 +554,11 @@ export function evalBoolean(rule, a, b) { | ||
| 554 | 554 | return aNumber === bNumber; |
| 555 | 555 | case 'neq': |
| 556 | 556 | return aNumber !== bNumber; |
| 557 | + case 'in': | |
| 558 | + case 'nin': | |
| 559 | + // Fall through to string comparison. Otherwise you could not check if 12345 contains 45 for example. | |
| 560 | + console.debug(`Boolean comparison rule '${rule}' is not supported for type number. Falling back to string comparison.`); | |
| 561 | + break; | |
| 557 | 562 | default: |
| 558 | 563 | throw new Error(`Unknown boolean comparison rule for type number. Accepted: gt, gte, lt, lte, eq, neq. Provided: ${rule}`); |
| 559 | 564 | } |
| @@ -1265,15 +1270,15 @@ export function registerVariableCommands() { | ||
| 1265 | 1270 | typeList: [ARGUMENT_TYPE.STRING], |
| 1266 | 1271 | defaultValue: 'eq', |
| 1267 | 1272 | enumList: [ |
| 1268 | 1273 | new SlashCommandEnumValue('gteq', 'a >== b (strings & numbers)'), |
| 1269 | 1274 | new SlashCommandEnumValue('gteneq', 'a >!== b (strings & numbers)'), |
| 1270 | 1275 | new SlashCommandEnumValue('ltin', 'a <includes b (strings & numbers as strings)'), |
| 1271 | 1276 | new SlashCommandEnumValue('ltenin', 'a <=not includes b (strings & numbers as strings)'), |
| 1272 | 1277 | new SlashCommandEnumValue('eqgt', 'a ==> b (numbers)'), |
| 1273 | 1278 | new SlashCommandEnumValue('neqgte', 'a !=>= b (numbers)'), |
| 1274 | 1279 | new SlashCommandEnumValue('notlt', '!a < b (numbers)'), |
| 1275 | 1280 | new SlashCommandEnumValue('inlte', 'a includes<= b (numbers)'), |
| 1276 | 1281 | new SlashCommandEnumValue('ninnot', '!a not includes b(truthy)'), |
| 1277 | 1282 | ], |
| 1278 | 1283 | forceEnum: true, |
| 1279 | 1284 | }), |
| @@ -1299,22 +1304,25 @@ export function registerVariableCommands() { | ||
| 1299 | 1304 | Numeric values and string literals for left and right operands supported. |
| 1300 | 1305 | </div> |
| 1301 | 1306 | <div> |
| 1302 | 1307 | If the rule is not provided, it defaults to <code>eq</code>.<br /> |
| 1308 | + </div> | |
| 1309 | + <div> | |
| 1303 | 1310 | If no right operand is provided, it defaults to checking the <code>left</code> value to be truthy. |
| 1304 | 1311 | A non-empty string or non-zero number is considered truthy, as is the value <code>true</code> or <code>on</code>.<br /> |
| 1312 | + Only acceptable rules for no provided right operand are <code>not</code>, and no provided rule - which default to returning whether it is not or is truthy. | |
| 1305 | 1313 | </div> |
| 1306 | 1314 | <div> |
| 1307 | 1315 | <strong>Available rules:</strong> |
| 1308 | 1316 | <ul> |
| 1309 | 1317 | <li>gt<code>eq</code> => a >== b <small>(strings & numbers)</small></li> |
| 1310 | 1318 | <li>gte<code>neq</code> => a >!== b <small>(strings & numbers)</small></li> |
| 1311 | 1319 | <li>lt<code>in</code> => a <includes b <small>(strings & numbers as strings)</small></li> |
| 1312 | - <li>lte => a <= b</li> | |
| 1320 | + <li><code>nin</code> => a not includes b <small>(strings & numbers as strings)</small></li> | |
| 1313 | 1321 | <li>eq<code>gt</code> => a ==> b <small>(numbers)</small></li> |
| 1314 | 1322 | <li>neq<code>gte</code> => a !>= b <small>(numbers)</small></li> |
| 1315 | 1323 | <li>not<code>lt</code> => !a < b <small>(numbers)</small></li> |
| 1316 | 1324 | <li>in (strings)<code>lte</code> => a includes<= b <small>(numbers)</small></li> |
| 1317 | - <li>nin (strings) => a not includes b</li> | |
| 1325 | + <li><code>not</code> => !a <small>(truthy)</small></li> | |
| 1318 | 1326 | </ul> |
| 1319 | 1327 | </div> |
| 1320 | 1328 | <div> |
| @@ -1328,9 +1336,13 @@ export function registerVariableCommands() { | ||
| 1328 | 1336 | <pre><code class="language-stscript">/if left={{lastMessage}} rule=in right=surprise {: /echo SURPISE! :}</code></pre> |
| 1329 | 1337 | executes a subcommand defined as a closure if the given value contains a specified word. |
| 1330 | 1338 | <li> |
| 1331 | 1339 | <pre><code class="language-stscript">/if left=myContent {: /echo "My content had some content." :}</code></pre> |
| 1332 | 1340 | executes the defined subcommand, if the provided value of left is truthy (contains some kind of contant that is not empty or false) |
| 1333 | 1341 | </li> |
| 1342 | + <li> | |
| 1343 | + <pre><code class="language-stscript">/if left=tree right={{getvar::object}} {: /echo The object is a tree! :}</code></pre> | |
| 1344 | + executes the defined subcommand, if the left and right values are equals. | |
| 1345 | + </li> | |
| 1334 | 1346 | </ul> |
| 1335 | 1347 | </div> |
| 1336 | 1348 | `, |
| @@ -1359,15 +1371,15 @@ export function registerVariableCommands() { | ||
| 1359 | 1371 | typeList: [ARGUMENT_TYPE.STRING], |
| 1360 | 1372 | defaultValue: 'eq', |
| 1361 | 1373 | enumList: [ |
| 1362 | 1374 | new SlashCommandEnumValue('gteq', 'a >== b (strings & numbers)'), |
| 1363 | 1375 | new SlashCommandEnumValue('gteneq', 'a >!== b (strings & numbers)'), |
| 1364 | 1376 | new SlashCommandEnumValue('ltin', 'a <includes b (strings & numbers as strings)'), |
| 1365 | 1377 | new SlashCommandEnumValue('ltenin', 'a <=not includes b (strings & numbers as strings)'), |
| 1366 | 1378 | new SlashCommandEnumValue('eqgt', 'a ==> b (numbers)'), |
| 1367 | 1379 | new SlashCommandEnumValue('neqgte', 'a !=>= b (numbers)'), |
| 1368 | 1380 | new SlashCommandEnumValue('notlt', '!a < b (numbers)'), |
| 1369 | 1381 | new SlashCommandEnumValue('inlte', 'a includes<= b (numbers)'), |
| 1370 | 1382 | new SlashCommandEnumValue('ninnot', '!a not includes b(truthy)'), |
| 1371 | 1383 | ], |
| 1372 | 1384 | forceEnum: true, |
| 1373 | 1385 | }), |
| @@ -1396,15 +1408,15 @@ export function registerVariableCommands() { | ||
| 1396 | 1408 | <div> |
| 1397 | 1409 | <strong>Available rules:</strong> |
| 1398 | 1410 | <ul> |
| 1399 | 1411 | <li>gt<code>eq</code> => a >== b <small>(strings & numbers)</small></li> |
| 1400 | 1412 | <li>gte<code>neq</code> => a >!== b <small>(strings & numbers)</small></li> |
| 1401 | 1413 | <li>lt<code>in</code> => a <includes b <small>(strings & numbers as strings)</small></li> |
| 1402 | - <li>lte => a <= b</li> | |
| 1414 | + <li><code>nin</code> => a not includes b <small>(strings & numbers as strings)</small></li> | |
| 1403 | 1415 | <li>eq<code>gt</code> => a ==> b <small>(numbers)</small></li> |
| 1404 | 1416 | <li>neq<code>gte</code> => a !>= b <small>(numbers)</small></li> |
| 1405 | 1417 | <li>not<code>lt</code> => !a < b <small>(numbers)</small></li> |
| 1406 | 1418 | <li>in (strings)<code>lte</code> => a includes<= b <small>(numbers)</small></li> |
| 1407 | - <li>nin (strings) => a not includes b</li> | |
| 1419 | + <li><code>not</code> => !a <small>(truthy)</small></li> | |
| 1408 | 1420 | </ul> |
| 1409 | 1421 | </div> |
| 1410 | 1422 | <div> |