Only convert variable if needed

6f85327078e1abd841a3cba9da71d254c5984aa7

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +8 -11Showing whitespace changes
public/scripts/slash-commands/SlashCommandScope.js+4 -5
@@ -57,7 +57,6 @@ export class SlashCommandScope {
57 this.variables[key] = value;57 this.variables[key] = value;
58 }58 }
59 setVariable(key, value, index = null, type = null) {59 setVariable(key, value, index = null, type = null) {
60 value = convertValueType(value, type);
61 if (this.existsVariableInScope(key)) {60 if (this.existsVariableInScope(key)) {
62 if (index !== null && index !== undefined) {61 if (index !== null && index !== undefined) {
63 let v = this.variables[key];62 let v = this.variables[key];
@@ -65,13 +64,13 @@ export class SlashCommandScope {
65 v = JSON.parse(v);64 v = JSON.parse(v);
66 const numIndex = Number(index);65 const numIndex = Number(index);
67 if (Number.isNaN(numIndex)) {66 if (Number.isNaN(numIndex)) {
68 v[index] = value;67 v[index] = convertValueType(value, type);
69 } else {68 } else {
70 v[numIndex] = value;69 v[numIndex] = convertValueType(value, type);
71 }70 }
72 v = JSON.stringify(v);71 v = JSON.stringify(v);
73 } catch {72 } catch {
74 v[index] = value;73 v[index] = convertValueType(value, type);
75 }74 }
76 this.variables[key] = v;75 this.variables[key] = v;
77 } else {76 } else {
@@ -80,7 +79,7 @@ export class SlashCommandScope {
80 return value;79 return value;
81 }80 }
82 if (this.parent) {81 if (this.parent) {
83 return this.parent.setVariable(key, value, index);82 return this.parent.setVariable(key, value, index, type);
84 }83 }
85 throw new SlashCommandScopeVariableNotFoundError(`No such variable: "${key}"`);84 throw new SlashCommandScopeVariableNotFoundError(`No such variable: "${key}"`);
86 }85 }
public/scripts/variables.js+4 -6
@@ -51,19 +51,18 @@ function setLocalVariable(name, value, args = {}) {
5151
52 if (args.index !== undefined) {52 if (args.index !== undefined) {
53 try {53 try {
54 value = convertValueType(value, args.type);
55 let localVariable = JSON.parse(chat_metadata.variables[name] ?? 'null');54 let localVariable = JSON.parse(chat_metadata.variables[name] ?? 'null');
56 const numIndex = Number(args.index);55 const numIndex = Number(args.index);
57 if (Number.isNaN(numIndex)) {56 if (Number.isNaN(numIndex)) {
58 if (localVariable === null) {57 if (localVariable === null) {
59 localVariable = {};58 localVariable = {};
60 }59 }
61 localVariable[args.index] = value;60 localVariable[args.index] = convertValueType(value, args.type);
62 } else {61 } else {
63 if (localVariable === null) {62 if (localVariable === null) {
64 localVariable = [];63 localVariable = [];
65 }64 }
66 localVariable[numIndex] = value;65 localVariable[numIndex] = convertValueType(value, args.type);
67 }66 }
68 chat_metadata.variables[name] = JSON.stringify(localVariable);67 chat_metadata.variables[name] = JSON.stringify(localVariable);
69 } catch {68 } catch {
@@ -101,19 +100,18 @@ function getGlobalVariable(name, args = {}) {
101function setGlobalVariable(name, value, args = {}) {100function setGlobalVariable(name, value, args = {}) {
102 if (args.index !== undefined) {101 if (args.index !== undefined) {
103 try {102 try {
104 value = convertValueType(value, args.type);
105 let globalVariable = JSON.parse(extension_settings.variables.global[name] ?? 'null');103 let globalVariable = JSON.parse(extension_settings.variables.global[name] ?? 'null');
106 const numIndex = Number(args.index);104 const numIndex = Number(args.index);
107 if (Number.isNaN(numIndex)) {105 if (Number.isNaN(numIndex)) {
108 if (globalVariable === null) {106 if (globalVariable === null) {
109 globalVariable = {};107 globalVariable = {};
110 }108 }
111 globalVariable[args.index] = value;109 globalVariable[args.index] = convertValueType(value, args.type);
112 } else {110 } else {
113 if (globalVariable === null) {111 if (globalVariable === null) {
114 globalVariable = [];112 globalVariable = [];
115 }113 }
116 globalVariable[numIndex] = value;114 globalVariable[numIndex] = convertValueType(value, args.type);
117 }115 }
118 extension_settings.variables.global[name] = JSON.stringify(globalVariable);116 extension_settings.variables.global[name] = JSON.stringify(globalVariable);
119 } catch {117 } catch {