mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Add type conversion for /setvar commands with index
This commit is contained in:
@ -4,6 +4,7 @@ import { isMobile } from './RossAscends-mods.js';
|
||||
import { collapseNewlines } from './power-user.js';
|
||||
import { debounce_timeout } from './constants.js';
|
||||
import { Popup, POPUP_RESULT, POPUP_TYPE } from './popup.js';
|
||||
import { SlashCommandClosure } from './slash-commands/SlashCommandClosure.js';
|
||||
|
||||
/**
|
||||
* Pagination status string template.
|
||||
@ -33,6 +34,74 @@ export function isValidUrl(value) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts string to a value of a given type. Includes pythonista-friendly aliases.
|
||||
* @param {string|SlashCommandClosure} value String value
|
||||
* @param {string} type Type to convert to
|
||||
* @returns {any} Converted value
|
||||
*/
|
||||
export function convertValueType(value, type) {
|
||||
if (value instanceof SlashCommandClosure || typeof type !== 'string') {
|
||||
return value;
|
||||
}
|
||||
|
||||
type = type.trim().toLowerCase();
|
||||
|
||||
switch (type) {
|
||||
case 'string':
|
||||
case 'str':
|
||||
return String(value);
|
||||
|
||||
case 'null':
|
||||
return null;
|
||||
|
||||
case 'undefined':
|
||||
case 'none':
|
||||
return undefined;
|
||||
|
||||
case 'number':
|
||||
return Number(value);
|
||||
|
||||
case 'int':
|
||||
return parseInt(value, 10);
|
||||
|
||||
case 'float':
|
||||
return parseFloat(value);
|
||||
|
||||
case 'boolean':
|
||||
case 'bool':
|
||||
return isTrueBoolean(value);
|
||||
|
||||
case 'list':
|
||||
case 'array':
|
||||
try {
|
||||
const parsedArray = JSON.parse(value);
|
||||
if (Array.isArray(parsedArray)) {
|
||||
return parsedArray;
|
||||
}
|
||||
throw new Error('Value is not an array.');
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
|
||||
case 'object':
|
||||
case 'dict':
|
||||
case 'dictionary':
|
||||
try {
|
||||
const parsedObject = JSON.parse(value);
|
||||
if (typeof parsedObject === 'object') {
|
||||
return parsedObject;
|
||||
}
|
||||
throw new Error('Value is not an object.');
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
|
||||
default:
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses ranges like 10-20 or 10.
|
||||
* Range is inclusive. Start must be less than end.
|
||||
|
Reference in New Issue
Block a user