let  Utils = {
  groupTabs(tabs, method) {
    if (method == "window") {
      return this.groupByWindow(tabs);
    } else if ( method == "domain") {
      return this.groupByDomain(tabs);
    }

    console.log("problem getting group by preference");
    return this.groupByWindow(tabs);
  },

  // returns tabs grouped by window
  // input [tabs]
  // Map {window_id => [tab]+}
  groupByWindow(tabs) {
    return tabs.reduce(function(memo, cur) {
      let win = memo.get(cur.windowId) || [];

      win.push(cur);
      memo.set(cur.windowId, win);

      return memo;
    }, new Map());
  },

  // returns tabs grouped by domain name
  // input [tabs]
  // Map {baseDomain => [tab]+}
  groupByDomain(tabs) {
    let returnValue = new Map();
    let tabmap =  tabs.reduce(function(memo, cur) {
      let url = new URL(cur.url);
      let win = memo.get(url.hostname) || [];

      win.push(cur);
      memo.set(url.hostname, win);

      return memo;
    }, new Map());

    for(let [domain, tablist] of tabmap) {
      if (tablist.length == 1){
        // group the misc domains into one group
        let misc = returnValue.get("Misc") || [];

        misc.push(tablist[0]);
        returnValue.set("Misc", misc);
      } else {
        returnValue.set(domain, tablist);
      }
    }

    return returnValue;
  },

  // Creates the audible elements division at the top of the page.
  createAudibleElement(tabs) {
    let returnval = document.createElement("div");

    // section title
    var AudibleTitle = document.createElement("h2");
    AudibleTitle.innerText = "Audible Tabs";
    returnval.appendChild(AudibleTitle);

    // each tab
    tabs.forEach( (audibleTab) => {
      let ul = document.createElement("ul");
      returnval.appendChild(ul);

      for(let tab of audibleTabs) {
        var link = Utils.makeLink(tab);
        var li = document.createElement("li");

        li.appendChild(link);
        ul.appendChild(li);
      }
    });

    return returnval;
  },

  lexSort(l,r) {
    let left = new URL(l.url);
    let right = new URL(r.url);
    left = left.hostname || left.href;
    right = right.hostname || right.href;

    if(left > right) {
      return 1;
    }
    if(left < right) {
      return -1;
    }
    return 0;
  },

  isNumeric(value) {
    return !isNaN(value - parseFloat(value));
  },

/**
 * Sorts a Map of window_ids with an array of tabs as the values by the domain of the url.
 * @param {Boolean} sortByDoemainValue - Parameter description.
 * @param {Map} windows - input Map {window_ids => [tab]+}
 * @returns {Map<String,Array>} returns Map {window_ids => [sorted_tabs]}
 */
  sortByDomain(sortByDomainValue, windows) {
    if(sortByDomainValue){
      let sortedWindows = new Map();

      for(let [windowId, tabs] of windows){
        let sortedTabs = tabs.sort(this.lexSort);
        sortedWindows.set(windowId, sortedTabs);
      }
      return sortedWindows;
    }
    return windows;
  },

  // Returns a link object for a tab with click handlers set
  makeLink(tab, clickHandler) {
    var link = document.createElement("a");
    link.href = "#";
    link.onclick = clickHandler;
    link.tabId = tab.id;
    link.windowId = tab.windowId;

    var linkText = tab.title || tab.url;
    var isActive = tab.active ? " <span style='color:darkred'> &raquo; (Selected)</span> " : "" ;

    var audibleText = tab.audible ? "&#x1f508; " : "";
    var favicon = tab.favIconUrl ? "<img src='" + tab.favIconUrl + "' width='24' height='24'/><span>&nbsp;</span>": "";
    link.innerHTML = favicon + audibleText + linkText + isActive;

    return link;
  },

  // returns an array of tabs for a set of tabs 
  // input [tabs]
  // returns [urls]
  getLinksFromTabs(tabs) {
    return tabs.reduce((memo, cur) => {
      memo = memo || [];
      memo.push(this.handleSpecialURIs(cur.url));
      return memo;
    }, [])
      .filter(val => val);
  },

  handleSpecialURIs(url) {
    //might need to handle about:config, performance etc.
    if (url.search("http") >= 0) {
      return decodeURIComponent(url.replace("about:reader?url=",""));
    }

    return null;
  }


};

module.exports = Utils;

@@ -1318,677 +1318,8 @@
 %7D,%0A%0A
-  // Creates the audible elements division at the top of the page.%0A  createAudibleElement(tabs) %7B%0A    let returnval = document.createElement(%22div%22);%0A%0A    // section title%0A    var AudibleTitle = document.createElement(%22h2%22);%0A    AudibleTitle.innerText = %22Audible Tabs%22;%0A    returnval.appendChild(AudibleTitle);%0A%0A    // each tab%0A    tabs.forEach( (audibleTab) =%3E %7B%0A      let ul = document.createElement(%22ul%22);%0A      returnval.appendChild(ul);%0A%0A      for(let tab of audibleTabs) %7B%0A        var link = Utils.makeLink(tab);%0A        var li = document.createElement(%22li%22);%0A%0A        li.appendChild(link);%0A        ul.appendChild(li);%0A      %7D%0A    %7D);%0A%0A    return returnval;%0A  %7D,%0A%0A
   le
@@ -1327,24 +1327,24 @@
 Sort(l,r) %7B%0A
+
     let left
@@ -1664,12 +1664,16 @@
 %7D,%0A%0A
+  
 /**%0A
+  
  * S
@@ -1759,16 +1759,18 @@
 he url.%0A
+  
  * @para
@@ -1825,16 +1825,18 @@
 iption.%0A
+  
  * @para
@@ -1886,16 +1886,18 @@
 %5Btab%5D+%7D%0A
+  
  * @retu
@@ -1962,16 +1962,18 @@
 _tabs%5D%7D%0A
+  
  */%0A  so
@@ -2293,664 +2293,8 @@
 %7D,%0A%0A
-  // Returns a link object for a tab with click handlers set%0A  makeLink(tab, clickHandler) %7B%0A    var link = document.createElement(%22a%22);%0A    link.href = %22#%22;%0A    link.onclick = clickHandler;%0A    link.tabId = tab.id;%0A    link.windowId = tab.windowId;%0A%0A    var linkText = tab.title %7C%7C tab.url;%0A    var isActive = tab.active ? %22 %3Cspan style='color:darkred'%3E &raquo; (Selected)%3C/span%3E %22 : %22%22 ;%0A%0A    var audibleText = tab.audible ? %22&#x1f508; %22 : %22%22;%0A    var favicon = tab.favIconUrl ? %22%3Cimg src='%22 + tab.favIconUrl + %22' width='24' height='24'/%3E%3Cspan%3E&nbsp;%3C/span%3E%22: %22%22;%0A    link.innerHTML = favicon + audibleText + linkText + isActive;%0A%0A    return link;%0A  %7D,%0A%0A
   //
@@ -2336,17 +2336,16 @@
  of tabs
- 
 %0A  // in

clean up the unneeded helpers