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
7import { enumIcons } from '../../slash-commands/SlashCommandCommonEnumsProvider.js';7import { enumIcons } from '../../slash-commands/SlashCommandCommonEnumsProvider.js';
8import { enumTypes, SlashCommandEnumValue } from '../../slash-commands/SlashCommandEnumValue.js';8import { enumTypes, SlashCommandEnumValue } from '../../slash-commands/SlashCommandEnumValue.js';
9import { SlashCommandParser } from '../../slash-commands/SlashCommandParser.js';9import { SlashCommandParser } from '../../slash-commands/SlashCommandParser.js';
10import { uuidv4 } from '../../utils.js';10import { collapseSpaces, getUniqueName, uuidv4 } from '../../utils.js';
1111
12const MODULE_NAME = 'connection-manager';12const MODULE_NAME = 'connection-manager';
13const NONE = '<None>';13const NONE = '<None>';
@@ -71,8 +71,6 @@ const profilesProvider = () => [
71 */71 */
7272
73const escapeArgument = (a) => a.replace(/"/g, '\\"').replace(/\|/g, '\\|');73const escapeArgument = (a) => a.replace(/"/g, '\\"').replace(/\|/g, '\\|');
74const collapseSpaces = (s) => s.replace(/\s+/g, ' ').trim();
75const makeUnique = (s, f, i) => { if (!f(s)) { return s; } else { while (f(`${s} (${i})`)) { i++; } return `${s} (${i})`; } };
7674
77/**75/**
78 * Finds the best match for the search value.76 * Finds the best match for the search value.
@@ -150,7 +148,7 @@ async function createConnectionProfile(forceName = null) {
150 const profileForDisplay = makeFancyProfile(profile);148 const profileForDisplay = makeFancyProfile(profile);
151 const template = await renderExtensionTemplateAsync(MODULE_NAME, 'profile', { profile: profileForDisplay });149 const template = await renderExtensionTemplateAsync(MODULE_NAME, 'profile', { profile: profileForDisplay });
152 const checkName = (n) => extension_settings.connectionManager.profiles.some(p => p.name === n);150 const checkName = (n) => extension_settings.connectionManager.profiles.some(p => p.name === n);
153 const suggestedName = makeUnique(collapseSpaces(`${profile.api ?? ''} ${profile.model ?? ''} - ${profile.preset ?? ''}`), checkName, 1);151 const suggestedName = getUniqueName(collapseSpaces(`${profile.api ?? ''} ${profile.model ?? ''} - ${profile.preset ?? ''}`), checkName);
154 const name = forceName ?? await callGenericPopup(template, POPUP_TYPE.INPUT, suggestedName, { rows: 2 });152 const name = forceName ?? await callGenericPopup(template, POPUP_TYPE.INPUT, suggestedName, { rows: 2 });
155153
156 if (!name) {154 if (!name) {
public/scripts/utils.js+9 -0
@@ -1436,6 +1436,15 @@ export function uuidv4() {
1436 });1436 });
1437}1437}
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 */
1444export function collapseSpaces(s) {
1445 return s.replace(/\s+/g, ' ').trim();
1446}
1447
1439function postProcessText(text, collapse = true) {1448function postProcessText(text, collapse = true) {
1440 // Remove carriage returns1449 // Remove carriage returns
1441 text = text.replace(/\r/g, '');1450 text = text.replace(/\r/g, '');