2
0

🐛 (editor) Fix duplicate group unique key parsing

Closes #1148
This commit is contained in:
Baptiste Arnaud
2024-01-12 14:59:28 +01:00
parent e95e31e22c
commit 1c4c058e01
2 changed files with 23 additions and 17 deletions

View File

@ -1,8 +1,16 @@
export const parseUniqueKey = (
key: string,
existingKeys: string[],
count = 0
): string => {
if (!existingKeys.includes(key)) return key
return parseUniqueKey(`${key} (${count + 1})`, existingKeys, count + 1)
export const parseUniqueKey = (key: string, existingKeys: string[]): string => {
const keyMatcher = /^(.*?)\s*\(\d+\)$/
const parsedKey = keyMatcher.test(key) ? key.match(keyMatcher)![1] : key
const sameKeyCount = existingKeys.reduce((acc, existingKey) => {
if (
(keyMatcher.test(existingKey) &&
existingKey.match(keyMatcher)![1] === parsedKey) ||
parsedKey === existingKey
) {
return acc + 1
}
return acc
}, 0)
if (sameKeyCount === 0) return parsedKey
return `${parsedKey} (${sameKeyCount})`
}