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

@ -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];