<commit_before>angular.module('mushin').factory('lists',
  function ($rootScope, $filter, hoodie, $q) {

  var debug = new window.$debug('mushin:services/list');

  var lists = {};

  hoodie.store.on('change:list', function(name, list) {
    $rootScope.$broadcast('listChange', {
      type: name,
      list: list
    })
  });

  var getAll = function() {
      var promise = $q.when(hoodie.store.findAll('list'));

      var deferred = $q.defer();
      promise.then(function(listsData) {
        debug('loaded ' + listsData.length + ' lists');
        lists = listsData;
        deferred.resolve(listsData);
      });
      return deferred.promise;
  };

  return {
    get: function(id) {
      return $q.when(hoodie.store.find('list', id));
    },
    getAll: getAll,
    /* add: if the title is the same, update if already exists */
    add: function(title, query) {
      var existing = $filter('filter')(lists, function(list) {
        return list.title == title;
      });
      var listData = {
          title: title,
          query: query
      };

      if (existing) {
        var list = existing[0];

        debug('updating existing list with title ' + list.title);
      } else {
        debug('saving new list with title ' + title);
        return $q.when(hoodie.store.add('list', listData));
      }
    },
  }
});<commit_msg>make add overwrite lists with the same title<commit_after>angular.module('mushin').factory('lists',
  function ($rootScope, $filter, hoodie, $q) {

  var debug = new window.$debug('mushin:services/list');

  var lists = {};

  hoodie.store.on('change:list', function(name, list) {
    $rootScope.$broadcast('listChange', {
      type: name,
      list: list
    })
  });

  var getAll = function() {
      var promise = $q.when(hoodie.store.findAll('list'));

      var deferred = $q.defer();
      promise.then(function(listsData) {
        debug('loaded ' + listsData.length + ' lists');
        lists = listsData;
        deferred.resolve(listsData);
      });
      return deferred.promise;
  };

  return {
    get: function(id) {
      return $q.when(hoodie.store.find('list', id));
    },
    getAll: getAll,
    /* add: if the title is the same, update if already exists */
    add: function(title, query) {
      getAll()
        .then(function(lists) {

          var existing = $filter('filter')(lists, function(list) {
            return list.title == title;
          });
          var listData = {
              title: title,
              query: query
          };

          if (existing) {
            var list = existing[0];

            debug('updating existing list with title ' + list.title);

            return $q.when(hoodie.store.update('list', list.id, listData));

          } else {
            debug('saving new list with title ' + title);

            return $q.when(hoodie.store.add('list', listData));
          }
      });
    },
  }
});
{'fraction_non_alphanumeric': 0.15294924554183814, 'fraction_numerical': 0.0006858710562414266, 'mean_word_length': 2.230343300110742, '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': '5778073', 'n_tokens_mistral': 899, 'n_tokens_neox': 873, 'n_words': 201}