<commit_before>(function() {
  'use strict';

  angular.module('dimApp')
    .factory('dimSettingsService', SettingsService);

  SettingsService.$inject = ['$q', '$rootScope'];

  function SettingsService($q, $rootScope) {
    var settingState;
    var currentSettings = null;

    return {
      'getSetting': getSetting,
      'getSettings': getSettings,
      'saveSetting': saveSetting
    };

    function loadSettings() {
      settingState = {
        hideFilteredItems: false,
        itemDetails: false,
        itemStat: false,
        condensed: false,
        characterOrder: 'mostRecent',
        itemSort: 'primaryStat',
        charCol: 3,
        vaultCol: 4
      };

      if (currentSettings !== null) {
        return $q.when(currentSettings);
      } else {
        return $q(function(resolve, reject) {
          function processStorageSettings(data) {
            if (_.has(data, "settings-v1.0")) {
              currentSettings = data["settings-v1.0"];

              var currentKeys = _.keys(currentSettings);
              var defaultKeys = _.keys(settingState);
              var diff = _.difference(defaultKeys, currentKeys);

              _.each(diff, function(key) {
                if ((key === 'charCol') && currentSettings.condensed) {
                  currentSettings.charCol = 4;
                } else if ((key === 'vaultCol') && currentSettings.condensed) {
                  currentSettings.vaultCol = 6;
                } else {
                  currentSettings[key] = settingState[key];
                }

              });

              if (diff.length > 0) {
                saveSettings();
              }
            } else {
              currentSettings = _.clone(settingState);

              saveSettings();
            }

            resolve(currentSettings);
          }

          chrome.storage.sync.get(null, processStorageSettings);
        });
      }
    }

    function getSettings() {
      return loadSettings();
    }

    function getSetting(key) {
      return loadSettings()
        .then(function(settings) {
          if (_.isUndefined(key)) {
            return settings;
          } else if (_.has(settings, key)) {
            return _.propertyOf(settings)(key);
          } else if (_.has(settingState, key)) {
            // Found default
            settings[key] = settingState[key];
            saveSetting(key, settingState[key]);
          } else {
            return $q.reject("The key is not defined in the settings.");
          }
        });
    }

    function saveSetting(key, value) {
      return getSetting()
        .then(function(settings) {
          settings[key] = value;
          var data = {};
          var kvp = {};

          kvp[key] = value;

          data["settings-v1.0"] = settings;

          chrome.storage.sync.set(data, function(e) {<commit_msg>Return a setting when getting a default.<commit_after>(function() {
  'use strict';

  angular.module('dimApp')
    .factory('dimSettingsService', SettingsService);

  SettingsService.$inject = ['$q', '$rootScope'];

  function SettingsService($q, $rootScope) {
    var settingState;
    var currentSettings = null;

    return {
      'getSetting': getSetting,
      'getSettings': getSettings,
      'saveSetting': saveSetting
    };

    function loadSettings() {
      settingState = {
        hideFilteredItems: false,
        itemDetails: false,
        itemStat: false,
        condensed: false,
        characterOrder: 'mostRecent',
        itemSort: 'primaryStat',
        charCol: 3,
        vaultCol: 4
      };

      if (currentSettings !== null) {
        return $q.when(currentSettings);
      } else {
        return $q(function(resolve, reject) {
          function processStorageSettings(data) {
            if (_.has(data, "settings-v1.0")) {
              currentSettings = data["settings-v1.0"];

              var currentKeys = _.keys(currentSettings);
              var defaultKeys = _.keys(settingState);
              var diff = _.difference(defaultKeys, currentKeys);

              _.each(diff, function(key) {
                if ((key === 'charCol') && currentSettings.condensed) {
                  currentSettings.charCol = 4;
                } else if ((key === 'vaultCol') && currentSettings.condensed) {
                  currentSettings.vaultCol = 6;
                } else {
                  currentSettings[key] = settingState[key];
                }

              });

              if (diff.length > 0) {
                saveSettings();
              }
            } else {
              currentSettings = _.clone(settingState);

              saveSettings();
            }

            resolve(currentSettings);
          }

          chrome.storage.sync.get(null, processStorageSettings);
        });
      }
    }

    function getSettings() {
      return loadSettings();
    }

    function getSetting(key) {
      return loadSettings()
        .then(function(settings) {
          if (_.isUndefined(key)) {
            return settings;
          } else if (_.has(settings, key)) {
            return _.propertyOf(settings)(key);
          } else if (_.has(settingState, key)) {
            // Found default
            settings[key] = settingState[key];
            saveSetting(key, settingState[key]);
            return _.propertyOf(settings)(key);
          } else {
            return $q.reject("The key is not defined in the settings.");
          }
        });
    }

    function saveSetting(key, value) {
      return getSetting()
        .then(function(settings) {
          settings[key] = value;
          var data = {};
          var kvp = {};

          kvp[key] = value;

          data["settings-v1.0"] = settings;

          chrome.storage.sync.set(data, function(e) {
{'fraction_non_alphanumeric': 0.12082973206568712, 'fraction_numerical': 0.0038029386343993088, 'mean_word_length': 1.7228235294117646, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '7053020', 'n_tokens_mistral': 1552, 'n_tokens_neox': 1481, 'n_words': 318}