Add type conversion for /setvar commands with index
| @@ -36,6 +36,8 @@ export const enumIcons = { | ||
| 36 | 36 | |
| 37 | 37 | true: '✔️', |
| 38 | 38 | false: '❌', |
| 39 | + null: '🚫', | |
| 40 | + undefined: '❓', | |
| 39 | 41 | |
| 40 | 42 | // Value types |
| 41 | 43 | boolean: '🔲', |
| @@ -230,4 +232,19 @@ export const commonEnumProviders = { | ||
| 230 | 232 | enumTypes.enum, '💉'); |
| 231 | 233 | }); |
| 232 | 234 | }, |
| 235 | + | |
| 236 | + /** | |
| 237 | + * Gets somewhat recognizable STscript types. | |
| 238 | + * | |
| 239 | + * @returns {SlashCommandEnumValue[]} | |
| 240 | + */ | |
| 241 | + types: () => [ | |
| 242 | + new SlashCommandEnumValue('string', null, enumTypes.type, enumIcons.string), | |
| 243 | + new SlashCommandEnumValue('number', null, enumTypes.type, enumIcons.number), | |
| 244 | + new SlashCommandEnumValue('boolean', null, enumTypes.type, enumIcons.boolean), | |
| 245 | + new SlashCommandEnumValue('array', null, enumTypes.type, enumIcons.array), | |
| 246 | + new SlashCommandEnumValue('object', null, enumTypes.type, enumIcons.dictionary), | |
| 247 | + new SlashCommandEnumValue('null', null, enumTypes.type, enumIcons.null), | |
| 248 | + new SlashCommandEnumValue('undefined', null, enumTypes.type, enumIcons.undefined), | |
| 249 | + ], | |
| 233 | 250 | }; |
| @@ -1,4 +1,5 @@ | ||
| 1 | 1 | import { SlashCommandClosure } from './SlashCommandClosure.js'; |
| 2 | +import { convertValueType } from '../utils.js'; | |
| 2 | 3 | |
| 3 | 4 | export class SlashCommandScope { |
| 4 | 5 | /**@type {string[]}*/ variableNames = []; |
| @@ -55,7 +56,8 @@ export class SlashCommandScope { | ||
| 55 | 56 | if (this.existsVariableInScope(key)) throw new SlashCommandScopeVariableExistsError(`Variable named "${key}" already exists.`); |
| 56 | 57 | this.variables[key] = value; |
| 57 | 58 | } |
| 58 | 59 | setVariable(key, value, index = null, type = null) { |
| 60 | + value = convertValueType(value, type); | |
| 59 | 61 | if (this.existsVariableInScope(key)) { |
| 60 | 62 | if (index !== null && index !== undefined) { |
| 61 | 63 | let v = this.variables[key]; |
| @@ -4,6 +4,7 @@ import { isMobile } from './RossAscends-mods.js'; | ||
| 4 | 4 | import { collapseNewlines } from './power-user.js'; |
| 5 | 5 | import { debounce_timeout } from './constants.js'; |
| 6 | 6 | import { Popup, POPUP_RESULT, POPUP_TYPE } from './popup.js'; |
| 7 | +import { SlashCommandClosure } from './slash-commands/SlashCommandClosure.js'; | |
| 7 | 8 | |
| 8 | 9 | /** |
| 9 | 10 | * Pagination status string template. |
| @@ -34,6 +35,74 @@ export function isValidUrl(value) { | ||
| 34 | 35 | } |
| 35 | 36 | |
| 36 | 37 | /** |
| 38 | + * Converts string to a value of a given type. Includes pythonista-friendly aliases. | |
| 39 | + * @param {string|SlashCommandClosure} value String value | |
| 40 | + * @param {string} type Type to convert to | |
| 41 | + * @returns {any} Converted value | |
| 42 | + */ | |
| 43 | +export function convertValueType(value, type) { | |
| 44 | + if (value instanceof SlashCommandClosure || typeof type !== 'string') { | |
| 45 | + return value; | |
| 46 | + } | |
| 47 | + | |
| 48 | + type = type.trim().toLowerCase(); | |
| 49 | + | |
| 50 | + switch (type) { | |
| 51 | + case 'string': | |
| 52 | + case 'str': | |
| 53 | + return String(value); | |
| 54 | + | |
| 55 | + case 'null': | |
| 56 | + return null; | |
| 57 | + | |
| 58 | + case 'undefined': | |
| 59 | + case 'none': | |
| 60 | + return undefined; | |
| 61 | + | |
| 62 | + case 'number': | |
| 63 | + return Number(value); | |
| 64 | + | |
| 65 | + case 'int': | |
| 66 | + return parseInt(value, 10); | |
| 67 | + | |
| 68 | + case 'float': | |
| 69 | + return parseFloat(value); | |
| 70 | + | |
| 71 | + case 'boolean': | |
| 72 | + case 'bool': | |
| 73 | + return isTrueBoolean(value); | |
| 74 | + | |
| 75 | + case 'list': | |
| 76 | + case 'array': | |
| 77 | + try { | |
| 78 | + const parsedArray = JSON.parse(value); | |
| 79 | + if (Array.isArray(parsedArray)) { | |
| 80 | + return parsedArray; | |
| 81 | + } | |
| 82 | + throw new Error('Value is not an array.'); | |
| 83 | + } catch { | |
| 84 | + return []; | |
| 85 | + } | |
| 86 | + | |
| 87 | + case 'object': | |
| 88 | + case 'dict': | |
| 89 | + case 'dictionary': | |
| 90 | + try { | |
| 91 | + const parsedObject = JSON.parse(value); | |
| 92 | + if (typeof parsedObject === 'object') { | |
| 93 | + return parsedObject; | |
| 94 | + } | |
| 95 | + throw new Error('Value is not an object.'); | |
| 96 | + } catch { | |
| 97 | + return {}; | |
| 98 | + } | |
| 99 | + | |
| 100 | + default: | |
| 101 | + return value; | |
| 102 | + } | |
| 103 | +} | |
| 104 | + | |
| 105 | +/** | |
| 37 | 106 | * Parses ranges like 10-20 or 10. |
| 38 | 107 | * Range is inclusive. Start must be less than end. |
| 39 | 108 | * Returns null if invalid. |
| @@ -11,7 +11,7 @@ import { commonEnumProviders, enumIcons } from './slash-commands/SlashCommandCom | ||
| 11 | 11 | import { SlashCommandEnumValue, enumTypes } from './slash-commands/SlashCommandEnumValue.js'; |
| 12 | 12 | import { PARSER_FLAG, SlashCommandParser } from './slash-commands/SlashCommandParser.js'; |
| 13 | 13 | import { SlashCommandScope } from './slash-commands/SlashCommandScope.js'; |
| 14 | 14 | import { isFalseBoolean, convertValueType } from './utils.js'; |
| 15 | 15 | |
| 16 | 16 | /** @typedef {import('./slash-commands/SlashCommandParser.js').NamedArguments} NamedArguments */ |
| 17 | 17 | /** @typedef {import('./slash-commands/SlashCommand.js').UnnamedArguments} UnnamedArguments */ |
| @@ -51,6 +51,7 @@ function setLocalVariable(name, value, args = {}) { | ||
| 51 | 51 | |
| 52 | 52 | if (args.index !== undefined) { |
| 53 | 53 | try { |
| 54 | + value = convertValueType(value, args.type); | |
| 54 | 55 | let localVariable = JSON.parse(chat_metadata.variables[name] ?? 'null'); |
| 55 | 56 | const numIndex = Number(args.index); |
| 56 | 57 | if (Number.isNaN(numIndex)) { |
| @@ -100,6 +101,7 @@ function getGlobalVariable(name, args = {}) { | ||
| 100 | 101 | function setGlobalVariable(name, value, args = {}) { |
| 101 | 102 | if (args.index !== undefined) { |
| 102 | 103 | try { |
| 104 | + value = convertValueType(value, args.type); | |
| 103 | 105 | let globalVariable = JSON.parse(extension_settings.variables.global[name] ?? 'null'); |
| 104 | 106 | const numIndex = Number(args.index); |
| 105 | 107 | if (Number.isNaN(numIndex)) { |
| @@ -667,23 +669,28 @@ function parseNumericSeries(value, scope = null) { | ||
| 667 | 669 | } |
| 668 | 670 | |
| 669 | 671 | function performOperation(value, operation, singleOperand = false, scope = null) { |
| 670 | 672 | iffunction getResult(!value) { |
| 671 | - return 0; | |
| 673 | + if (!value) { | |
| 672 | - } | |
| 674 | + return 0; | |
| 675 | + } | |
| 673 | 676 | |
| 674 | 677 | const array = parseNumericSeries(value, scope); |
| 675 | 678 | |
| 676 | 679 | if (array.length === 0) { |
| 677 | 680 | return 0; |
| 678 | 681 | } |
| 679 | 682 | |
| 680 | 683 | const result = singleOperand ? operation(array[0]) : operation(array); |
| 681 | 684 | |
| 682 | 685 | if (isNaN(result) || !isFinite(result)) { |
| 683 | 686 | return 0; |
| 687 | + } | |
| 688 | + | |
| 689 | + return result; | |
| 684 | 690 | } |
| 685 | 691 | |
| 686 | 692 | returnconst result = getResult(); |
| 693 | + return String(result); | |
| 687 | 694 | } |
| 688 | 695 | |
| 689 | 696 | function addValuesCallback(args, value) { |
| @@ -836,7 +843,7 @@ function varCallback(args, value) { | ||
| 836 | 843 | if (typeof key != 'string') throw new Error('Key must be a string'); |
| 837 | 844 | if (args._hasUnnamedArgument) { |
| 838 | 845 | const val = typeof value[0] == 'string' ? value.join(' ') : value[0]; |
| 839 | 846 | args._scope.setVariable(key, val, args.index, args.type); |
| 840 | 847 | return val; |
| 841 | 848 | } else { |
| 842 | 849 | return args._scope.getVariable(key, args.index); |
| @@ -846,7 +853,7 @@ function varCallback(args, value) { | ||
| 846 | 853 | if (typeof key != 'string') throw new Error('Key must be a string'); |
| 847 | 854 | if (value.length > 0) { |
| 848 | 855 | const val = typeof value[0] == 'string' ? value.join(' ') : value[0]; |
| 849 | 856 | args._scope.setVariable(key, val, args.index, args.type); |
| 850 | 857 | return val; |
| 851 | 858 | } else { |
| 852 | 859 | return args._scope.getVariable(key, args.index); |
| @@ -901,6 +908,14 @@ export function registerVariableCommands() { | ||
| 901 | 908 | new SlashCommandNamedArgument( |
| 902 | 909 | 'index', 'list index', [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.STRING], false, |
| 903 | 910 | ), |
| 911 | + SlashCommandNamedArgument.fromProps({ | |
| 912 | + name: 'type', | |
| 913 | + description: 'type of the value when used with index', | |
| 914 | + forceEnum: true, | |
| 915 | + enumProvider: commonEnumProviders.types, | |
| 916 | + isRequired: false, | |
| 917 | + defaultValue: 'string', | |
| 918 | + }), | |
| 904 | 919 | ], |
| 905 | 920 | unnamedArgumentList: [ |
| 906 | 921 | new SlashCommandArgument( |
| @@ -910,6 +925,7 @@ export function registerVariableCommands() { | ||
| 910 | 925 | helpString: ` |
| 911 | 926 | <div> |
| 912 | 927 | Set a local variable value and pass it down the pipe. The <code>index</code> argument is optional. |
| 928 | + To perform a type conversion when using <code>index</code>, use the <code>type</code> argument. | |
| 913 | 929 | </div> |
| 914 | 930 | <div> |
| 915 | 931 | <strong>Example:</strong> |
| @@ -917,6 +933,9 @@ export function registerVariableCommands() { | ||
| 917 | 933 | <li> |
| 918 | 934 | <pre><code class="language-stscript">/setvar key=color green</code></pre> |
| 919 | 935 | </li> |
| 936 | + <li> | |
| 937 | + <pre><code class="language-stscript">/setvar key=colors index=3 type=string blue</code></pre> | |
| 938 | + </li> | |
| 920 | 939 | </ul> |
| 921 | 940 | </div> |
| 922 | 941 | `, |
| @@ -1015,6 +1034,14 @@ export function registerVariableCommands() { | ||
| 1015 | 1034 | new SlashCommandNamedArgument( |
| 1016 | 1035 | 'index', 'list index', [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.STRING], false, |
| 1017 | 1036 | ), |
| 1037 | + SlashCommandNamedArgument.fromProps({ | |
| 1038 | + name: 'type', | |
| 1039 | + description: 'type of the value when used with index', | |
| 1040 | + forceEnum: true, | |
| 1041 | + enumProvider: commonEnumProviders.types, | |
| 1042 | + isRequired: false, | |
| 1043 | + defaultValue: 'string', | |
| 1044 | + }), | |
| 1018 | 1045 | ], |
| 1019 | 1046 | unnamedArgumentList: [ |
| 1020 | 1047 | new SlashCommandArgument( |
| @@ -1024,6 +1051,7 @@ export function registerVariableCommands() { | ||
| 1024 | 1051 | helpString: ` |
| 1025 | 1052 | <div> |
| 1026 | 1053 | Set a global variable value and pass it down the pipe. The <code>index</code> argument is optional. |
| 1054 | + To perform a type conversion when using <code>index</code>, use the <code>type</code> argument. | |
| 1027 | 1055 | </div> |
| 1028 | 1056 | <div> |
| 1029 | 1057 | <strong>Example:</strong> |
| @@ -1031,6 +1059,9 @@ export function registerVariableCommands() { | ||
| 1031 | 1059 | <li> |
| 1032 | 1060 | <pre><code class="language-stscript">/setglobalvar key=color green</code></pre> |
| 1033 | 1061 | </li> |
| 1062 | + <li> | |
| 1063 | + <pre><code class="language-stscript">/setglobalvar key=colors index=3 type=string blue</code></pre> | |
| 1064 | + </li> | |
| 1034 | 1065 | </ul> |
| 1035 | 1066 | </div> |
| 1036 | 1067 | `, |
| @@ -1247,16 +1278,16 @@ export function registerVariableCommands() { | ||
| 1247 | 1278 | }), |
| 1248 | 1279 | new SlashCommandNamedArgument( |
| 1249 | 1280 | 'rule', 'comparison rule', [ARGUMENT_TYPE.STRING], true, false, null, [ |
| 1250 | 1281 | new SlashCommandEnumValue('gt', 'a > b'), |
| 1251 | 1282 | new SlashCommandEnumValue('gte', 'a >= b'), |
| 1252 | 1283 | new SlashCommandEnumValue('lt', 'a < b'), |
| 1253 | 1284 | new SlashCommandEnumValue('lte', 'a <= b'), |
| 1254 | 1285 | new SlashCommandEnumValue('eq', 'a == b'), |
| 1255 | 1286 | new SlashCommandEnumValue('neq', 'a !== b'), |
| 1256 | 1287 | new SlashCommandEnumValue('not', '!a'), |
| 1257 | 1288 | new SlashCommandEnumValue('in', 'a includes b'), |
| 1258 | 1289 | new SlashCommandEnumValue('nin', 'a not includes b'), |
| 1259 | 1290 | ], |
| 1260 | 1291 | ), |
| 1261 | 1292 | new SlashCommandNamedArgument( |
| 1262 | 1293 | 'else', 'command to execute if not true', [ARGUMENT_TYPE.CLOSURE, ARGUMENT_TYPE.SUBCOMMAND], false, |
| @@ -1325,16 +1356,16 @@ export function registerVariableCommands() { | ||
| 1325 | 1356 | }), |
| 1326 | 1357 | new SlashCommandNamedArgument( |
| 1327 | 1358 | 'rule', 'comparison rule', [ARGUMENT_TYPE.STRING], true, false, null, [ |
| 1328 | 1359 | new SlashCommandEnumValue('gt', 'a > b'), |
| 1329 | 1360 | new SlashCommandEnumValue('gte', 'a >= b'), |
| 1330 | 1361 | new SlashCommandEnumValue('lt', 'a < b'), |
| 1331 | 1362 | new SlashCommandEnumValue('lte', 'a <= b'), |
| 1332 | 1363 | new SlashCommandEnumValue('eq', 'a == b'), |
| 1333 | 1364 | new SlashCommandEnumValue('neq', 'a !== b'), |
| 1334 | 1365 | new SlashCommandEnumValue('not', '!a'), |
| 1335 | 1366 | new SlashCommandEnumValue('in', 'a includes b'), |
| 1336 | 1367 | new SlashCommandEnumValue('nin', 'a not includes b'), |
| 1337 | 1368 | ], |
| 1338 | 1369 | ), |
| 1339 | 1370 | new SlashCommandNamedArgument( |
| 1340 | 1371 | 'guard', 'disable loop iteration limit', [ARGUMENT_TYPE.STRING], false, false, null, commonEnumProviders.boolean('onOff')(), |
| @@ -2030,6 +2061,14 @@ export function registerVariableCommands() { | ||
| 2030 | 2061 | false, // isRequired |
| 2031 | 2062 | false, // acceptsMultiple |
| 2032 | 2063 | ), |
| 2064 | + SlashCommandNamedArgument.fromProps({ | |
| 2065 | + name: 'type', | |
| 2066 | + description: 'type of the value when used with index', | |
| 2067 | + forceEnum: true, | |
| 2068 | + enumProvider: commonEnumProviders.types, | |
| 2069 | + isRequired: false, | |
| 2070 | + defaultValue: 'string', | |
| 2071 | + }), | |
| 2033 | 2072 | ], |
| 2034 | 2073 | unnamedArgumentList: [ |
| 2035 | 2074 | SlashCommandArgument.fromProps({ |