diff --git a/frontend_tests/node_tests/topic_generator.js b/frontend_tests/node_tests/topic_generator.js index 2814822f9e..4a8b26350e 100644 --- a/frontend_tests/node_tests/topic_generator.js +++ b/frontend_tests/node_tests/topic_generator.js @@ -61,6 +61,13 @@ function is_odd(i) { return i % 2 === 1; } assert.equal(gen.next(), undefined); assert.equal(gen.next(), undefined); + ints = tg.list_generator([10, 20, 30]); + + function mult10(x) { return x * 10; } + + gen = tg.map(ints, mult10); + assert.equal(gen.next(), 100); + assert.equal(gen.next(), 200); }()); (function test_fchain() { diff --git a/static/js/topic_generator.js b/static/js/topic_generator.js index 3e74b2b00c..e6a95d3711 100644 --- a/static/js/topic_generator.js +++ b/static/js/topic_generator.js @@ -110,6 +110,18 @@ exports.filter = function (gen, filter_func) { }; }; +exports.map = function (gen, map_func) { + return { + next: function () { + var val = gen.next(); + if (val === undefined) { + return; + } + return map_func(val); + }, + }; +}; + exports.next_topic = function (streams, get_topics, has_unread_messages, curr_stream, curr_topic) { var stream_gen = exports.wrap(streams, curr_stream);