generated from the-collab-lab/smart-shopping-list
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #34 from the-collab-lab/ma-fz/duplicate-or-emptyitem
Issue # 10 Prevent user from adding duplicate or empty item to the list
- Loading branch information
Showing
4 changed files
with
56 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,41 @@ | ||
// makes sure the string passed into the function isn't an empty string | ||
export function validateTrimmedString(input: string) { | ||
const trimmedInput = input.trim(); | ||
import { ListItem } from "../api"; | ||
|
||
// Validates the item name input for a shopping list | ||
export function validateItemName( | ||
input: string, | ||
existingItems: ListItem[], | ||
): string | null { | ||
const trimmedInput = input.trim(); //removes leading and trailing whitespaces | ||
|
||
// Condition 1: Check if the input is empty | ||
if (trimmedInput.length === 0) { | ||
return null; | ||
return "Item cannot be empty"; | ||
} | ||
|
||
//Remove punctuation marks and normalize input | ||
const punctuationRegex = /[^\p{L}]/gu; | ||
|
||
const normalizedInputName = trimmedInput | ||
.replace(punctuationRegex, "") | ||
.toLowerCase(); | ||
|
||
//Create a list of normalized existing item names | ||
const normalizedExistingItemNames = existingItems.map((existingItem) => { | ||
return existingItem.name.replace(punctuationRegex, "").toLowerCase(); | ||
}); | ||
|
||
// Condition 2: Check if the normalized input matches any existing item | ||
const isDuplicateItem = (normalizedInputName: string): boolean => { | ||
return normalizedExistingItemNames.some( | ||
(item) => item === normalizedInputName, | ||
); | ||
}; | ||
|
||
//return error if the item already exists | ||
if (isDuplicateItem(normalizedInputName)) { | ||
return ` ${normalizedInputName} already exists in the list`; | ||
} | ||
|
||
return trimmedInput; | ||
// Return null if no errors are found (input is valid) | ||
return null; | ||
} |
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