Uh oh!
There was an error while loading. Please reload this page.
LONDON-10/BAKI TUNCER/NODE-2 - #288
Conversation
batuncer
commented
Jul 23, 2023
- Your Name: BAKI
- Your City: LONDON
- Your Slack Name:BAKI
| 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?
| // 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