mirror of
				https://github.com/SillyTavern/SillyTavern.git
				synced 2025-06-05 21:59:27 +02:00 
			
		
		
		
	Allow using JSON arrays for math commands
- applies to all that receive a list. /add, /sub, /min, /max etc - Parsing is the same as the other commands where we already allow "LIST" as an argument.
This commit is contained in:
		| @@ -679,10 +679,17 @@ function parseNumericSeries(value, scope = null) { | |||||||
|         return [value]; |         return [value]; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     const values = Array.isArray(value) ? value : value.split(' '); |     /** @type {(string|number)[]} */ | ||||||
|     const array = values.map(i => i.trim()) |     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 !== '') |         .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)); |         .filter(i => !isNaN(i)); | ||||||
|  |  | ||||||
|     return array; |     return array; | ||||||
| @@ -1599,7 +1606,7 @@ export function registerVariableCommands() { | |||||||
|         unnamedArgumentList: [ |         unnamedArgumentList: [ | ||||||
|             SlashCommandArgument.fromProps({ |             SlashCommandArgument.fromProps({ | ||||||
|                 description: 'values to sum', |                 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, |                 isRequired: true, | ||||||
|                 acceptsMultiple: true, |                 acceptsMultiple: true, | ||||||
|                 enumProvider: commonEnumProviders.numbersAndVariables, |                 enumProvider: commonEnumProviders.numbersAndVariables, | ||||||
| @@ -1610,7 +1617,9 @@ export function registerVariableCommands() { | |||||||
|         helpString: ` |         helpString: ` | ||||||
|             <div> |             <div> | ||||||
|                 Performs an addition of the set of values and passes the result down the pipe. |                 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> | ||||||
|             <div> |             <div> | ||||||
|                 <strong>Example:</strong> |                 <strong>Example:</strong> | ||||||
| @@ -1618,6 +1627,9 @@ export function registerVariableCommands() { | |||||||
|                     <li> |                     <li> | ||||||
|                         <pre><code class="language-stscript">/add 10 i 30 j</code></pre> |                         <pre><code class="language-stscript">/add 10 i 30 j</code></pre> | ||||||
|                     </li> |                     </li> | ||||||
|  |                     <li> | ||||||
|  |                         <pre><code class="language-stscript">/add ["count", 15, 2, "i"]</code></pre> | ||||||
|  |                     </li> | ||||||
|                 </ul> |                 </ul> | ||||||
|             </div> |             </div> | ||||||
|         `, |         `, | ||||||
| @@ -1629,7 +1641,7 @@ export function registerVariableCommands() { | |||||||
|         unnamedArgumentList: [ |         unnamedArgumentList: [ | ||||||
|             SlashCommandArgument.fromProps({ |             SlashCommandArgument.fromProps({ | ||||||
|                 description: 'values to multiply', |                 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, |                 isRequired: true, | ||||||
|                 acceptsMultiple: true, |                 acceptsMultiple: true, | ||||||
|                 enumProvider: commonEnumProviders.numbersAndVariables, |                 enumProvider: commonEnumProviders.numbersAndVariables, | ||||||
| @@ -1639,7 +1651,10 @@ export function registerVariableCommands() { | |||||||
|         splitUnnamedArgument: true, |         splitUnnamedArgument: true, | ||||||
|         helpString: ` |         helpString: ` | ||||||
|             <div> |             <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> | ||||||
|             <div> |             <div> | ||||||
|                 <strong>Examples:</strong> |                 <strong>Examples:</strong> | ||||||
| @@ -1647,6 +1662,9 @@ export function registerVariableCommands() { | |||||||
|                     <li> |                     <li> | ||||||
|                         <pre><code class="language-stscript">/mul 10 i 30 j</code></pre> |                         <pre><code class="language-stscript">/mul 10 i 30 j</code></pre> | ||||||
|                     </li> |                     </li> | ||||||
|  |                     <li> | ||||||
|  |                         <pre><code class="language-stscript">/mul ["count", 15, 2, "i"]</code></pre> | ||||||
|  |                     </li> | ||||||
|                 </ul> |                 </ul> | ||||||
|             </div> |             </div> | ||||||
|         `, |         `, | ||||||
| @@ -1658,7 +1676,7 @@ export function registerVariableCommands() { | |||||||
|         unnamedArgumentList: [ |         unnamedArgumentList: [ | ||||||
|             SlashCommandArgument.fromProps({ |             SlashCommandArgument.fromProps({ | ||||||
|                 description: 'values to find the max', |                 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, |                 isRequired: true, | ||||||
|                 acceptsMultiple: true, |                 acceptsMultiple: true, | ||||||
|                 enumProvider: commonEnumProviders.numbersAndVariables, |                 enumProvider: commonEnumProviders.numbersAndVariables, | ||||||
| @@ -1668,7 +1686,10 @@ export function registerVariableCommands() { | |||||||
|         splitUnnamedArgument: true, |         splitUnnamedArgument: true, | ||||||
|         helpString: ` |         helpString: ` | ||||||
|             <div> |             <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> | ||||||
|             <div> |             <div> | ||||||
|                 <strong>Examples:</strong> |                 <strong>Examples:</strong> | ||||||
| @@ -1676,6 +1697,9 @@ export function registerVariableCommands() { | |||||||
|                     <li> |                     <li> | ||||||
|                         <pre><code class="language-stscript">/max 10 i 30 j</code></pre> |                         <pre><code class="language-stscript">/max 10 i 30 j</code></pre> | ||||||
|                     </li> |                     </li> | ||||||
|  |                     <li> | ||||||
|  |                         <pre><code class="language-stscript">/max ["count", 15, 2, "i"]</code></pre> | ||||||
|  |                     </li> | ||||||
|                 </ul> |                 </ul> | ||||||
|             </div> |             </div> | ||||||
|         `, |         `, | ||||||
| @@ -1687,7 +1711,7 @@ export function registerVariableCommands() { | |||||||
|         unnamedArgumentList: [ |         unnamedArgumentList: [ | ||||||
|             SlashCommandArgument.fromProps({ |             SlashCommandArgument.fromProps({ | ||||||
|                 description: 'values to find the min', |                 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, |                 isRequired: true, | ||||||
|                 acceptsMultiple: true, |                 acceptsMultiple: true, | ||||||
|                 enumProvider: commonEnumProviders.numbersAndVariables, |                 enumProvider: commonEnumProviders.numbersAndVariables, | ||||||
| @@ -1698,7 +1722,9 @@ export function registerVariableCommands() { | |||||||
|         helpString: ` |         helpString: ` | ||||||
|             <div> |             <div> | ||||||
|                 Returns the minimum value of the set of values and passes the result down the pipe. |                 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> | ||||||
|             <div> |             <div> | ||||||
|                 <strong>Example:</strong> |                 <strong>Example:</strong> | ||||||
| @@ -1706,6 +1732,9 @@ export function registerVariableCommands() { | |||||||
|                     <li> |                     <li> | ||||||
|                         <pre><code class="language-stscript">/min 10 i 30 j</code></pre> |                         <pre><code class="language-stscript">/min 10 i 30 j</code></pre> | ||||||
|                     </li> |                     </li> | ||||||
|  |                     <li> | ||||||
|  |                         <pre><code class="language-stscript">/min ["count", 15, 2, "i"]</code></pre> | ||||||
|  |                     </li> | ||||||
|                 </ul> |                 </ul> | ||||||
|             </div> |             </div> | ||||||
|         `, |         `, | ||||||
| @@ -1717,7 +1746,7 @@ export function registerVariableCommands() { | |||||||
|         unnamedArgumentList: [ |         unnamedArgumentList: [ | ||||||
|             SlashCommandArgument.fromProps({ |             SlashCommandArgument.fromProps({ | ||||||
|                 description: 'values to subtract, starting form the first provided value', |                 description: 'values to subtract, starting form the first provided value', | ||||||
|                 typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME], |                 typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME, ARGUMENT_TYPE.LIST], | ||||||
|                 isRequired: true, |                 isRequired: true, | ||||||
|                 acceptsMultiple: true, |                 acceptsMultiple: true, | ||||||
|                 enumProvider: commonEnumProviders.numbersAndVariables, |                 enumProvider: commonEnumProviders.numbersAndVariables, | ||||||
| @@ -1728,7 +1757,9 @@ export function registerVariableCommands() { | |||||||
|         helpString: ` |         helpString: ` | ||||||
|             <div> |             <div> | ||||||
|                 Performs a subtraction of the set of values and passes the result down the pipe. |                 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> | ||||||
|             <div> |             <div> | ||||||
|                 <strong>Example:</strong> |                 <strong>Example:</strong> | ||||||
| @@ -1736,6 +1767,9 @@ export function registerVariableCommands() { | |||||||
|                     <li> |                     <li> | ||||||
|                         <pre><code class="language-stscript">/sub i 5</code></pre> |                         <pre><code class="language-stscript">/sub i 5</code></pre> | ||||||
|                     </li> |                     </li> | ||||||
|  |                     <li> | ||||||
|  |                         <pre><code class="language-stscript">/sub ["count", 4, "i"]</code></pre> | ||||||
|  |                     </li> | ||||||
|                 </ul> |                 </ul> | ||||||
|             </div> |             </div> | ||||||
|         `, |         `, | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user