<commit_before>import FileSaver from 'file-saver'

const importData = (event) => {
  const files = event.target.files;

  if (files[0]) { 
    return new Promise((resolve, reject) => {
      const fileReader = new FileReader();
      fileReader.onload = () => {
        try {
          chrome.storage.local.set(JSON.parse(fileReader.result));
          resolve();
        } catch (e) {
          reject(e);
        }
      };

      fileReader.onerror = (arg) => {
        reject(arg);
      };

      fileReader.readAsText(files[0], 'utf-8');
    });
  } else {
    console.log('Nothing to import');

    return Promise.resolve();
  }
}

const exportData = () => {
  // since there is storageLocal, I don't know if it would be better to put
  // that function call there
  chrome.storage.local.get('savedTabs', (savedTabs) => {
    const result = JSON.stringify(savedTabs);

    const blob = new Blob([result], {
      type: 'application/json;charset=utf-8',
    });
    FileSaver.saveAs(blob, exportFileName(new Date(Date.now())));
  })
}

const exportFileName = (date) => {
  const localeDateString = date.toLocaleDateString().replace(/\//g, '-')
  return `TabWranglerExport-${localeDateString}.json`
}

export {
  importData,
  exportData,
  exportFileName,
}<commit_msg>-update rimraf - simplify the code to read the data from local storage with a function returning a promise. During export all 4 relevant values for an export are read and returned as one result - updated the test<commit_after>import FileSaver from 'file-saver'

function readLocalStorage(key) {
  return new Promise((resolve) => {
    chrome.storage.local.get(key, (value) => {
      resolve(value);
    });
  });
}

const importData = (event) => {
  const files = event.target.files;

  if (files[0]) { 
    return new Promise((resolve, reject) => {
      const fileReader = new FileReader();
      fileReader.onload = () => {
        try {
          chrome.storage.local.set(JSON.parse(fileReader.result));
          resolve();
        } catch (e) {
          reject(e);
        }
      };

      fileReader.onerror = (arg) => {
        reject(arg);
      };

      fileReader.readAsText(files[0], 'utf-8');
    });
  } else {
    console.log('Nothing to import');

    return Promise.resolve();
  }
}

const exportData = () => {
  // since there is storageLocal, I don't know if it would be better to put
  // that function call there
  Promise.all([
    readLocalStorage('savedTabs'),
    readLocalStorage('totalTabsRemoved'),
    readLocalStorage('totalTabsUnwrangled'),
    readLocalStorage('totalTabsWrangled')]).then((allValues) => {
      // allValues is an array containing all the values stored in local storage
      const _result = JSON.stringify(allValues);

      const blob = new Blob([_result], {
        type: 'application/json;charset=utf-8',
      });
      FileSaver.saveAs(blob, exportFileName(new Date(Date.now())));
    });
}

const exportFileName = (date) => {
  const localeDateString = date.toLocaleDateString().replace(/\//g, '-')
  return `TabWranglerExport-${localeDateString}.json`
}

export {
  importData,
  exportData,
  exportFileName,
}
{'fraction_non_alphanumeric': 0.1426755640292342, 'fraction_numerical': 0.002859866539561487, 'mean_word_length': 2.896039603960396, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5592932', 'n_tokens_mistral': 972, 'n_tokens_neox': 948, 'n_words': 227}