Refactor hideMessageCallback and unhideMessageCallback to remove unnecessary console logs. Introduce onlyUniqueJson and sortIgnoreCaseAndAccents utility functions

This commit is contained in:
Cohee
2025-03-12 10:18:02 +02:00
parent 1026e1f8e9
commit e7189a1260
3 changed files with 38 additions and 17 deletions

View File

@ -3055,10 +3055,6 @@ async function askCharacter(args, text) {
}
async function hideMessageCallback(args, value) {
if (!value) {
console.log('No range provided. Hiding the last message.');
}
const range = value ? stringToRange(value, 0, chat.length - 1) : { start: chat.length - 1, end: chat.length - 1 };
if (!range) {
@ -3072,10 +3068,6 @@ async function hideMessageCallback(args, value) {
}
async function unhideMessageCallback(args, value) {
if (!value) {
console.log('No range provided. Unhiding the last message');
}
const range = value ? stringToRange(value, 0, chat.length - 1) : { start: chat.length - 1, end: chat.length - 1 };
if (!range) {

View File

@ -3,7 +3,7 @@ import { extension_settings } from '../extensions.js';
import { getGroupMembers, groups } from '../group-chats.js';
import { power_user } from '../power-user.js';
import { searchCharByName, getTagsList, tags, tag_map } from '../tags.js';
import { onlyUnique } from '../utils.js';
import { onlyUniqueJson, sortIgnoreCaseAndAccents } from '../utils.js';
import { world_names } from '../world-info.js';
import { SlashCommandClosure } from './SlashCommandClosure.js';
import { SlashCommandEnumValue, enumTypes } from './SlashCommandEnumValue.js';
@ -266,7 +266,14 @@ export const commonEnumProviders = {
*
* @returns {SlashCommandEnumValue[]}
*/
messageNames: () => chat.map((message) => message.name).filter(onlyUnique).sort(Intl.Collator().compare).map(name => new SlashCommandEnumValue(name)),
messageNames: () => chat
.map(message => ({
name: message.name,
icon: message.is_user ? enumIcons.user : enumIcons.assistant,
}))
.filter(onlyUniqueJson)
.sort((a, b) => sortIgnoreCaseAndAccents(a.name, b.name))
.map(name => new SlashCommandEnumValue(name.name, null, null, name.icon)),
/**
* All existing worlds / lorebooks

View File

@ -14,7 +14,7 @@ import { Popup, POPUP_RESULT, POPUP_TYPE } from './popup.js';
import { SlashCommandClosure } from './slash-commands/SlashCommandClosure.js';
import { getTagsList } from './tags.js';
import { groups, selected_group } from './group-chats.js';
import { getCurrentLocale } from './i18n.js';
import { getCurrentLocale, t } from './i18n.js';
/**
* Pagination status string template.
@ -196,6 +196,17 @@ export function onlyUnique(value, index, array) {
return array.indexOf(value) === index;
}
/**
* Determines if a value is unique in an array of objects.
* @param {any} value Current value.
* @param {number} index Current index.
* @param {any[]} array The array being processed.
* @returns {boolean} True if the value is unique, false otherwise.
*/
export function onlyUniqueJson(value, index, array) {
return array.map(v => JSON.stringify(v)).indexOf(JSON.stringify(value)) === index;
}
/**
* Removes the first occurrence of a specified item from an array
*
@ -1799,8 +1810,9 @@ export function runAfterAnimation(control, callback, timeout = 500) {
*
* @param {string} a - The first string to compare.
* @param {string} b - The second string to compare.
* @param {(a:string,b:string)=>boolean} comparisonFunction - The function to use for the comparison.
* @returns {*} - The result of the comparison.
* @param {(a:string,b:string)=>T} comparisonFunction - The function to use for the comparison.
* @returns {T} - The result of the comparison.
* @template T
*/
export function compareIgnoreCaseAndAccents(a, b, comparisonFunction) {
if (!a || !b) return comparisonFunction(a, b); // Return the comparison result if either string is empty
@ -1837,6 +1849,16 @@ export function equalsIgnoreCaseAndAccents(a, b) {
return compareIgnoreCaseAndAccents(a, b, (a, b) => a === b);
}
/**
* Performs a case-insensitive and accent-insensitive sort.
* @param {string} a - The first string to compare
* @param {string} b - The second string to compare
* @returns {number} -1 if a < b, 1 if a > b, 0 if a === b
*/
export function sortIgnoreCaseAndAccents(a, b) {
return compareIgnoreCaseAndAccents(a, b, (a, b) => a?.localeCompare(b));
}
/**
* @typedef {object} Select2Option The option object for select2 controls
* @property {string} id - The unique ID inside this select
@ -2231,8 +2253,8 @@ export function findPersona({ name = null, allowAvatar = true, insensitive = tru
// Search for matching personas by name
const matchingPersonas = personas.filter(a => matches(a));
if (matchingPersonas.length > 1) {
if (!quiet) toastr.warning('Multiple personas found for given conditions.');
else console.warn('Multiple personas found for given conditions. Returning the first match.');
if (!quiet) toastr.warning(t`Multiple personas found for given conditions.`);
else console.warn(t`Multiple personas found for given conditions. Returning the first match.`);
}
return matchingPersonas[0] || null;
@ -2270,8 +2292,8 @@ export function findChar({ name = null, allowAvatar = true, insensitive = true,
if (preferCurrentChar) {
const preferredCharSearch = currentChars.filter(matches);
if (preferredCharSearch.length > 1) {
if (!quiet) toastr.warning('Multiple characters found for given conditions.');
else console.warn('Multiple characters found for given conditions. Returning the first match.');
if (!quiet) toastr.warning(t`Multiple characters found for given conditions.`);
else console.warn(t`Multiple characters found for given conditions. Returning the first match.`);
}
if (preferredCharSearch.length) {
return preferredCharSearch[0];