MCPcopy
hub / github.com/simstudioai/sim / getUniqueBlockName

Function getUniqueBlockName

apps/sim/stores/workflows/utils.ts:62–97  ·  view source on GitHub ↗
(baseName: string, existingBlocks: Record<string, any>)

Source from the content-addressed store, hash-verified

60 * @returns A unique block name with an appropriate number suffix
61 */
62export function getUniqueBlockName(baseName: string, existingBlocks: Record<string, any>): string {
63 // Special case: Start blocks should always be named "Start" without numbers
64 // This applies to both "Start" and "Starter" base names
65 const normalizedBaseName = normalizeName(baseName)
66 if (normalizedBaseName === 'start' || normalizedBaseName === 'starter') {
67 return 'Start'
68 }
69
70 if (normalizedBaseName === 'response') {
71 return 'Response'
72 }
73
74 const baseNameMatch = baseName.match(/^(.*?)(\s+\d+)?$/)
75 const namePrefix = baseNameMatch ? baseNameMatch[1].trim() : baseName
76
77 const normalizedBase = normalizeName(namePrefix)
78
79 const existingNumbers = Object.values(existingBlocks)
80 .filter((block) => {
81 const blockNameMatch = block.name?.match(/^(.*?)(\s+\d+)?$/)
82 const blockPrefix = blockNameMatch ? blockNameMatch[1].trim() : block.name
83 return blockPrefix && normalizeName(blockPrefix) === normalizedBase
84 })
85 .map((block) => {
86 const match = block.name?.match(/(\d+)$/)
87 return match ? Number.parseInt(match[1], 10) : 0
88 })
89
90 const maxNumber = existingNumbers.length > 0 ? Math.max(...existingNumbers) : 0
91
92 if (maxNumber === 0 && existingNumbers.length === 0) {
93 return `${namePrefix} 1`
94 }
95
96 return `${namePrefix} ${maxNumber + 1}`
97}
98
99export interface PrepareBlockStateOptions {
100 id: string

Callers 4

utils.test.tsFile · 0.90
store.tsFile · 0.90
workflow.tsxFile · 0.90

Calls 1

normalizeNameFunction · 0.90

Tested by

no test coverage detected