Skip to content

Commit

Permalink
Implement format support.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredhanson committed Mar 17, 2012
1 parent 2af8458 commit 0ad2c70
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/flash.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
var format = require('util').format;


module.exports = function flash(options) {
options = options || {};
var safe = (options.unsafe === undefined) ? true : !options.unsafe;
Expand All @@ -13,8 +16,10 @@ function _flash(type, msg) {
if (this.session === undefined) throw Error('req.flash() requires sessions');
var msgs = this.session.flash = this.session.flash || {};
if (type && msg) {
// TODO: Format messages

if (arguments.length > 2) {
var args = Array.prototype.slice.call(arguments, 1);
msg = format.apply(undefined, args);
}
return (msgs[type] = msgs[type] || []).push(msg);
} else if (type) {
var arr = msgs[type];
Expand Down
9 changes: 9 additions & 0 deletions test/flash-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ vows.describe('flash').addBatch({
assert.lengthOf(msgs['notice'], 1);
assert.lengthOf(Object.keys(req.session.flash), 0);
},
'should format messages' : function(err, req, res) {
req.flash('info', 'Hello %s', 'Jared');
var msg = req.flash('info')[0];
assert.equal(msg, 'Hello Jared')

req.flash('info', 'Hello %s %s', 'Jared', 'Hanson');
var msg = req.flash('info')[0];
assert.equal(msg, 'Hello Jared Hanson')
},
'should return empty array for flash type with no messages' : function(err, req, res) {
var msgs = req.flash('what');
assert.lengthOf(msgs, 0);
Expand Down

0 comments on commit 0ad2c70

Please sign in to comment.