-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor utils into stand-alone functions #292
Conversation
@@ -0,0 +1,23 @@ | |||
import moment from 'moment'; | |||
|
|||
export function isSameDay(currentMessage = {}, diffMessage = {}) { |
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.
You can take the opportunity and also refactor this using the isSame
moment's function.
Something like
return moment(diffMessage.createdAt).isSame(currentMessage.createdAt)
} | ||
|
||
export function isSameUser(currentMessage = {}, diffMessage = {}) { | ||
if (diffMessage.user && currentMessage.user) { |
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.
Here one if
statement could be avoided, although the line length would be greater.
if (diffMessage.user && currentMessage.user && diffMessage.user._id === currentMessage.user._id) {
return true;
}
@scarmuega Looks solid, @FaridSafi what do you think? |
@dgdavid thanks for the feedback, I agree with the proposed optimizations, but my approach was to avoid any extra modification to original source code, to minimize the impact of the PR. With that being said, if @kfiroo and @FaridSafi agree, I can apply some adjustments within the utils.js file before closing the PR. |
I supposed that, but as you said if they are agree you could to apply more changes that improve both, legibility and performance of code. BTW, nice job! |
@scarmuega Nice job indeed! |
} | ||
return false; | ||
|
||
return currentCreatedAt.startOf('day').isSame(diffCreatedAt.startOf('day')); |
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.
According with isSame
documentation, you can limit the granularity directly:
return currentCreatedAt.isSame(diffCreatedAt, 'day')
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.
didn't know about the granularity param of the isSame function. I'll test it and push it.
return true; | ||
} | ||
|
||
if (diffMessage.user && currentMessage.user && diffMessage.user._id === currentMessage.user._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.
It looks good.
But I only to want share a thought about this: we can even to do something like
return diffMessage.user && currentMessage.user && diffMessage.user._id === currentMessage.user._id
The risk is that it could return an undefined value - valid for this case - and, in my opinion, is less clear.
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.
You can always do:
return !!(diffMessage.user && currentMessage.user && diffMessage.user._id === currentMessage.user._id)
@scarmuega I think I found another issue with this PR, the renderTime(props) {
if (props.isSameUser(props.currentMessage, props.nextMessage)) {
// Do something
}
} Any idea on how to go about this? |
@kfiroo my idea is that implementors may use these functions by importing them directly, like this:
Then, they can use them in any renderXXX like this:
|
@scarmuega, @kfiroo perhaps this information deserves either a section in the README or an entry in the Wiki if finally the changes are accepted. Or even both, a warning in the README file and a more detailed example(s) in the Wiki. |
@scarmuega @dgdavid Hey guys, The only thing left open here is the backward compatibility issue. renderTime(props) {
if (props.isSameUser(props.currentMessage, props.nextMessage)) {
return (
<Time
{...props}
textStyle={someCustomTextStyles}
/>
);
}
return (
// SOMETHING ELSE...
);
}, So I think we should pass these methods as props to these override methods at least. |
@kfiroo It is a difficult decision, and honestly I don't know what I'd do. But, Did you think about a not backward-compatible update? I believe that RNGC deserves a major update with all PR that will be accepted and maybe is moment to break the backward compatibility. Why not take the step from Just is an idea. It not answer your question, I know, but just now I see no point to keep these props bloating the component. |
@dgdavid I will work on a new version with breaking changes, it's on my todo list :) I still feel like giving people a heads up, like maybe a warning about deprecation before completely breaking something is fair. |
Nice! I very happy to read that. I was also looking for some free time to review all issues and PR and make a summary for you and @FaridSafi. But free time also flies 😄 |
@dgdavid I know what you mean! No worries we will get this going soon! :) |
@kfiroo @dgdavid sounds good. Just to double check:
Let me know if this is what you had in mind and I'll push the new commit. |
@scarmuega finally did you add a utils file? |
@benjaminb10 yes, if you are pulling from this PR, you should be able to use |
@scarmuega I prefere wait for the merge. |
@scarmuega @dgdavid Good job guys :) |
What was done: