make `req.notify()` arity based
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 05b76a1 · Browse files · 2 files·+26 added−27 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+26 added−27 removed
- tests:+4 added−4 removed
- other:+22 added−23 removed
Provenance
- Commit
fd5d107- Tree
5605a0b- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
05b76a1
2 files changed+26 added−27 removed
modified:lib/request.js+22 added-23 removed
224 unmodified lines225 var sess = this.session;226 if (null == sess) throw Error('req.notify() requires sessions');227 var msgs = sess.notifications = sess.notifications || {};228 229+ switch (arguments.length) {230+ // flush all messages231+ case 0:232+ sess.notifications = {};233+ return msgs234+ // flush messages for a specific type235+ case 1:236+ var arr = msgs[type];237+ delete msgs[type];238+ return arr || [];229- // notification230- if (type && msg) {231- var i = 2232- , args = arguments233- , formatters = this.app.formatters || {};234- formatters.__proto__ = defaultFormatters;235- msg = utils.miniMarkdown(utils.escape(msg));236- msg = msg.replace(/%([a-zA-Z])/g, function(_, format){237- var formatter = formatters[format];238- if (formatter) return formatter(args[i++]);239- });240- return (msgs[type] = msgs[type] || []).push(msg);239+ // set notification message240+ default:241+ var i = 2242+ , args = arguments243+ , formatters = this.app.formatters || {};244+ formatters.__proto__ = defaultFormatters;245+ msg = utils.miniMarkdown(utils.escape(msg));246+ msg = msg.replace(/%([a-zA-Z])/g, function(_, format){247+ var formatter = formatters[format];248+ if (formatter) return formatter(args[i++]);249+ });250+ return (msgs[type] = msgs[type] || []).push(msg);251 }242-243- // flush messages for a specific type244- if (type) {245- var arr = msgs[type];246- delete msgs[type];247- return arr || [];248- }249-250- // flush all messages251- sess.notifications = {};252- return msgs;252 };253 254 /**255 * Check if the incoming request contains the "Content-Type" 76 unmodified linesmodified:test/request.test.js+4 added-4 removed
148 unmodified lines149 req.notify('info', 'one').should.equal(1);150 req.notify('info', 'two').should.equal(2);151 req.notify('info').should.eql(['one', 'two']);152 req.notify('info').should.eql([]);153-153+ 154 req.notify('info', 'one').should.equal(1);155 req.notify('info').should.eql(['one']);156-156+ 157 req.notify('info', 'Email _sent_.');158 req.notify('info', '<script>');159 req.notify('info').should.eql(['Email <em>sent</em>.', '<script>']);160 161 req.notify('info', 'Welcome _%s_ to %s', 'TJ', 'something');162 req.notify('info').should.eql(['Welcome <em>TJ</em> to something']);163-163+ 164 req.notify('info', 'TJ has %d pets', 5.12323);165 req.notify('info').should.eql(['TJ has 5 pets']);166-166+ 167 req.notify('error', 'Foo %u', 'bar');168 req.notify('error').should.eql(['Foo BAR']);169 170 res.send('ok');171 unmodified lines