(baseName: string, existingBlocks: Record<string, any>)
| 60 | * @returns A unique block name with an appropriate number suffix |
| 61 | */ |
| 62 | export 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 | |
| 99 | export interface PrepareBlockStateOptions { |
| 100 | id: string |
no test coverage detected