Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. /**
  2. * A debugging function that prints its arguments
  3. * @returns {String} an identifier that shows the name of this function
  4. */
  5. function createConnFunc(name) {
  6. /* eslint-disable no-console */
  7. return function connFunnFunc(...args) {
  8. console.log(`${name}(${args.join(',')})`);
  9. return `conn_${name}`;
  10. };
  11. }
  12.  
  13. const send_resp = createConnFunc('send_resp');
  14. const put_resp_header = createConnFunc('put_resp_header');
  15. const put_session = createConnFunc('put_session');
  16. const put_resp_content_type = createConnFunc('put_resp_content_type');
  17. const current_user = 'current_user_BOB';
  18. const user = 'user_ALICE';
  19. const location = 'location_OSLO';
  20.  
  21. console.log('\nALT1:');
  22. const {bind, flow} = require('lodash');
  23. const _ = bind.placeholder;
  24. flow(
  25. bind(put_session, null, _, current_user, user),
  26. bind(put_resp_header, null, _, location, '/'),
  27. bind(put_resp_content_type, null, _, 'text/html'),
  28. bind(send_resp, null, _, 302, 'You are being redirected')
  29. )('conn');
  30.  
  31. //////
  32. // Trying to make it more compact ...
  33. //////
  34.  
  35. console.log('\nALT2:');
  36.  
  37. // bind all but first argument - what should I call this?
  38. // bindAllButFirstArgument is a tad bit long
  39. const $$ = (...args) => bind(args[0], null, _, args.slice(1));
  40.  
  41. flow(
  42. $$(put_session, current_user, user),
  43. $$(put_resp_header, location, '/'),
  44. $$(put_resp_content_type, 'text/html'),
  45. $$(send_resp, 302, 'You are being redirected')
  46. )('conn');
  47.  
  48. //////
  49. // Third attempt at wrapping it up - renaming
  50. //////
  51.  
  52. console.log('\nALT3:');
  53.  
  54. const pipe = (...args) => flow(...args);
  55. pipe.prepare = fn => (...args) => bind(fn, null, _, args);
  56.  
  57. pipe(
  58. pipe.prepare(put_session)(current_user, user),
  59. pipe.prepare(put_resp_header)(location, '/'),
  60. pipe.prepare(put_resp_content_type)('text/html'),
  61. pipe.prepare(send_resp)(302, 'You are being redirected')
  62. )('conn');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement