<commit_before>'use strict';

var isObject = function(data) { typeof data === typeof {}; };

var parse = function(data) {
  var out = null;
  try {
    out = JSON.parse(data);
    if (!out) {
      out = null;
    }
  } catch (e) {
    out = null;
  }
  return out;
};

var load_item = function(namespace){
  return parse(window.localStorage.getItem(namespace));
};

module.exports = {
  load_item : load_item,
  init : function(app, namespace) {

    var port_name_load = namespace + '_load';
    if(!app.ports || !app.ports[port_name_load]){
      console.warn('Undefined ELM port', port_name_load);  // eslint-disable-line
      return;
    }

    var port_name_remove = namespace + '_remove';
    if(!app.ports || !app.ports[port_name_remove]){
      console.warn('Undefined ELM port', port_name_remove);  // eslint-disable-line
      return;
    }

    var port_name_set = namespace + '_set';
    if(!app.ports || !app.ports[port_name_set]){
      console.warn('Undefined ELM port', port_name_set);  // eslint-disable-line
      return;
    }

    app.ports[port_name_load].subscribe(function() {
      app.ports[namespace + '_get'].send(load_item(namespace));
    });

    app.ports[port_name_remove].subscribe(function() {
      window.localStorage.removeItem(namespace);
      app.ports[namespace + '_get'].send(null);
    });

    app.ports[port_name_set].subscribe(function(new_data) {
      var data = parse(window.localStorage.getItem(namespace));

      // Update without erasing other fields when data and new_data are objects
      if (isObject(data) && isObject(new_data)) {
        for (var property in new_data) {
          if (new_data.hasOwnProperty(property)) {
            data[property] = new_data[property];
          }
        }
      } else {
          data = new_data;
      }

      window.localStorage.setItem(namespace, JSON.stringify(data));
      app.ports[namespace + '_get'].send(data);
    });
  },
};
<commit_msg>elm_common: localstorage.js should check for *_get port<commit_after>'use strict';

var isObject = function(data) { typeof data === typeof {}; };

var parse = function(data) {
  var out = null;
  try {
    out = JSON.parse(data);
    if (!out) {
      out = null;
    }
  } catch (e) {
    out = null;
  }
  return out;
};

var load_item = function(namespace){
  return parse(window.localStorage.getItem(namespace));
};

module.exports = {
  load_item : load_item,
  init : function(app, namespace) {

    var port_name_load = namespace + '_load';
    if(!app.ports || !app.ports[port_name_load]){
      console.warn('Undefined ELM port', port_name_load);  // eslint-disable-line
      return;
    }

    var port_name_remove = namespace + '_remove';
    if(!app.ports || !app.ports[port_name_remove]){
      console.warn('Undefined ELM port', port_name_remove);  // eslint-disable-line
      return;
    }

    var port_name_set = namespace + '_set';
    if(!app.ports || !app.ports[port_name_set]){
      console.warn('Undefined ELM port', port_name_set);  // eslint-disable-line
      return;
    }

    var port_name_get = namespace + '_get';
    if(!app.ports || !app.ports[port_name_get]){
      console.warn('Undefined ELM port', port_name_get);  // eslint-disable-line
      return;
    }

    app.ports[port_name_load].subscribe(function() {
      app.ports[port_name_get].send(load_item(namespace));
    });

    app.ports[port_name_remove].subscribe(function() {
      window.localStorage.removeItem(namespace);
      app.ports[port_name_get].send(null);
    });

    app.ports[port_name_set].subscribe(function(new_data) {
      var data = parse(window.localStorage.getItem(namespace));

      // Update without erasing other fields when data and new_data are objects
      if (isObject(data) && isObject(new_data)) {
        for (var property in new_data) {
          if (new_data.hasOwnProperty(property)) {
            data[property] = new_data[property];
          }
        }
      } else {
          data = new_data;
      }

      window.localStorage.setItem(namespace, JSON.stringify(data));
      app.ports[port_name_get].send(data);
    });
  },
};

{'fraction_non_alphanumeric': 0.14199029126213591, 'fraction_numerical': 0.0, 'mean_word_length': 2.95489443378119, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5692729', 'n_tokens_mistral': 1334, 'n_tokens_neox': 1321, 'n_words': 245}