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

e7189a1260ff989afdcd43d3458617bca4b6d3e9

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

3 files changed, +38 -17Showing whitespace changes
public/scripts/slash-commands.js+0 -8
@@ -3055,10 +3055,6 @@ async function askCharacter(args, text) {
30553055}
30563056
30573057async function hideMessageCallback(args, value) {
3058- if (!value) {
3059- console.log('No range provided. Hiding the last message.');
3060- }
3061-
30623058 const range = value ? stringToRange(value, 0, chat.length - 1) : { start: chat.length - 1, end: chat.length - 1 };
30633059
30643060 if (!range) {
@@ -3072,10 +3068,6 @@ async function hideMessageCallback(args, value) {
30723068}
30733069
30743070async function unhideMessageCallback(args, value) {
3075- if (!value) {
3076- console.log('No range provided. Unhiding the last message');
3077- }
3078-
30793071 const range = value ? stringToRange(value, 0, chat.length - 1) : { start: chat.length - 1, end: chat.length - 1 };
30803072
30813073 if (!range) {
public/scripts/slash-commands/SlashCommandCommonEnumsProvider.js+9 -2
@@ -3,7 +3,7 @@ import { extension_settings } from '../extensions.js';
33import { getGroupMembers, groups } from '../group-chats.js';
44import { power_user } from '../power-user.js';
55import { searchCharByName, getTagsList, tags, tag_map } from '../tags.js';
66import { onlyUniqueonlyUniqueJson, sortIgnoreCaseAndAccents } from '../utils.js';
77import { world_names } from '../world-info.js';
88import { SlashCommandClosure } from './SlashCommandClosure.js';
99import { SlashCommandEnumValue, enumTypes } from './SlashCommandEnumValue.js';
@@ -266,7 +266,14 @@ export const commonEnumProviders = {
266266 *
267267 * @returns {SlashCommandEnumValue[]}
268268 */
269- messageNames: () => chat.map((message) => message.name).filter(onlyUnique).sort(Intl.Collator().compare).map(name => new SlashCommandEnumValue(name)),
269+ messageNames: () => chat
270+ .map(message => ({
271+ name: message.name,
272+ icon: message.is_user ? enumIcons.user : enumIcons.assistant,
273+ }))
274+ .filter(onlyUniqueJson)
275+ .sort((a, b) => sortIgnoreCaseAndAccents(a.name, b.name))
276+ .map(name => new SlashCommandEnumValue(name.name, null, null, name.icon)),
270277
271278 /**
272279 * All existing worlds / lorebooks
public/scripts/utils.js+29 -7
@@ -14,7 +14,7 @@ import { Popup, POPUP_RESULT, POPUP_TYPE } from './popup.js';
1414import { SlashCommandClosure } from './slash-commands/SlashCommandClosure.js';
1515import { getTagsList } from './tags.js';
1616import { groups, selected_group } from './group-chats.js';
1717import { getCurrentLocale, t } from './i18n.js';
1818
1919/**
2020 * Pagination status string template.
@@ -197,6 +197,17 @@ export function onlyUnique(value, index, array) {
197197}
198198
199199/**
200+ * Determines if a value is unique in an array of objects.
201+ * @param {any} value Current value.
202+ * @param {number} index Current index.
203+ * @param {any[]} array The array being processed.
204+ * @returns {boolean} True if the value is unique, false otherwise.
205+ */
206+export function onlyUniqueJson(value, index, array) {
207+ return array.map(v => JSON.stringify(v)).indexOf(JSON.stringify(value)) === index;
208+}
209+
210+/**
200211 * Removes the first occurrence of a specified item from an array
201212 *
202213 * @param {*[]} array - The array from which to remove the item
@@ -1799,8 +1810,9 @@ export function runAfterAnimation(control, callback, timeout = 500) {
17991810 *
18001811 * @param {string} a - The first string to compare.
18011812 * @param {string} b - The second string to compare.
18021813 * @param {(a:string,b:string)=>booleanT} comparisonFunction - The function to use for the comparison.
18031814 * @returns {*T} - The result of the comparison.
1815+ * @template T
18041816 */
18051817export function compareIgnoreCaseAndAccents(a, b, comparisonFunction) {
18061818 if (!a || !b) return comparisonFunction(a, b); // Return the comparison result if either string is empty
@@ -1838,6 +1850,16 @@ export function equalsIgnoreCaseAndAccents(a, b) {
18381850}
18391851
18401852/**
1853+ * Performs a case-insensitive and accent-insensitive sort.
1854+ * @param {string} a - The first string to compare
1855+ * @param {string} b - The second string to compare
1856+ * @returns {number} -1 if a < b, 1 if a > b, 0 if a === b
1857+ */
1858+export function sortIgnoreCaseAndAccents(a, b) {
1859+ return compareIgnoreCaseAndAccents(a, b, (a, b) => a?.localeCompare(b));
1860+}
1861+
1862+/**
18411863 * @typedef {object} Select2Option The option object for select2 controls
18421864 * @property {string} id - The unique ID inside this select
18431865 * @property {string} text - The text for this option
@@ -2231,8 +2253,8 @@ export function findPersona({ name = null, allowAvatar = true, insensitive = tru
22312253 // Search for matching personas by name
22322254 const matchingPersonas = personas.filter(a => matches(a));
22332255 if (matchingPersonas.length > 1) {
22342256 if (!quiet) toastr.warning('t`Multiple personas found for given conditions.'`);
22352257 else console.warn('t`Multiple personas found for given conditions. Returning the first match.'`);
22362258 }
22372259
22382260 return matchingPersonas[0] || null;
@@ -2270,8 +2292,8 @@ export function findChar({ name = null, allowAvatar = true, insensitive = true,
22702292 if (preferCurrentChar) {
22712293 const preferredCharSearch = currentChars.filter(matches);
22722294 if (preferredCharSearch.length > 1) {
22732295 if (!quiet) toastr.warning('t`Multiple characters found for given conditions.'`);
22742296 else console.warn('t`Multiple characters found for given conditions. Returning the first match.'`);
22752297 }
22762298 if (preferredCharSearch.length) {
22772299 return preferredCharSearch[0];