mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
basics for new parser
This commit is contained in:
102
public/scripts/slash-commands/SlashCommandScope.js
Normal file
102
public/scripts/slash-commands/SlashCommandScope.js
Normal file
@ -0,0 +1,102 @@
|
||||
export class SlashCommandScope {
|
||||
// @ts-ignore
|
||||
/**@type {Map<String, Object>}*/ variables = {};
|
||||
// @ts-ignore
|
||||
/**@type {Map<String, Object>}*/ macros = {};
|
||||
/**@type {SlashCommandScope}*/ parent;
|
||||
/**@type {String}*/ #pipe;
|
||||
get pipe() {
|
||||
return this.#pipe ?? this.parent?.pipe;
|
||||
}
|
||||
set pipe(value) {
|
||||
this.#pipe = value;
|
||||
}
|
||||
|
||||
|
||||
constructor(parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
getCopy() {
|
||||
const scope = new SlashCommandScope(this.parent);
|
||||
scope.variables = Object.assign({}, this.variables);
|
||||
scope.macros = this.macros;
|
||||
scope.#pipe = this.#pipe;
|
||||
return scope;
|
||||
}
|
||||
|
||||
|
||||
setMacro(key, value) {
|
||||
this.macros[key] = value;
|
||||
}
|
||||
|
||||
|
||||
existsVariableInScope(key) {
|
||||
return Object.keys(this.variables).includes(key);
|
||||
}
|
||||
existsVariable(key) {
|
||||
return Object.keys(this.variables).includes(key) || this.parent?.existsVariable(key);
|
||||
}
|
||||
letVariable(key, value = undefined) {
|
||||
if (this.existsVariableInScope(key)) throw new SlashCommandScopeVariableExistsError(`Variable named "${key}" already exists.`);
|
||||
this.variables[key] = value;
|
||||
}
|
||||
setVariable(key, value, index = null) {
|
||||
if (this.existsVariableInScope(key)) {
|
||||
if (index !== null && index !== undefined) {
|
||||
let v = this.variables[key];
|
||||
try {
|
||||
v = JSON.parse(v);
|
||||
const numIndex = Number(index);
|
||||
if (Number.isNaN(numIndex)) {
|
||||
v[index] = value;
|
||||
} else {
|
||||
v[numIndex] = value;
|
||||
}
|
||||
v = JSON.stringify(v);
|
||||
} catch {
|
||||
v[index] = value;
|
||||
}
|
||||
this.variables[key] = v;
|
||||
} else {
|
||||
this.variables[key] = value;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
if (this.parent) {
|
||||
return this.parent.setVariable(key, value, index);
|
||||
}
|
||||
throw new SlashCommandScopeVariableNotFoundError(`No such variable: "${key}"`);
|
||||
}
|
||||
getVariable(key, index = null) {
|
||||
if (this.existsVariableInScope(key)) {
|
||||
if (index !== null && index !== undefined) {
|
||||
let v = this.variables[key];
|
||||
try { v = JSON.parse(v); } catch { /* empty */ }
|
||||
const numIndex = Number(index);
|
||||
if (Number.isNaN(numIndex)) {
|
||||
v = v[index];
|
||||
} else {
|
||||
v = v[numIndex];
|
||||
}
|
||||
if (typeof v == 'object') return JSON.stringify(v);
|
||||
return v;
|
||||
} else {
|
||||
const value = this.variables[key];
|
||||
return (value === '' || isNaN(Number(value))) ? (value || '') : Number(value);
|
||||
}
|
||||
}
|
||||
if (this.parent) {
|
||||
return this.parent.getVariable(key, index);
|
||||
}
|
||||
throw new SlashCommandScopeVariableNotFoundError(`No such variable: "${key}"`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
export class SlashCommandScopeVariableExistsError extends Error {}
|
||||
|
||||
|
||||
export class SlashCommandScopeVariableNotFoundError extends Error {}
|
Reference in New Issue
Block a user