index.js
/**
 * Downloads all surveys from `localStorage` either as JSON or CSV
 * @param {boolean} askUser A boolean that represents whether to prompt the user
 */
Downloads all surveys from `localStorage` either as JSON or CSV
@param {boolean} askUser A boolean that represents whether to prompt the user
Downloads all surveys from ` localStorage ` either as JSON or CSV @param { boolean } askUser A boolean that represents whether to prompt the user
function downloadSurveys(askUser = true) {
  if (askUser) if (!confirm("Confirm download?")) return;
  const anchor = document.createElement("a");
  anchor.href = "data:text/plain;charset=utf-8,";
  switch (downloadSelect.value) {
    case "JSON":
      anchor.href += encodeURIComponent(localStorage.surveys);
      anchor.download = "surveys.json";
      break;
    case "CSV":
      let surveys = JSON.parse(localStorage.surveys);
      let csv = "";
      if (surveys) {
        surveys.forEach(survey => {
          let surveyAsCSV = "";
          survey.forEach(metric => {
            if (typeof metric.value == "string") surveyAsCSV += "\"" + metric.value + "\",";
            else surveyAsCSV += metric.value + ",";
          });
          csv += surveyAsCSV + "\n";
        });
      }
      anchor.href += encodeURIComponent(csv);
      anchor.download = "surveys.csv";
      break;
  }
  document.body.append(anchor);
  anchor.click();
  anchor.remove();
}
function downloadSurveys ( askUser = true ) { if ( askUser ) if ( ! confirm ( "Confirm download?" ) ) return ; const anchor = document . createElement ( "a" ) ; anchor . href = "data:text/plain;charset=utf-8," ; switch ( downloadSelect . value ) { case "JSON" : anchor . href += encodeURIComponent ( localStorage . surveys ) ; anchor . download = "surveys.json" ; break ; case "CSV" : let surveys = JSON . parse ( localStorage . surveys ) ; let csv = "" ; if ( surveys ) { surveys . forEach ( survey => { let surveyAsCSV = "" ; survey . forEach ( metric => { if ( typeof metric . value == "string" ) surveyAsCSV += "\"" + metric . value + "\"," ; else surveyAsCSV += metric . value + "," ; } ) ; csv += surveyAsCSV + "\n" ; } ) ; } anchor . href += encodeURIComponent ( csv ) ; anchor . download = "surveys.csv" ; break ; } document . body . append ( anchor ) ; anchor . click ( ) ; anchor . remove ( ) ; }
Downloads all surveys from `localStorage` either as JSON or CSV
 @param {boolean} askUser A boolean that represents whether to prompt the user
Downloads all surveys from ` localStorage ` either as JSON or CSV @param { boolean } askUser A boolean that represents whether to prompt the user