Skip to content

Instantly share code, notes, and snippets.

@fatso83
Created August 12, 2019 11:57
Show Gist options
  • Save fatso83/82eafe559818a89648f4bfb6c8174d18 to your computer and use it in GitHub Desktop.
Save fatso83/82eafe559818a89648f4bfb6c8174d18 to your computer and use it in GitHub Desktop.
/**
* A debugging function that prints its arguments
* @returns {String} an identifier that shows the name of this function
*/
function createConnFunc(name) {
/* eslint-disable no-console */
return function connFunnFunc(...args) {
console.log(`${name}(${args.join(',')})`);
return `conn_${name}`;
};
}
const send_resp = createConnFunc('send_resp');
const put_resp_header = createConnFunc('put_resp_header');
const put_session = createConnFunc('put_session');
const put_resp_content_type = createConnFunc('put_resp_content_type');
const current_user = 'current_user_BOB';
const user = 'user_ALICE';
const location = 'location_OSLO';
console.log('\nALT1:');
const {bind, flow} = require('lodash');
const _ = bind.placeholder;
flow(
bind(put_session, null, _, current_user, user),
bind(put_resp_header, null, _, location, '/'),
bind(put_resp_content_type, null, _, 'text/html'),
bind(send_resp, null, _, 302, 'You are being redirected')
)('conn');
//////
// Trying to make it more compact ...
//////
console.log('\nALT2:');
// bind all but first argument - what should I call this?
// bindAllButFirstArgument is a tad bit long
const $$ = (...args) => bind(args[0], null, _, args.slice(1));
flow(
$$(put_session, current_user, user),
$$(put_resp_header, location, '/'),
$$(put_resp_content_type, 'text/html'),
$$(send_resp, 302, 'You are being redirected')
)('conn');
//////
// Third attempt at wrapping it up - renaming
//////
console.log('\nALT3:');
const pipe = (...args) => flow(...args);
pipe.prepare = fn => (...args) => bind(fn, null, _, args);
pipe(
pipe.prepare(put_session)(current_user, user),
pipe.prepare(put_resp_header)(location, '/'),
pipe.prepare(put_resp_content_type)('text/html'),
pipe.prepare(send_resp)(302, 'You are being redirected')
)('conn');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment