Move string utils to shared module
| @@ -7,7 +7,7 @@ import { ARGUMENT_TYPE, SlashCommandArgument } from '../../slash-commands/SlashC | |||
| 7 | import { enumIcons } from '../../slash-commands/SlashCommandCommonEnumsProvider.js'; | 7 | import { enumIcons } from '../../slash-commands/SlashCommandCommonEnumsProvider.js'; |
| 8 | import { enumTypes, SlashCommandEnumValue } from '../../slash-commands/SlashCommandEnumValue.js'; | 8 | import { enumTypes, SlashCommandEnumValue } from '../../slash-commands/SlashCommandEnumValue.js'; |
| 9 | import { SlashCommandParser } from '../../slash-commands/SlashCommandParser.js'; | 9 | import { SlashCommandParser } from '../../slash-commands/SlashCommandParser.js'; |
| 10 | import { uuidv4 } from '../../utils.js'; | 10 | import { collapseSpaces, getUniqueName, uuidv4 } from '../../utils.js'; |
| 11 | 11 | ||
| 12 | const MODULE_NAME = 'connection-manager'; | 12 | const MODULE_NAME = 'connection-manager'; |
| 13 | const NONE = '<None>'; | 13 | const NONE = '<None>'; |
| @@ -71,8 +71,6 @@ const profilesProvider = () => [ | |||
| 71 | */ | 71 | */ |
| 72 | 72 | ||
| 73 | const escapeArgument = (a) => a.replace(/"/g, '\\"').replace(/\|/g, '\\|'); | 73 | const 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})`; } }; | ||
| 76 | 74 | ||
| 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 }); |
| 155 | 153 | ||
| 156 | if (!name) { | 154 | if (!name) { |
| @@ -1436,6 +1436,15 @@ export function uuidv4() { | |||
| 1436 | }); | 1436 | }); |
| 1437 | } | 1437 | } |
| 1438 | 1438 | ||
| 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 | |||
| 1439 | function postProcessText(text, collapse = true) { | 1448 | function postProcessText(text, collapse = true) { |
| 1440 | // Remove carriage returns | 1449 | // Remove carriage returns |
| 1441 | text = text.replace(/\r/g, ''); | 1450 | text = text.replace(/\r/g, ''); |