This repository was archived by the owner on Aug 17, 2024. It is now read-only.
Open
Conversation
batuncer
commented
Jul 23, 2023
- Your Name: BAKI
- Your City: LONDON
- Your Slack Name:BAKI
kitko112
reviewed
Jul 28, 2023
server.js
Outdated
| app.post("/messages", (request, response) => { | ||
| let newMessage = request.body; | ||
| if ( | ||
| newMessage.id === "" || |
There was a problem hiding this comment.
Are you trying to validate the id, from and text ? if they don't exist it will be a bad request right?
your logic check only if the parameter is an empty string, but what if the caller gives an undefined value?
server.js
Outdated
| // Helper function to get messages containing the search word | ||
| const getMessagesFromSearch = (messages, word) => { | ||
| return messages.filter((message) => | ||
| message.text.toLowerCase().includes(word.toLowerCase()) |
There was a problem hiding this comment.
There is another way to search in case insensitive with regular expression. with it you don't need to lower the message and the word.
// Value to search (can be obtained from user input or any other source)
const searchValue = 'a'; // Change this value as needed
// Function to search using regular expression
function searchWithRegex(array, searchValue) {
const regex = new RegExp(searchValue, 'i'); // 'i' for case-insensitive search
return array.filter(item => regex.test(item));
}
// Perform the search and store the result in a new array
const searchResult = searchWithRegex(arrayToSearch, searchValue);
|
|
||
| // Helper function to get the 10 latest messages | ||
| const getLatestMessages = (messages) => { | ||
| return messages.slice(Math.max(messages.length - 10, 0)); |
There was a problem hiding this comment.
Nice use of slice which return an shallow copy.
|
|
||
| // Helper function to get a message by ID | ||
| const getMessageByID = (messages, id) => { | ||
| return messages.filter((message) => message.id == id); |
There was a problem hiding this comment.
FYI. Another way to store the message is to use Map(), so you can easily get item by id.
But it is not for search.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
This file contains hidden or 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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.