Move string utils to shared module

827fce4542c646a401575b2c0da816d5d661af9d

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

2 files changed, +11 -4Showing whitespace changes
public/scripts/extensions/connection-manager/index.js+2 -4
@@ -7,7 +7,7 @@ import { ARGUMENT_TYPE, SlashCommandArgument } from '../../slash-commands/SlashC
77import { enumIcons } from '../../slash-commands/SlashCommandCommonEnumsProvider.js';
88import { enumTypes, SlashCommandEnumValue } from '../../slash-commands/SlashCommandEnumValue.js';
99import { SlashCommandParser } from '../../slash-commands/SlashCommandParser.js';
1010import { collapseSpaces, getUniqueName, uuidv4 } from '../../utils.js';
1111
1212const MODULE_NAME = 'connection-manager';
1313const NONE = '<None>';
@@ -71,8 +71,6 @@ const profilesProvider = () => [
7171 */
7272
7373const escapeArgument = (a) => a.replace(/"/g, '\\"').replace(/\|/g, '\\|');
74-const collapseSpaces = (s) => s.replace(/\s+/g, ' ').trim();
75-const makeUnique = (s, f, i) => { if (!f(s)) { return s; } else { while (f(`${s} (${i})`)) { i++; } return `${s} (${i})`; } };
7674
7775/**
7876 * Finds the best match for the search value.
@@ -150,7 +148,7 @@ async function createConnectionProfile(forceName = null) {
150148 const profileForDisplay = makeFancyProfile(profile);
151149 const template = await renderExtensionTemplateAsync(MODULE_NAME, 'profile', { profile: profileForDisplay });
152150 const checkName = (n) => extension_settings.connectionManager.profiles.some(p => p.name === n);
153151 const suggestedName = makeUniquegetUniqueName(collapseSpaces(`${profile.api ?? ''} ${profile.model ?? ''} - ${profile.preset ?? ''}`), checkName, 1);
154152 const name = forceName ?? await callGenericPopup(template, POPUP_TYPE.INPUT, suggestedName, { rows: 2 });
155153
156154 if (!name) {
public/scripts/utils.js+9 -0
@@ -1436,6 +1436,15 @@ export function uuidv4() {
14361436 });
14371437}
14381438
1439+/**
1440+ * Collapses multiple spaces in a strings into one.
1441+ * @param {string} s String to process
1442+ * @returns {string} String with collapsed spaces
1443+ */
1444+export function collapseSpaces(s) {
1445+ return s.replace(/\s+/g, ' ').trim();
1446+}
1447+
14391448function postProcessText(text, collapse = true) {
14401449 // Remove carriage returns
14411450 text = text.replace(/\r/g, '');