mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-02-26 00:47:42 +01:00
Fix variables casting
This commit is contained in:
parent
bcf73e0e55
commit
d81371c2b7
@ -9,7 +9,7 @@ function getLocalVariable(name) {
|
|||||||
|
|
||||||
const localVariable = chat_metadata?.variables[name];
|
const localVariable = chat_metadata?.variables[name];
|
||||||
|
|
||||||
return isNaN(Number(localVariable)) ? (localVariable || '') : Number(localVariable);
|
return (localVariable === '' || isNaN(Number(localVariable))) ? (localVariable || '') : Number(localVariable);
|
||||||
}
|
}
|
||||||
|
|
||||||
function setLocalVariable(name, value) {
|
function setLocalVariable(name, value) {
|
||||||
@ -25,7 +25,7 @@ function setLocalVariable(name, value) {
|
|||||||
function getGlobalVariable(name) {
|
function getGlobalVariable(name) {
|
||||||
const globalVariable = extension_settings.variables.global[name];
|
const globalVariable = extension_settings.variables.global[name];
|
||||||
|
|
||||||
return isNaN(Number(globalVariable)) ? (globalVariable || '') : Number(globalVariable);
|
return (globalVariable === '' || isNaN(Number(globalVariable))) ? (globalVariable || '') : Number(globalVariable);
|
||||||
}
|
}
|
||||||
|
|
||||||
function setGlobalVariable(name, value) {
|
function setGlobalVariable(name, value) {
|
||||||
@ -160,26 +160,38 @@ async function ifCallback(args, command) {
|
|||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function existsLocalVariable(name) {
|
||||||
|
return chat_metadata.variables && chat_metadata.variables[name] !== undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
function existsGlobalVariable(name) {
|
||||||
|
return extension_settings.variables.global && extension_settings.variables.global[name] !== undefined;
|
||||||
|
}
|
||||||
|
|
||||||
function parseBooleanOperands(args) {
|
function parseBooleanOperands(args) {
|
||||||
// Resultion order: numeric literal, local variable, global variable, string literal
|
// Resultion order: numeric literal, local variable, global variable, string literal
|
||||||
function getOperand(operand) {
|
function getOperand(operand) {
|
||||||
|
if (operand === undefined) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
const operandNumber = Number(operand);
|
const operandNumber = Number(operand);
|
||||||
const operandLocalVariable = getLocalVariable(operand);
|
|
||||||
const operandGlobalVariable = getGlobalVariable(operand);
|
|
||||||
const stringLiteral = String(operand);
|
|
||||||
|
|
||||||
if (!isNaN(operandNumber)) {
|
if (!isNaN(operandNumber)) {
|
||||||
return operandNumber;
|
return operandNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (operandLocalVariable !== undefined && operandLocalVariable !== null && operandLocalVariable !== '') {
|
if (existsLocalVariable(operand)) {
|
||||||
return operandLocalVariable;
|
const operandLocalVariable = getLocalVariable(operand);
|
||||||
|
return operandLocalVariable || '';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (operandGlobalVariable !== undefined && operandGlobalVariable !== null && operandGlobalVariable !== '') {
|
if (existsGlobalVariable(operand)) {
|
||||||
return operandGlobalVariable;
|
const operandGlobalVariable = getGlobalVariable(operand);
|
||||||
|
return operandGlobalVariable || '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const stringLiteral = String(operand);
|
||||||
return stringLiteral || '';
|
return stringLiteral || '';
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -187,11 +199,6 @@ function parseBooleanOperands(args) {
|
|||||||
const right = getOperand(args.b || args.right || args.second || args.y);
|
const right = getOperand(args.b || args.right || args.second || args.y);
|
||||||
const rule = args.rule;
|
const rule = args.rule;
|
||||||
|
|
||||||
if ((typeof left === 'number' && isNaN(left)) || (typeof left === 'string' && left === '')) {
|
|
||||||
toastr.warning('The left operand must be a number, string or a variable name.', 'Invalid command');
|
|
||||||
throw new Error('Invalid command.');
|
|
||||||
}
|
|
||||||
|
|
||||||
return { a: left, b: right, rule };
|
return { a: left, b: right, rule };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user