MCPcopy Create free account
hub / github.com/experdot/pointer / generateUniqueName

Function generateUniqueName

src/renderer/src/services/pagesService.ts:11–32  ·  view source on GitHub ↗
(baseName: string, existingNames: string[])

Source from the content-addressed store, hash-verified

9 * e.g., "新对话" -> "新对话 (1)" -> "新对话 (2)"
10 */
11export function generateUniqueName(baseName: string, existingNames: string[]): string {
12 // Check if base name is available
13 if (!existingNames.includes(baseName)) {
14 return baseName
15 }
16
17 // Find all names matching pattern "baseName" or "baseName (n)"
18 const pattern = new RegExp(`^${escapeRegExp(baseName)}(?: \\((\\d+)\\))?$`)
19 let maxNumber = 0
20
21 for (const name of existingNames) {
22 const match = name.match(pattern)
23 if (match) {
24 const num = match[1] ? parseInt(match[1], 10) : 0
25 if (num > maxNumber) {
26 maxNumber = num
27 }
28 }
29 }
30
31 return `${baseName} (${maxNumber + 1})`
32}
33
34/**
35 * Escape special regex characters in a string

Callers 5

usePagesFunction · 0.90
createPageFunction · 0.85
movePageFunction · 0.85
moveFolderFunction · 0.85
createFolderFunction · 0.85

Calls 1

escapeRegExpFunction · 0.70

Tested by

no test coverage detected