Merge branch 'flash-formatting'
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 · parents bb18f87, 192144f · Browse files · 3 files·+44 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+44 added−1 removed
- tests:+13 added−0 removed
- other:+31 added−1 removed
Provenance
- Commit
fa3adc7- Tree
66ee6e3- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent 1
bb18f87- Parent 2
192144f
3 files changed+44 added−1 removed
modified:lib/express/index.js+1 added-1 removed
42 unmodified lines4344454647474842 unmodified lines * View extensions. */
require('./view');require('./request');require('./response');require('./request');modified:lib/express/request.js+30 added
12 unmodified lines13 utils = require('./utils'),14 mime = require('connect/utils').mime;15 16 /**17+ * Default flash formatters.18+ *19+ * @type Object20+ */21+22+var flashFormatters = exports.flashFormatters = {23+ s: function(val){24+ return String(val);25+ }26+};27+28+/**29 * Return request header or optional default.30 *31 * Examples:32 *120 unmodified lines153 *154 * req.flash();155 * // => { error: ['email delivery failed'], info: [] }156 *157+ * Formatting:158+ *159+ * Flash notifications also support arbitrary formatting support.160+ * For example you may pass variable arguments to `req.flash()`161+ * and use the %s specifier to be replaced by the associated argument:162+ *163+ * req.flash('info', 'email has been sent to %s.', userName);164+ *165+ * To add custom formatters use the `exports.flashFormatters` object.166+ *167 * @param {String} type168 * @param {String} msg169 * @return {Array|Object|Number}170 * @api public1 unmodified line172 173 http.IncomingMessage.prototype.flash = function(type, msg){174 var msgs = this.session.flash = this.session.flash || {};175 if (type && msg) {176+ var i = 2,177+ args = arguments,178+ formatters = this.app.flashFormatters || {};179+ formatters.__proto__ = flashFormatters;180 msg = utils.miniMarkdown(utils.htmlEscape(msg));181+ msg = msg.replace(/%([a-zA-Z])/g, function(_, format){182+ var formatter = formatters[format];183+ if (formatter) return formatter(args[i++]);184+ });185 return (msgs[type] = msgs[type] || []).push(msg);186 } else if (type) {187 var arr = msgs[type];188 delete msgs[type];22 unmodified linesmodified:test/request.test.js+13 added
115 unmodified linesExpand all1161171181191201211221231241251261271281299 unmodified lines1391401411421431441451461471481491501511521534 unmodified lines115 unmodified linesExpand all connect.cookieDecoder(), connect.session({ store: memoryStore }) );
app.flashFormatters = { u: function(val){ return String(val).toUpperCase(); } };
app.get('/', function(req, res){ assert.eql([], req.flash('info')); assert.eql([], req.flash('error')); assert.eql({}, req.flash());9 unmodified lines req.flash('info', 'Email _sent_.'); req.flash('info', '<script>'); assert.eql(['Email <em>sent</em>.', '<script>'], req.flash('info')); req.flash('info', 'Welcome _%s_ to %s', 'TJ', 'something'); assert.eql(['Welcome <em>TJ</em> to something'], req.flash('info'));
req.flash('info', 'Foo %u', 'bar'); assert.eql(['Foo BAR'], req.flash('info'));
res.send('ok'); }); assert.response(app,4 unmodified lines