mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Add config value type converters for numbers and booleans
This commit is contained in:
51
src/util.js
51
src/util.js
@@ -54,19 +54,33 @@ export function getConfig() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the value for the given key from the config object.
|
||||
* @param {string} key - Key to get from the config object
|
||||
* @param {any} defaultValue - Default value to return if the key is not found
|
||||
* @param {'number'|'boolean'|null} typeConverter - Type to convert the value to
|
||||
* @returns {any} Value for the given key
|
||||
*/
|
||||
export function getConfigValue(key, defaultValue = null) {
|
||||
const envKey = keyToEnv(key);
|
||||
if (envKey in process.env) {
|
||||
return process.env[envKey];
|
||||
export function getConfigValue(key, defaultValue = null, typeConverter = null) {
|
||||
function _getValue() {
|
||||
const envKey = keyToEnv(key);
|
||||
if (envKey in process.env) {
|
||||
return process.env[envKey];
|
||||
}
|
||||
const config = getConfig();
|
||||
return _.get(config, key, defaultValue);
|
||||
}
|
||||
|
||||
const value = _getValue();
|
||||
switch (typeConverter) {
|
||||
case 'number':
|
||||
return Number(value);
|
||||
case 'boolean':
|
||||
return toBoolean(value);
|
||||
default:
|
||||
return value;
|
||||
}
|
||||
const config = getConfig();
|
||||
return _.get(config, key, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -392,7 +406,7 @@ export function generateTimestamp() {
|
||||
* @param {number?} limit Maximum number of backups to keep. If null, the limit is determined by the `backups.common.numberOfBackups` config value.
|
||||
*/
|
||||
export function removeOldBackups(directory, prefix, limit = null) {
|
||||
const MAX_BACKUPS = limit ?? Number(getConfigValue('backups.common.numberOfBackups', 50));
|
||||
const MAX_BACKUPS = limit ?? Number(getConfigValue('backups.common.numberOfBackups', 50, 'number'));
|
||||
|
||||
let files = fs.readdirSync(directory).filter(f => f.startsWith(prefix));
|
||||
if (files.length > MAX_BACKUPS) {
|
||||
@@ -745,6 +759,27 @@ export async function canResolve(name, useIPv6 = true, useIPv4 = true) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts various JavaScript primitives to boolean values.
|
||||
* Handles special case for "true"/"false" strings (case-insensitive)
|
||||
*
|
||||
* @param {any} value - The value to convert to boolean
|
||||
* @returns {boolean} - The boolean representation of the value
|
||||
*/
|
||||
export function toBoolean(value) {
|
||||
// Handle string values case-insensitively
|
||||
if (typeof value === 'string') {
|
||||
// Trim and convert to lowercase for case-insensitive comparison
|
||||
const trimmedLower = value.trim().toLowerCase();
|
||||
|
||||
// Handle explicit "true"/"false" strings
|
||||
if (trimmedLower === 'true') return true;
|
||||
if (trimmedLower === 'false') return false;
|
||||
}
|
||||
|
||||
// Handle all other JavaScript values based on their "truthiness"
|
||||
return Boolean(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* converts string to boolean accepts 'true' or 'false' else it returns the string put in
|
||||
@@ -761,7 +796,7 @@ export function stringToBool(str) {
|
||||
* Setup the minimum log level
|
||||
*/
|
||||
export function setupLogLevel() {
|
||||
const logLevel = getConfigValue('minLogLevel', LOG_LEVELS.DEBUG);
|
||||
const logLevel = getConfigValue('minLogLevel', LOG_LEVELS.DEBUG, 'number');
|
||||
|
||||
globalThis.console.debug = logLevel <= LOG_LEVELS.DEBUG ? console.debug : () => {};
|
||||
globalThis.console.info = logLevel <= LOG_LEVELS.INFO ? console.info : () => {};
|
||||
|
Reference in New Issue
Block a user