Lodash methods and helper functions#149
Lodash methods and helper functions#149alexanderroidl wants to merge 6 commits intoorangecoding:masterfrom
Conversation
lib/api/routes/userRoute.js
Outdated
| return; | ||
| } | ||
| if (nullOrEmpty(username) || nullOrEmpty(password) || nullOrEmpty(password2)) { | ||
| if (!username || !password || !password2) { |
There was a problem hiding this comment.
See the previous pr. your check is not the same as mine.
let a = 0;
nullOrEmpty(a); //false
!a; //true
There was a problem hiding this comment.
in my case I want to explicitly check for null or empty
There was a problem hiding this comment.
Sounds good, I changed it back to the same logic but slimer:
const nullOrEmpty = (str) => str == null || str.length === 0;is the same as
const nullOrEmpty = (str) => Boolean(str?.length);as the if-block will convert everything inside it's brackets to a boolean value for checking, we can inline it:
if (!username?.length || !password?.length || !password2?.length) {Let me know if that works for you.
There was a problem hiding this comment.
But that is not true... (Or I'm missing something here):
Your version: const nullOrEmpty = (str) => Boolean(str?.length);
returns false on empty string.
const nullOrEmpty = (str) => Boolean(str?.length);
console.log(nullOrEmpty(''); //false, but should be true
There was a problem hiding this comment.
I typed it the wrong way, I meant
const nullOrEmpty = (str) => !Boolean(str?.length);
as an if-block implicitly converts statements inside it to a boolean for checking it, we can check for !str?.length directly instead of implementing and calling isNullOrEmpty(str)
| function duringWorkingHoursOrNotSet(config, now) { | ||
| const {workingHours} = config; | ||
| if (workingHours == null || nullOrEmpty(workingHours.from) || nullOrEmpty(workingHours.to)) { | ||
| if (!workingHours?.from || !workingHours?.to) { |
There was a problem hiding this comment.
I'd consider checking for null or empty by doing this truthy/falsy check relatively unsafe for reason mentioned above. WDYT?
There was a problem hiding this comment.
I think you're making a good point generally!
In this case I think we're still good though as the check will only be true when workingHours
- is an object (=neither null, nor undefined)
- has a
fromproperty - the content of that property is truthy
Which would actually make the check more complex and safer here.
> console.log(undefined?.test)
undefined
> console.log(Boolean(undefined?.test))
false
> console.log(""?.test)
undefined
> console.log(Boolean(""?.test))
false
> console.log(''?.test23abc)
undefined
> console.log(Boolean(''?.test23abc))
false
orangecoding
left a comment
There was a problem hiding this comment.
I added 1 comment. I think your "nullOrEmpty" check does not work as intended. Also there are now a couple of conflicts. Would you mind resolving them? :)
orangecoding
left a comment
There was a problem hiding this comment.
This pr results in Test failures. Tbh, I'm loving what you have contributed so far, but I'm not convinced about this one...
|
@alexanderroidl I'd close this pr as I don't see any activity. Thanks a lot tho |
This MR aims to refactor by:
nullOrEmptyhelper functions to check for truthy or falsy values natively.Lodashmethods as it's a dependency of ours anyways.