-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(util): extract color name deduplication into separate function
- Loading branch information
Showing
2 changed files
with
27 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { normalizeName } from './normalize-name'; | ||
|
||
/** | ||
* Deduplicate a name by adding a number to the end if it already exists. | ||
*/ | ||
export function deduplicateName(name: string, existingNames: Array<string>): string { | ||
const normalizedNames = existingNames.map((n) => normalizeName(n)); | ||
|
||
// Check if name already exists | ||
while (normalizedNames.includes(normalizeName(name))) { | ||
// Check if the name already has a number | ||
const lastNumber = name.match(/\d+$/); | ||
if (lastNumber) { | ||
// Increment the number | ||
const number = parseInt(lastNumber[0], 10); | ||
name = name.replace(/\d+$/, '') + (number + 1); | ||
} else { | ||
// Add a number to the color name | ||
name += ' 2'; | ||
} | ||
} | ||
|
||
// Return the deduplicated name | ||
return name; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters