Piping with binding

node v10.24.1
version: 1.0.0
endpointsharetweet
/** * 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');
Loading…

no comments

    sign in to comment