From df6d6447880184ae6229da85216ecee475bd1666 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Sat, 4 Mar 2017 09:49:32 -0800 Subject: [PATCH] refactor: Extract get_sub_for_target(). --- static/js/subs.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/static/js/subs.js b/static/js/subs.js index 9403598260..ec66be89a3 100644 --- a/static/js/subs.js +++ b/static/js/subs.js @@ -85,12 +85,27 @@ function get_stream_id(target) { return target.closest(".stream-row, .subscription_settings").attr("data-stream-id"); } +function get_sub_for_target(target) { + var stream_id = get_stream_id(target); + if (!stream_id) { + blueslip.error('Cannot find stream id for target'); + return; + } + + var sub = stream_data.get_sub_by_id(stream_id); + if (!sub) { + blueslip.error('get_sub_for_target() failed id lookup: ' + stream_id); + return; + } + return sub; +} + // Finds the stream name of a jquery object that's inside a // .stream-row or .subscription_settings element. function get_stream_name(target) { - var stream = stream_data.get_sub_by_id(get_stream_id(target)); - if (stream) { - return stream.name; + var sub = get_sub_for_target(target); + if (sub) { + return sub.name; } }