-
-
Notifications
You must be signed in to change notification settings - Fork 445
LONDON-10/BAKI TUNCER/NODE-2 #288
base: master
Are you sure you want to change the base?
Conversation
batuncer
commented
Jul 23, 2023
- Your Name: BAKI
- Your City: LONDON
- Your Slack Name:BAKI
server.js
Outdated
app.post("/messages", (request, response) => { | ||
let newMessage = request.body; | ||
if ( | ||
newMessage.id === "" || |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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