Skip to content

Instantly share code, notes, and snippets.

@pyreta
Created July 31, 2017 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pyreta/547f3de4b2e65a4f63b6477d4c30297a to your computer and use it in GitHub Desktop.
Save pyreta/547f3de4b2e65a4f63b6477d4c30297a to your computer and use it in GitHub Desktop.
Using map for non array objects
/*
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