Merge branch 'staging' into send-commands-return-value

This commit is contained in:
Cohee
2024-10-01 01:52:33 +03:00
12 changed files with 454 additions and 190 deletions

View File

@ -683,8 +683,8 @@ function deleteGlobalVariable(name) {
}
/**
* Parses a series of numeric values from a string.
* @param {string} value A space-separated list of numeric values or variable names
* Parses a series of numeric values from a string or a string array.
* @param {string|string[]} value A space-separated list of numeric values or variable names
* @param {SlashCommandScope} scope Scope
* @returns {number[]} An array of numeric values
*/
@ -693,11 +693,17 @@ function parseNumericSeries(value, scope = null) {
return [value];
}
const array = value
.split(' ')
.map(i => i.trim())
/** @type {(string|number)[]} */
let values = Array.isArray(value) ? value : value.split(' ');
// If a JSON array was provided as the only value, convert it to an array
if (values.length === 1 && typeof values[0] === 'string' && values[0].startsWith('[')) {
values = convertValueType(values[0], 'array');
}
const array = values.map(i => typeof i === 'string' ? i.trim() : i)
.filter(i => i !== '')
.map(i => isNaN(Number(i)) ? Number(resolveVariable(i, scope)) : Number(i))
.map(i => isNaN(Number(i)) ? Number(resolveVariable(String(i), scope)) : Number(i))
.filter(i => !isNaN(i));
return array;
@ -717,7 +723,7 @@ function performOperation(value, operation, singleOperand = false, scope = null)
const result = singleOperand ? operation(array[0]) : operation(array);
if (isNaN(result) || !isFinite(result)) {
if (isNaN(result)) {
return 0;
}
@ -745,7 +751,7 @@ function maxValuesCallback(args, value) {
}
function subValuesCallback(args, value) {
return performOperation(value, (array) => array[0] - array[1], false, args._scope);
return performOperation(value, (array) => array.reduce((a, b) => a - b, array.shift() ?? 0), false, args._scope);
}
function divValuesCallback(args, value) {
@ -1618,36 +1624,15 @@ export function registerVariableCommands() {
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'add',
callback: (args, /**@type {string[]}*/value) => addValuesCallback(args, value.join(' ')),
callback: (args, value) => addValuesCallback(args, value),
returns: 'sum of the provided values',
unnamedArgumentList: [
SlashCommandArgument.fromProps({
description: 'values to sum',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME, ARGUMENT_TYPE.LIST],
isRequired: true,
acceptsMultiple: true,
enumProvider: (executor, scope) => {
const vars = commonEnumProviders.variables('all')(executor, scope);
vars.push(
new SlashCommandEnumValue(
'any variable name',
null,
enumTypes.variable,
enumIcons.variable,
(input) => /^\w*$/.test(input),
(input) => input,
),
new SlashCommandEnumValue(
'any number',
null,
enumTypes.number,
enumIcons.number,
(input) => input == '' || !Number.isNaN(Number(input)),
(input) => input,
),
);
return vars;
},
enumProvider: commonEnumProviders.numbersAndVariables,
forceEnum: false,
}),
],
@ -1655,7 +1640,9 @@ export function registerVariableCommands() {
helpString: `
<div>
Performs an addition of the set of values and passes the result down the pipe.
Can use variable names.
</div>
<div>
Can use variable names, or a JSON array consisting of numbers and variables (with quotes).
</div>
<div>
<strong>Example:</strong>
@ -1663,6 +1650,9 @@ export function registerVariableCommands() {
<li>
<pre><code class="language-stscript">/add 10 i 30 j</code></pre>
</li>
<li>
<pre><code class="language-stscript">/add ["count", 15, 2, "i"]</code></pre>
</li>
</ul>
</div>
`,
@ -1674,16 +1664,20 @@ export function registerVariableCommands() {
unnamedArgumentList: [
SlashCommandArgument.fromProps({
description: 'values to multiply',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME, ARGUMENT_TYPE.LIST],
isRequired: true,
acceptsMultiple: true,
enumProvider: commonEnumProviders.variables('all'),
enumProvider: commonEnumProviders.numbersAndVariables,
forceEnum: false,
}),
],
splitUnnamedArgument: true,
helpString: `
<div>
Performs a multiplication of the set of values and passes the result down the pipe. Can use variable names.
Performs a multiplication of the set of values and passes the result down the pipe.
</div>
<div>
Can use variable names, or a JSON array consisting of numbers and variables (with quotes).
</div>
<div>
<strong>Examples:</strong>
@ -1691,6 +1685,9 @@ export function registerVariableCommands() {
<li>
<pre><code class="language-stscript">/mul 10 i 30 j</code></pre>
</li>
<li>
<pre><code class="language-stscript">/mul ["count", 15, 2, "i"]</code></pre>
</li>
</ul>
</div>
`,
@ -1702,16 +1699,20 @@ export function registerVariableCommands() {
unnamedArgumentList: [
SlashCommandArgument.fromProps({
description: 'values to find the max',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME, ARGUMENT_TYPE.LIST],
isRequired: true,
acceptsMultiple: true,
enumProvider: commonEnumProviders.variables('all'),
enumProvider: commonEnumProviders.numbersAndVariables,
forceEnum: false,
}),
],
splitUnnamedArgument: true,
helpString: `
<div>
Returns the maximum value of the set of values and passes the result down the pipe. Can use variable names.
Returns the maximum value of the set of values and passes the result down the pipe.
</div>
<div>
Can use variable names, or a JSON array consisting of numbers and variables (with quotes).
</div>
<div>
<strong>Examples:</strong>
@ -1719,6 +1720,9 @@ export function registerVariableCommands() {
<li>
<pre><code class="language-stscript">/max 10 i 30 j</code></pre>
</li>
<li>
<pre><code class="language-stscript">/max ["count", 15, 2, "i"]</code></pre>
</li>
</ul>
</div>
`,
@ -1730,17 +1734,20 @@ export function registerVariableCommands() {
unnamedArgumentList: [
SlashCommandArgument.fromProps({
description: 'values to find the min',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME, ARGUMENT_TYPE.LIST],
isRequired: true,
acceptsMultiple: true,
enumProvider: commonEnumProviders.variables('all'),
enumProvider: commonEnumProviders.numbersAndVariables,
forceEnum: false,
}),
],
splitUnnamedArgument: true,
helpString: `
<div>
Returns the minimum value of the set of values and passes the result down the pipe.
Can use variable names.
</div>
<div>
Can use variable names, or a JSON array consisting of numbers and variables (with quotes).
</div>
<div>
<strong>Example:</strong>
@ -1748,6 +1755,9 @@ export function registerVariableCommands() {
<li>
<pre><code class="language-stscript">/min 10 i 30 j</code></pre>
</li>
<li>
<pre><code class="language-stscript">/min ["count", 15, 2, "i"]</code></pre>
</li>
</ul>
</div>
`,
@ -1758,18 +1768,21 @@ export function registerVariableCommands() {
returns: 'difference of the provided values',
unnamedArgumentList: [
SlashCommandArgument.fromProps({
description: 'values to find the difference',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
description: 'values to subtract, starting form the first provided value',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME, ARGUMENT_TYPE.LIST],
isRequired: true,
acceptsMultiple: true,
enumProvider: commonEnumProviders.variables('all'),
enumProvider: commonEnumProviders.numbersAndVariables,
forceEnum: false,
}),
],
splitUnnamedArgument: true,
helpString: `
<div>
Performs a subtraction of the set of values and passes the result down the pipe.
Can use variable names.
</div>
<div>
Can use variable names, or a JSON array consisting of numbers and variables (with quotes).
</div>
<div>
<strong>Example:</strong>
@ -1777,6 +1790,9 @@ export function registerVariableCommands() {
<li>
<pre><code class="language-stscript">/sub i 5</code></pre>
</li>
<li>
<pre><code class="language-stscript">/sub ["count", 4, "i"]</code></pre>
</li>
</ul>
</div>
`,
@ -1790,17 +1806,18 @@ export function registerVariableCommands() {
description: 'dividend',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
enumProvider: commonEnumProviders.variables('all'),
enumProvider: commonEnumProviders.numbersAndVariables,
forceEnum: false,
}),
SlashCommandArgument.fromProps({
description: 'divisor',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
enumProvider: commonEnumProviders.variables('all'),
enumProvider: commonEnumProviders.numbersAndVariables,
forceEnum: false,
}),
],
splitUnnamedArgument: true,
helpString: `
<div>
Performs a division of two values and passes the result down the pipe.
@ -1825,17 +1842,18 @@ export function registerVariableCommands() {
description: 'dividend',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
enumProvider: commonEnumProviders.variables('all'),
enumProvider: commonEnumProviders.numbersAndVariables,
forceEnum: false,
}),
SlashCommandArgument.fromProps({
description: 'divisor',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
enumProvider: commonEnumProviders.variables('all'),
enumProvider: commonEnumProviders.numbersAndVariables,
forceEnum: false,
}),
],
splitUnnamedArgument: true,
helpString: `
<div>
Performs a modulo operation of two values and passes the result down the pipe.
@ -1860,17 +1878,18 @@ export function registerVariableCommands() {
description: 'base',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
enumProvider: commonEnumProviders.variables('all'),
enumProvider: commonEnumProviders.numbersAndVariables,
forceEnum: false,
}),
SlashCommandArgument.fromProps({
description: 'exponent',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
enumProvider: commonEnumProviders.variables('all'),
enumProvider: commonEnumProviders.numbersAndVariables,
forceEnum: false,
}),
],
splitUnnamedArgument: true,
helpString: `
<div>
Performs a power operation of two values and passes the result down the pipe.
@ -1895,7 +1914,7 @@ export function registerVariableCommands() {
description: 'value',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
enumProvider: commonEnumProviders.variables('all'),
enumProvider: commonEnumProviders.numbersAndVariables,
forceEnum: false,
}),
],
@ -1923,7 +1942,7 @@ export function registerVariableCommands() {
description: 'value',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
enumProvider: commonEnumProviders.variables('all'),
enumProvider: commonEnumProviders.numbersAndVariables,
forceEnum: false,
}),
],
@ -1952,7 +1971,7 @@ export function registerVariableCommands() {
description: 'value',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
enumProvider: commonEnumProviders.variables('all'),
enumProvider: commonEnumProviders.numbersAndVariables,
forceEnum: false,
}),
],
@ -1980,7 +1999,7 @@ export function registerVariableCommands() {
description: 'value',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
enumProvider: commonEnumProviders.variables('all'),
enumProvider: commonEnumProviders.numbersAndVariables,
forceEnum: false,
}),
],
@ -2008,7 +2027,7 @@ export function registerVariableCommands() {
description: 'value',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
enumProvider: commonEnumProviders.variables('all'),
enumProvider: commonEnumProviders.numbersAndVariables,
forceEnum: false,
}),
],
@ -2036,7 +2055,7 @@ export function registerVariableCommands() {
description: 'value',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
enumProvider: commonEnumProviders.variables('all'),
enumProvider: commonEnumProviders.numbersAndVariables,
forceEnum: false,
}),
],