-
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.
Merge pull request #78 from pawcoding/fix/duplicate-color-names
Prevent duplicate color names
- Loading branch information
Showing
10 changed files
with
169 additions
and
4 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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* Normalize a string by removing whitespace and converting to lowercase. | ||
*/ | ||
export function normalizeName(value: string): string { | ||
return value.trim().replace(/\s+/g, '').toLowerCase(); | ||
} |
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,24 @@ | ||
import { ValidatorFn } from '@angular/forms'; | ||
import { normalizeName } from '../../shared/utils/normalize-name'; | ||
|
||
/** | ||
* Validator that checks if the value is already in the array. | ||
*/ | ||
export function duplicateValidator(values: Array<string>): ValidatorFn { | ||
// Normalize the values | ||
const normalizedValues = values.map((value) => normalizeName(value)); | ||
|
||
return (control) => { | ||
const normalizedValue = normalizeName(control.value); | ||
|
||
// Check if the value is already in the array | ||
const duplicate = normalizedValues.findIndex((value) => value === normalizedValue); | ||
if (duplicate === -1) { | ||
// No duplicate found | ||
return null; | ||
} | ||
|
||
// Return duplicate | ||
return { duplicate: { value: values.at(duplicate) } }; | ||
}; | ||
} |
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
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
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