<commit_before>(function(){

  var root = this;

  // Native extension
  if (typeof exports !== 'undefined') {
    var util = require('../../util/util');
    var errors = require('../../util/errors');
    var Store = require('./store').Store;
    var _ = require('underscore');
  } else {
    var util = root.Substance.util;
    var errors = root.Substance.errors;
    var Store = root.Substance.Store;
    var _ = root._;
  }

  var LocalStore = function(settings) {
    var scope = settings.scope || "";

    Store.call(this);
    var proto = util.prototype(this);

    function key(id) {
       return scope+":documents:"+id;
    }

    function __documents__() {
      return new LocalStore.Hash(scope+":documents");
    }

    proto.__list__ = function () {
      return __documents__().keys();
    }

    proto.__init__ = function (id) {
      __documents__().set(id, true);
    }

    proto.__delete__ = function (id) {
      __documents__().delete(id);
      this.__meta__(id).delete();
      this.__refs__(id).delete();
      this.__commits__(id).delete();
      this.__blobs__(id).delete();
    }

    proto.__deletedDocuments__ = function () {
      return new LocalStore.Hash(scope+":trashbin");
    }

    proto.__exists__ = function (id) {
      return __documents__().contains(id);
    }

    proto.__meta__ = function (id) {
      return new LocalStore.Hash(key(id)+":meta");
    }

    proto.__refs__ = function (id, branch) {
      return new LocalStore.Hash(key(id)+":refs");
    }

    proto.__commits__ = function (id) {
      return new LocalStore.Hash(key(id)+":commits");
    }

    proto.__blobs__ = function(id) {
      return new LocalStore.Hash(key(id)+":blobs");
    }

    proto.__clear__ = function() {
      var ids = this.__list__();
      var self = this;
      _.each(ids, function(id) {
        self.__delete__(id);
      });
      this.__deletedDocuments__().delete();
    }
  };

  LocalStore.Hash = function(scope) {

    var KEYS = scope+":__keys__";

    this.contains = function(key) {
      return localStorage.hasOwnProperty(scope+":"+key);
    };

    this.get = function(key) {
      if (arguments.length == 0) {
        var keys = this.keys();
        var result = {};
        var self = this;<commit_msg>Fix bugs in localStorage adapter.<commit_after>(function(){

  var root = this;

  // Native extension
  if (typeof exports !== 'undefined') {
    var util = require('../../util/util');
    var errors = require('../../util/errors');
    var Store = require('./store').Store;
    var _ = require('underscore');
  } else {
    var util = root.Substance.util;
    var errors = root.Substance.errors;
    var Store = root.Substance.Store;
    var _ = root._;
  }

  var LocalStore = function(scope) {
    scope = scope || "";
    console.log("LocalStore: scope", scope);

    Store.call(this);
    var proto = util.prototype(this);

    function __key__(id) {
       return scope+":documents:"+id;
    };

    function __documents__() {
      return new LocalStore.Hash(scope+":documents");
    }

    proto.__list__ = function () {
      return __documents__().keys();
    }

    proto.__init__ = function (id) {
      __documents__().set(id, true);
    }

    proto.__delete__ = function (id) {
      __documents__().delete(id);
      this.__meta__(id).delete();
      this.__refs__(id).delete();
      this.__commits__(id).delete();
      this.__blobs__(id).delete();
    }

    proto.__deletedDocuments__ = function () {
      return new LocalStore.Hash(scope+":trashbin");
    }

    proto.__exists__ = function (id) {
      return __documents__().contains(id);
    }

    proto.__meta__ = function (id) {
      return new LocalStore.Hash(__key__(id)+":meta");
    }

    proto.__refs__ = function (id, branch) {
      return new LocalStore.Hash(__key__(id)+":refs");
    }

    proto.__commits__ = function (id) {
      return new LocalStore.Hash(__key__(id)+":commits");
    }

    proto.__blobs__ = function(id) {
      return new LocalStore.Hash(__key__(id)+":blobs");
    }

    proto.__clear__ = function() {
      var keys = [];
      var idx = 0;
      var key;
      while(key = localStorage.key(idx++)) {
        //console.log("Is prefix?", scope, key, key.indexOf(scope));
        if (key.indexOf(scope) === 0) {
          keys.push(key);
        }
      }
      _.each(keys, function(key) {
        console.log("Clearing", key);
        localStorage.removeItem(key);
      })
    }
  };

  LocalStore.Hash = function(scope) {

    var KEYS = scope+":__keys__";

    this.contains = function(key) {
      return localStorage.hasOwnProperty(scope+":"+key);
    };

    this.get = function(key) {
      if (arguments.length == 0) {
        var keys = this.keys();
        var result = {};
        var self = this;
{'fraction_non_alphanumeric': 0.16104712041884817, 'fraction_numerical': 0.000837696335078534, 'mean_word_length': 2.4886778670562455, 'pattern_counts': {'":': 18, '<': 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': '4779950', 'n_tokens_mistral': 1521, 'n_tokens_neox': 1453, 'n_words': 292}