Merge pull request #2301 from LenAnderson/fix-var-behavior

Fix var behavior
This commit is contained in:
Cohee 2024-05-22 20:01:30 +03:00 committed by GitHub
commit d33ca68620
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 14 additions and 15 deletions

View File

@ -795,27 +795,26 @@ function letCallback(args, value) {
/** /**
* Set or retrieve a variable in the current scope or nearest ancestor scope. * Set or retrieve a variable in the current scope or nearest ancestor scope.
* @param {{_scope:SlashCommandScope, key?:string, index?:String|Number}} args Named arguments. * @param {{_scope:SlashCommandScope, key?:string, index?:string|number}} args Named arguments.
* @param {String|[String, SlashCommandClosure]} value Name and optional value for the variable. * @param {string|SlashCommandClosure|(string|SlashCommandClosure)[]} value Name and optional value for the variable.
* @returns The variable's value * @returns The variable's value
*/ */
function varCallback(args, value) { function varCallback(args, value) {
if (Array.isArray(value)) { if (!Array.isArray(value)) value = [value];
args._scope.setVariable(value[0], typeof value[1] == 'string' ? value.slice(1).join(' ') : value[1], args.index);
return value[1];
}
if (args.key !== undefined) { if (args.key !== undefined) {
const key = args.key; const key = args.key;
const val = value; const val = value.join(' ');
args._scope.setVariable(key, val, args.index);
return val;
} else if (value.includes(' ')) {
const key = value.split(' ')[0];
const val = value.split(' ').slice(1).join(' ');
args._scope.setVariable(key, val, args.index); args._scope.setVariable(key, val, args.index);
return val; return val;
} }
return args._scope.getVariable(args.key ?? value, args.index); const key = value.shift();
if (value.length > 0) {
const val = value.join(' ');
args._scope.setVariable(key, val, args.index);
return val;
} else {
return args._scope.getVariable(key, args.index);
}
} }
export function registerVariableCommands() { export function registerVariableCommands() {
@ -1733,7 +1732,7 @@ export function registerVariableCommands() {
returns: 'the variable value', returns: 'the variable value',
namedArgumentList: [ namedArgumentList: [
new SlashCommandNamedArgument( new SlashCommandNamedArgument(
'key', 'variable name', [ARGUMENT_TYPE.VARIABLE_NAME], false, 'key', 'variable name; forces setting the variable, even if no value is provided', [ARGUMENT_TYPE.VARIABLE_NAME], false,
), ),
new SlashCommandNamedArgument( new SlashCommandNamedArgument(
'index', 'index',
@ -1769,7 +1768,7 @@ export function registerVariableCommands() {
<pre><code class="language-stscript">/let x foo | /var x foo bar | /var x | /echo</code></pre> <pre><code class="language-stscript">/let x foo | /var x foo bar | /var x | /echo</code></pre>
</li> </li>
<li> <li>
<pre><code class="language-stscript">/let x foo | /var key=x foo bar | /var key=x | /echo</code></pre> <pre><code class="language-stscript">/let x foo | /var key=x foo bar | /var x | /echo</code></pre>
</li> </li>
</ul> </ul>
</div> </div>