Created
July 31, 2017 13:54
-
-
Save pyreta/547f3de4b2e65a4f63b6477d4c30297a to your computer and use it in GitHub Desktop.
Using map for non array objects
This file contains 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
/* | |
This is a refactor from my previous job that I really like. Before the refactor, I had declared at least 3 | |
different variables that were only necessary within the immediate function scope. Then I tried nesting functions | |
passing arguments with arguments into arguments etc... | |
Using .map for objects other than arrays proves to be very useful here. I no longer have to declare unneeded variables, | |
yet I still maintain readibility through a functional approach. All that was needed was to wrap the object in brackets, | |
and then select the first index in that array at the end. | |
I have found this approach to be consistently useful. | |
*/ | |
const queryStringWithoutPageParams = (router, resource, params) => | |
[(params || queryParams(router, resource)).query] | |
.map(paramsWithPage => fromJS(paramsWithPage).set('page', {}).toJS()) | |
.map(paramsWithoutPage => queryStringBuilder(paramsWithoutPage)) | |
.map(queryStringWithoutPageParams => queryStringWithoutPageParams.slice(1))[0]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment