Skip to content
Myr

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

2 files+26 added−27 removed

modified:lib/request.js+22 added-23 removed
224 unmodified lines
225 var sess = this.session;226 if (null == sess) throw Error('req.notify() requires sessions');227 var msgs = sess.notifications = sess.notifications || {};228229 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 }242243 // flush messages for a specific type244 if (type) {245 var arr = msgs[type];246 delete msgs[type];247 return arr || [];248 }249250 // flush all messages251 sess.notifications = {};252 return msgs;252};253254/**255 * Check if the incoming request contains the "Content-Type"
76 unmodified lines
modified:test/request.test.js+4 added-4 removed
148 unmodified lines
149 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([]);153153 154 req.notify('info', 'one').should.equal(1);155 req.notify('info').should.eql(['one']);156156 157 req.notify('info', 'Email _sent_.');158 req.notify('info', '<script>');159 req.notify('info').should.eql(['Email <em>sent</em>.', '&lt;script&gt;']);160 161 req.notify('info', 'Welcome _%s_ to %s', 'TJ', 'something');162 req.notify('info').should.eql(['Welcome <em>TJ</em> to something']);163163 164 req.notify('info', 'TJ has %d pets', 5.12323);165 req.notify('info').should.eql(['TJ has 5 pets']);166166 167 req.notify('error', 'Foo %u', 'bar');168 req.notify('error').should.eql(['Foo BAR']);169170 res.send('ok');
171 unmodified lines