-
Notifications
You must be signed in to change notification settings - Fork 1
Crisp branch #8
base: master
Are you sure you want to change the base?
Crisp branch #8
Conversation
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.
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;
})
names: [ | ||
"tomorrow" | ||
] | ||
}, | ||
]; | ||
|
||
module.exports = {MealTimes, Dates}; | ||
module.exports = {mealTimes, datesLog}; |
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.
- It's important to be consistent
module.exports = {mealTimes, datesLog}; | |
module.exports = { mealTimes, datesLog }; |
//TODO Fill this in | ||
return false; | ||
}; | ||
module.exports = { stopWordList } |
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.
module.exports = { stopWordList } | |
module.exports = { stopWordList }; |
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you can break these into controllers. So it's easier to maintain in the future
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove unnecessary comments
}); | ||
|
||
module.exports = router; | ||
// end line |
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.
This is not needed, we don't need a comment to specify EOL. A new line is enough
const regex = /[`~!@#$%^&*()_|+\-=?;:,'.<>\{\}\[\]\\\/]/gi; //special characters. | ||
string = string.replace(regex , "").trim().split(" "); // filter out special characters and spaces. |
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.
- 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- We should not name a function parameter
string
- string is usually a type in other languages, we should use
words
or 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let
is rarely recommended because we want to avoid mutability, if you believelowerCaseWord
won't be reassigned then we should useconst
- Can we just do
string[i].toLowerCase()
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mealTimes
anddatesLog
don't share the same length- If
j = mealTimes.length - 1
, thendatesLog[j]
will be out of bound
No description provided.