<commit_before>var Event = require('./event');
var AdapterMemory = require('./adapter/memory');

/**
 * @class PersistentStorage
 * @extends Event
 */
var PersistentStorage = Event.extend({

  /**
   * @param {String} name
   * @param {Storage} adapter
   * @param {*} [logger]
   */
  constructor: function(name, adapter, logger) {
    this._name = name;
    this._data = {};
    this._adapter = null;
    this._logger = logger && logger.warn ? logger : console;
    if (this._isSupported(adapter)) {
      this._adapter = adapter;
    } else {
      this._adapter = new AdapterMemory();
    }
  },

  /**
   * @param {String} [name]
   * @returns {*}
   */
  get: function(name) {
    var obj = this.read();
    if (name) {
      return obj[name];
    } else {
      return obj;
    }
  },

  /**
   * @param {String|Object} key
   * @param {*} [value]
   */
  set: function(key, value) {
    var obj = this.read();
    if (!_.isUndefined(value)) {
      obj[key] = value;
    } else {
      _.extend(obj, key);
      obj = key;
    }
    this.write(obj);
  },

  /**
   * @param {String} key
   * @returns {Boolean}
   */
  has: function(key) {
    var obj = this.read();
    return !!_.contains(_.keys(obj), key);
  },

  /**
   * @param {String} name
   */
  remove: function(name) {
    var obj = this.read();
    delete obj[name];
    this.write(obj);
  },

  clear: function() {
    this.write();
  },

  delete: function() {
    this._adapter.removeItem(this._name);
  },

  /**
   * @returns {Object}
   */
  read: function() {
    var data = {};
    try {
      var rawData = this._adapter.getItem(this._name);
      if (!_.isUndefined(rawData)) {
        data = JSON.parse(rawData);
      }
    } catch (error) {
      this.getLogger().warn('Failed to parse the `%s` PersistentStorage', this._name);
    }
    return data;
  },

  /**
   * @param {Object} [obj]
   */
  write: function(obj) {
    obj = !_.isUndefined(obj) ? obj : {};
    this._adapter.setItem(this._name, JSON.stringify(obj));
  },

  /**
   * @private
   */
  _isSupported: function(adapter) {
    try {<commit_msg>add getter for storage properties<commit_after>var Event = require('./event');
var AdapterMemory = require('./adapter/memory');

/**
 * @class PersistentStorage
 * @extends Event
 */
var PersistentStorage = Event.extend({

  /**
   * @param {String} name
   * @param {Storage} adapter
   * @param {*} [logger]
   */
  constructor: function(name, adapter, logger) {
    this._name = name;
    this._adapter = null;
    this._logger = logger && logger.warn ? logger : console;
    if (this._isSupported(adapter)) {
      this._adapter = adapter;
    } else {
      this._adapter = new AdapterMemory();
    }
  },

  /**
   * @param {String} [name]
   * @returns {*}
   */
  get: function(name) {
    var obj = this.read();
    if (name) {
      return obj[name];
    } else {
      return obj;
    }
  },

  /**
   * @param {String|Object} key
   * @param {*} [value]
   */
  set: function(key, value) {
    var obj = this.read();
    if (!_.isUndefined(value)) {
      obj[key] = value;
    } else {
      _.extend(obj, key);
      obj = key;
    }
    this.write(obj);
  },

  /**
   * @param {String} key
   * @returns {Boolean}
   */
  has: function(key) {
    var obj = this.read();
    return !!_.contains(_.keys(obj), key);
  },

  /**
   * @param {String} name
   */
  remove: function(name) {
    var obj = this.read();
    delete obj[name];
    this.write(obj);
  },

  clear: function() {
    this.write();
  },

  delete: function() {
    this.getAdapter().removeItem(this.getName());
  },

  /**
   * @returns {Object}
   */
  read: function() {
    var data = {};
    try {
      var rawData = this.getAdapter().getItem(this.getName());
      if (!_.isUndefined(rawData)) {
        data = JSON.parse(rawData);
      }
    } catch (error) {
      this.getLogger().warn('Failed to parse the `%s` PersistentStorage', this.getName(), error);
    }
    return data;
  },

  /**
   * @param {Object} [obj]
   */
  write: function(obj) {
    obj = !_.isUndefined(obj) ? obj : {};
    this.getAdapter().setItem(this.getName(), JSON.stringify(obj));
  },

  /**
   * @returns {String}
   */
  getName: function() {
    return this._name;
  },

  /**
   * @returns {Storage|*}
   */
  getAdapter: function() {
    return this._adapter;
  },

  /**
   * @returns {*}
   */
  getLogger: function() {
    return this._logger;
  },

  /**
   * @private
   */
  _isSupported: function(adapter) {
    try {
{'fraction_non_alphanumeric': 0.18319253001333927, 'fraction_numerical': 0.0, 'mean_word_length': 2.295970695970696, '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': '5266805', 'n_tokens_mistral': 1549, 'n_tokens_neox': 1526, 'n_words': 319}