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 -17Ignore whitespace
public/scripts/slash-commands.js+0 -8
@@ -3055,10 +3055,6 @@ async function askCharacter(args, text) {
3055}3055}
30563056
3057async function hideMessageCallback(args, value) {3057async function hideMessageCallback(args, value) {
3058 if (!value) {
3059 console.log('No range provided. Hiding the last message.');
3060 }
3061
3062 const range = value ? stringToRange(value, 0, chat.length - 1) : { start: chat.length - 1, end: chat.length - 1 };3058 const range = value ? stringToRange(value, 0, chat.length - 1) : { start: chat.length - 1, end: chat.length - 1 };
30633059
3064 if (!range) {3060 if (!range) {
@@ -3072,10 +3068,6 @@ async function hideMessageCallback(args, value) {
3072}3068}
30733069
3074async function unhideMessageCallback(args, value) {3070async function unhideMessageCallback(args, value) {
3075 if (!value) {
3076 console.log('No range provided. Unhiding the last message');
3077 }
3078
3079 const range = value ? stringToRange(value, 0, chat.length - 1) : { start: chat.length - 1, end: chat.length - 1 };3071 const range = value ? stringToRange(value, 0, chat.length - 1) : { start: chat.length - 1, end: chat.length - 1 };
30803072
3081 if (!range) {3073 if (!range) {
public/scripts/slash-commands/SlashCommandCommonEnumsProvider.js+9 -2
@@ -3,7 +3,7 @@ import { extension_settings } from '../extensions.js';
3import { getGroupMembers, groups } from '../group-chats.js';3import { getGroupMembers, groups } from '../group-chats.js';
4import { power_user } from '../power-user.js';4import { power_user } from '../power-user.js';
5import { searchCharByName, getTagsList, tags, tag_map } from '../tags.js';5import { searchCharByName, getTagsList, tags, tag_map } from '../tags.js';
6import { onlyUnique } from '../utils.js';6import { onlyUniqueJson, sortIgnoreCaseAndAccents } from '../utils.js';
7import { world_names } from '../world-info.js';7import { world_names } from '../world-info.js';
8import { SlashCommandClosure } from './SlashCommandClosure.js';8import { SlashCommandClosure } from './SlashCommandClosure.js';
9import { SlashCommandEnumValue, enumTypes } from './SlashCommandEnumValue.js';9import { SlashCommandEnumValue, enumTypes } from './SlashCommandEnumValue.js';
@@ -266,7 +266,14 @@ export const commonEnumProviders = {
266 *266 *
267 * @returns {SlashCommandEnumValue[]}267 * @returns {SlashCommandEnumValue[]}
268 */268 */
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
271 /**278 /**
272 * All existing worlds / lorebooks279 * All existing worlds / lorebooks
public/scripts/utils.js+29 -7
@@ -14,7 +14,7 @@ import { Popup, POPUP_RESULT, POPUP_TYPE } from './popup.js';
14import { SlashCommandClosure } from './slash-commands/SlashCommandClosure.js';14import { SlashCommandClosure } from './slash-commands/SlashCommandClosure.js';
15import { getTagsList } from './tags.js';15import { getTagsList } from './tags.js';
16import { groups, selected_group } from './group-chats.js';16import { groups, selected_group } from './group-chats.js';
17import { getCurrentLocale } from './i18n.js';17import { getCurrentLocale, t } from './i18n.js';
1818
19/**19/**
20 * Pagination status string template.20 * Pagination status string template.
@@ -197,6 +197,17 @@ export function onlyUnique(value, index, array) {
197}197}
198198
199/**199/**
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 */
206export function onlyUniqueJson(value, index, array) {
207 return array.map(v => JSON.stringify(v)).indexOf(JSON.stringify(value)) === index;
208}
209
210/**
200 * Removes the first occurrence of a specified item from an array211 * Removes the first occurrence of a specified item from an array
201 *212 *
202 * @param {*[]} array - The array from which to remove the item213 * @param {*[]} array - The array from which to remove the item
@@ -1799,8 +1810,9 @@ export function runAfterAnimation(control, callback, timeout = 500) {
1799 *1810 *
1800 * @param {string} a - The first string to compare.1811 * @param {string} a - The first string to compare.
1801 * @param {string} b - The second string to compare.1812 * @param {string} b - The second string to compare.
1802 * @param {(a:string,b:string)=>boolean} comparisonFunction - The function to use for the comparison.1813 * @param {(a:string,b:string)=>T} comparisonFunction - The function to use for the comparison.
1803 * @returns {*} - The result of the comparison.1814 * @returns {T} - The result of the comparison.
1815 * @template T
1804 */1816 */
1805export function compareIgnoreCaseAndAccents(a, b, comparisonFunction) {1817export function compareIgnoreCaseAndAccents(a, b, comparisonFunction) {
1806 if (!a || !b) return comparisonFunction(a, b); // Return the comparison result if either string is empty1818 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) {
1838}1850}
18391851
1840/**1852/**
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 */
1858export function sortIgnoreCaseAndAccents(a, b) {
1859 return compareIgnoreCaseAndAccents(a, b, (a, b) => a?.localeCompare(b));
1860}
1861
1862/**
1841 * @typedef {object} Select2Option The option object for select2 controls1863 * @typedef {object} Select2Option The option object for select2 controls
1842 * @property {string} id - The unique ID inside this select1864 * @property {string} id - The unique ID inside this select
1843 * @property {string} text - The text for this option1865 * @property {string} text - The text for this option
@@ -2231,8 +2253,8 @@ export function findPersona({ name = null, allowAvatar = true, insensitive = tru
2231 // Search for matching personas by name2253 // Search for matching personas by name
2232 const matchingPersonas = personas.filter(a => matches(a));2254 const matchingPersonas = personas.filter(a => matches(a));
2233 if (matchingPersonas.length > 1) {2255 if (matchingPersonas.length > 1) {
2234 if (!quiet) toastr.warning('Multiple personas found for given conditions.');2256 if (!quiet) toastr.warning(t`Multiple personas found for given conditions.`);
2235 else console.warn('Multiple personas found for given conditions. Returning the first match.');2257 else console.warn(t`Multiple personas found for given conditions. Returning the first match.`);
2236 }2258 }
22372259
2238 return matchingPersonas[0] || null;2260 return matchingPersonas[0] || null;
@@ -2270,8 +2292,8 @@ export function findChar({ name = null, allowAvatar = true, insensitive = true,
2270 if (preferCurrentChar) {2292 if (preferCurrentChar) {
2271 const preferredCharSearch = currentChars.filter(matches);2293 const preferredCharSearch = currentChars.filter(matches);
2272 if (preferredCharSearch.length > 1) {2294 if (preferredCharSearch.length > 1) {
2273 if (!quiet) toastr.warning('Multiple characters found for given conditions.');2295 if (!quiet) toastr.warning(t`Multiple characters found for given conditions.`);
2274 else console.warn('Multiple characters found for given conditions. Returning the first match.');2296 else console.warn(t`Multiple characters found for given conditions. Returning the first match.`);
2275 }2297 }
2276 if (preferredCharSearch.length) {2298 if (preferredCharSearch.length) {
2277 return preferredCharSearch[0];2299 return preferredCharSearch[0];