This repository was archived by the owner on Oct 23, 2023. It is now read-only.
Crisp branch#8
Open
CrislpWW2 wants to merge 7 commits into
Open
Conversation
songrenzhao
suggested changes
Jun 14, 2023
There was a problem hiding this comment.
Congrats on your first PR.
A few things that you should consider
- You are writing a lot of imperative code, can we be a bit more functional?
- Look into usage of
Array.prototype.forEach(),Array.prototype.find(),Array.prototype.map()
- Look into usage of
- You are nesting too much logic in a single function, with numerous if and else, can we make the function cleaner and easier to read?
Consider doing something below
- Read each word
- Check if current word is
stopWords - Check if current word is
mealTime - Check if current word is
dates - Check if current word is unknown
const formattedWords = words.map(word => {
const loweredCaseWord = word.toLowerCase();
const isStopWord = stopWordList.includes(loweredCaseWord);
if (isStopWord) {
/// Your logic here ...
return { ... }
}
const mealTime = mealTimes.find(...); // This will return mealTIme or undefined
const datesLog = datesLogs.find(...); // This will return dateLog or undefined
const mealTimeOrDatesLog = mealTime ?? datesLog;
const response = !!mealTimeOrDatesLog ? { ... } : { ... };
return response;
})| ]; | ||
|
|
||
| module.exports = {MealTimes, Dates}; | ||
| module.exports = {mealTimes, datesLog}; |
There was a problem hiding this comment.
- It's important to be consistent
Suggested change
| module.exports = {mealTimes, datesLog}; | |
| module.exports = { mealTimes, datesLog }; |
| //TODO Fill this in | ||
| return false; | ||
| }; | ||
| module.exports = { stopWordList } |
There was a problem hiding this comment.
Suggested change
| module.exports = { stopWordList } | |
| module.exports = { stopWordList }; |
Comment on lines
+20
to
+24
| router.post('/parse', function (req, res) { | ||
| const { str } = req.body; | ||
| const parsed = parseFunction.parser(str); | ||
| res.json(parsed); | ||
| }); |
There was a problem hiding this comment.
Maybe you can break these into controllers. So it's easier to maintain in the future
Suggested change
| router.post('/parse', function (req, res) { | |
| const { str } = req.body; | |
| const parsed = parseFunction.parser(str); | |
| res.json(parsed); | |
| }); | |
| router.post('/parse', parse); |
| res.send("Parse It!"); | ||
| }); | ||
|
|
||
| //TODO Fulfill endpoint to list Meal Times |
There was a problem hiding this comment.
We can remove unnecessary comments
| }); | ||
|
|
||
| module.exports = router; | ||
| // end line |
There was a problem hiding this comment.
This is not needed, we don't need a comment to specify EOL. A new line is enough
Comment on lines
+6
to
+7
| const regex = /[`~!@#$%^&*()_|+\-=?;:,'.<>\{\}\[\]\\\/]/gi; //special characters. | ||
| string = string.replace(regex , "").trim().split(" "); // filter out special characters and spaces. |
There was a problem hiding this comment.
- This can be extracted into its middleware. We can do something like this
router.post('/parse', formatBody, parse);- Reason this is recommended is the concept of
separation of concerns. We want to make the function as simple as possible, these types of validations can be handled before it reaches to the handler
|
|
||
| class Parser { | ||
| //TODO Parse through and identify all StopWords & Identifiers | ||
| function parser(string) { |
There was a problem hiding this comment.
- We should not name a function parameter
string - string is usually a type in other languages, we should use
wordsor other meaningful plurals
| string = string.replace(regex , "").trim().split(" "); // filter out special characters and spaces. | ||
|
|
||
| for (let i = 0; i < string.length; i++) { | ||
| let lowerCaseWord = string[i][0].toLowerCase() + string[i].slice(1); |
There was a problem hiding this comment.
letis rarely recommended because we want to avoid mutability, if you believelowerCaseWordwon't be reassigned then we should useconst- Can we just do
string[i].toLowerCase()
Comment on lines
+12
to
+26
| for (let j = 0; j < mealTimes.length; j++) { // nested loop to check if the word is in mealTimes array or datesLog array. | ||
| if (mealTimes[j].names.includes(lowerCaseWord)) { | ||
| objStringArray.push({ | ||
| matchedWord: `${ string[i] }`, | ||
| type: `meal type`, | ||
| value: `${ mealTimes[j].value} `, | ||
| }); | ||
| } else if ( datesLog[j] && datesLog[j].names.includes(lowerCaseWord)) { | ||
| objStringArray.push({ | ||
| matchedWord: `${ string[i] }`, | ||
| type: `date`, | ||
| value: `${ datesLog[j].value }`, | ||
| }); | ||
| } | ||
| } |
There was a problem hiding this comment.
- Seems like you are doing a look up here
- Great to see some solid understanding of iterative programming, but maybe we can leverage some built in function like
Array.prototype.find()?
| type: `meal type`, | ||
| value: `${ mealTimes[j].value} `, | ||
| }); | ||
| } else if ( datesLog[j] && datesLog[j].names.includes(lowerCaseWord)) { |
There was a problem hiding this comment.
mealTimesanddatesLogdon't share the same length- If
j = mealTimes.length - 1, thendatesLog[j]will be out of bound
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.
No description provided.