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