<commit_before>'use strict';

let angular = require('angular');

module.exports = angular
  .module('spinnaker.core.filterModel.service', [
    require('../utils/lodash.js'),
    require('angular-ui-router'),
  ])
.factory('filterModelService', function (_, $location, $state, $stateParams, $timeout) {

    function isFilterable(sortFilterModel) {
      return _.size(sortFilterModel) > 0 && _.any(sortFilterModel);
    }

    function getCheckValues(sortFilterModel) {
      return  _.reduce(sortFilterModel, function(acc, val, key) {
        if (val) {
          acc.push(key);
        }
        return acc;
      }, []).sort();
    }

    function checkAccountFilters(model) {
      return function(target) {
        if(isFilterable(model.sortFilter.account)) {
          var checkedAccounts = getCheckValues(model.sortFilter.account);
          return _.contains(checkedAccounts, target.account);
        } else {
          return true;
        }
      };
    }

    function checkRegionFilters(model) {
      return function(target) {
        if(isFilterable(model.sortFilter.region)) {
          var checkedRegions = getCheckValues(model.sortFilter.region);
          return _.contains(checkedRegions, target.region);
        } else {
          return true;
        }
      };
    }

    function checkStackFilters(model) {
      return function(target) {
        if (isFilterable(model.sortFilter.stack)) {
          var checkedStacks = getCheckValues(model.sortFilter.stack);
          return _.contains(checkedStacks, target.stack);
        } else {
          return true;
        }
      };
    }

    function checkStatusFilters(model) {
      return function(target) {
        if (isFilterable(model.sortFilter.status)) {
          var checkedStatus = getCheckValues(model.sortFilter.status);
          return _.contains(checkedStatus, 'Up') && target.downCount === 0 ||
            _.contains(checkedStatus, 'Down') && target.downCount > 0 ||
            _.contains(checkedStatus, 'OutOfService') && target.outOfServiceCount > 0 ||
            _.contains(checkedStatus, 'Starting') && target.startingCount > 0 ||
            _.contains(checkedStatus, 'Disabled') && target.isDisabled;
        }
        return true;
      };
    }

    function checkProviderFilters(model) {
      return function(target) {
        if (isFilterable(model.sortFilter.providerType)) {
          var checkedProviderTypes = getCheckValues(model.sortFilter.providerType);
          return _.contains(checkedProviderTypes, target.type) || _.contains(checkedProviderTypes, target.provider);
        } else {
          return true;
        }
      };
    }

    function getParamVal(property) {
      return $location.search()[property.param] || property.defaultValue;
    }

    function addTagsForSection(model, property) {
      var key = property.model,
          label = property.filterLabel || property.model,
          translator = property.filterTranslator || {},
          clearValue = property.clearValue;
      translator = translator || {};
      var tags = model.tags;
      var modelVal = model.sortFilter[key];
      if (property.type === 'object') {
        _.forOwn(modelVal, function (isActive, value) {
          if (isActive) {
            tags.push({
              key: key,
              label: label,
              value: translator[value] || value,
              clear: function () {
                delete model.sortFilter[key][value];
                model.applyParamsToUrl();
              }
            });
          }
        });
      } else {
        if (modelVal !== null && modelVal !== undefined && modelVal !== '' && modelVal !== false) {
          tags.push({
            key: key,
            label: label,
            value: translator[modelVal] || modelVal,
            clear: function () {
              model.sortFilter[key] = clearValue;
              model.applyParamsToUrl();
            }
          });
        }
      }
      return tags;
    }

    var converters = {
      'object': {
        toParam: function(filterModel, property) {
          var obj = filterModel.sortFilter[property.model];
          if (obj) {
            return _.chain(obj)
              .collect(function (val, key) {
                if (val) {
                  return key;
                }
              })
              .remove(undefined)
              .value()
              .sort()
              .join(',') || null;
          }
        },
        toModel: function(filterModel, property) {
          var paramList = getParamVal(property);
          if (paramList) {
            return _.reduce(paramList.split(','), function (acc, value) {
              acc[value] = true;
              return acc;
            }, {});
          } else {
            return {};
          }
        }
      },
      'string': {
        toParam: function(filterModel, property) {
          var val = filterModel.sortFilter[property.model];
          return val && val !== property.defaultValue ? val : null;
        },
        toModel: function(filterModel, property) {
          var val = getParamVal(property);
          return val ? val : '';
        }
      },
      'boolean': {
        toParam: function(filterModel, property) {
          var val = filterModel.sortFilter[property.model];
          return val ? val.toString() : null;
        },
        toModel: function(filterModel, property) {
          var val = getParamVal(property);
          return Boolean(val);
        }
      },
      'inverse-boolean': {
        toParam: function(filterModel, property) {
          var val = filterModel.sortFilter[property.model];<commit_msg>encode commas when storing as filter model objects in url<commit_after>'use strict';

let angular = require('angular');

module.exports = angular
  .module('spinnaker.core.filterModel.service', [
    require('../utils/lodash.js'),
    require('angular-ui-router'),
  ])
.factory('filterModelService', function (_, $location, $state, $stateParams, $timeout) {

    function isFilterable(sortFilterModel) {
      return _.size(sortFilterModel) > 0 && _.any(sortFilterModel);
    }

    function getCheckValues(sortFilterModel) {
      return  _.reduce(sortFilterModel, function(acc, val, key) {
        if (val) {
          acc.push(key);
        }
        return acc;
      }, []).sort();
    }

    function checkAccountFilters(model) {
      return function(target) {
        if(isFilterable(model.sortFilter.account)) {
          var checkedAccounts = getCheckValues(model.sortFilter.account);
          return _.contains(checkedAccounts, target.account);
        } else {
          return true;
        }
      };
    }

    function checkRegionFilters(model) {
      return function(target) {
        if(isFilterable(model.sortFilter.region)) {
          var checkedRegions = getCheckValues(model.sortFilter.region);
          return _.contains(checkedRegions, target.region);
        } else {
          return true;
        }
      };
    }

    function checkStackFilters(model) {
      return function(target) {
        if (isFilterable(model.sortFilter.stack)) {
          var checkedStacks = getCheckValues(model.sortFilter.stack);
          return _.contains(checkedStacks, target.stack);
        } else {
          return true;
        }
      };
    }

    function checkStatusFilters(model) {
      return function(target) {
        if (isFilterable(model.sortFilter.status)) {
          var checkedStatus = getCheckValues(model.sortFilter.status);
          return _.contains(checkedStatus, 'Up') && target.downCount === 0 ||
            _.contains(checkedStatus, 'Down') && target.downCount > 0 ||
            _.contains(checkedStatus, 'OutOfService') && target.outOfServiceCount > 0 ||
            _.contains(checkedStatus, 'Starting') && target.startingCount > 0 ||
            _.contains(checkedStatus, 'Disabled') && target.isDisabled;
        }
        return true;
      };
    }

    function checkProviderFilters(model) {
      return function(target) {
        if (isFilterable(model.sortFilter.providerType)) {
          var checkedProviderTypes = getCheckValues(model.sortFilter.providerType);
          return _.contains(checkedProviderTypes, target.type) || _.contains(checkedProviderTypes, target.provider);
        } else {
          return true;
        }
      };
    }

    function getParamVal(property) {
      return $location.search()[property.param] || property.defaultValue;
    }

    function addTagsForSection(model, property) {
      var key = property.model,
          label = property.filterLabel || property.model,
          translator = property.filterTranslator || {},
          clearValue = property.clearValue;
      translator = translator || {};
      var tags = model.tags;
      var modelVal = model.sortFilter[key];
      if (property.type === 'object') {
        _.forOwn(modelVal, function (isActive, value) {
          if (isActive) {
            tags.push({
              key: key,
              label: label,
              value: translator[value] || value,
              clear: function () {
                delete model.sortFilter[key][value];
                model.applyParamsToUrl();
              }
            });
          }
        });
      } else {
        if (modelVal !== null && modelVal !== undefined && modelVal !== '' && modelVal !== false) {
          tags.push({
            key: key,
            label: label,
            value: translator[modelVal] || modelVal,
            clear: function () {
              model.sortFilter[key] = clearValue;
              model.applyParamsToUrl();
            }
          });
        }
      }
      return tags;
    }

    var converters = {
      'object': {
        toParam: function(filterModel, property) {
          var obj = filterModel.sortFilter[property.model];
          if (obj) {
            return _.chain(obj)
              .collect(function (val, key) {
                if (val) {
                  // replace any commas in the string with their uri-encoded version ('%2c'), since
                  // we use commas as our separator in the URL
                  return key.replace(/,/g, '%2c');
                }
              })
              .remove(undefined)
              .value()
              .sort()
              .join(',') || null;
          }
        },
        toModel: function(filterModel, property) {
          var paramList = getParamVal(property);
          if (paramList) {
            return _.reduce(paramList.split(','), function (acc, value) {
              // replace any uri-encoded commas in the string ('%2c') with actual commas, since
              // we use commas as our separator in the URL
              acc[value.replace(/%2c/g, ',')] = true;
              return acc;
            }, {});
          } else {
            return {};
          }
        }
      },
      'string': {
        toParam: function(filterModel, property) {
          var val = filterModel.sortFilter[property.model];
          return val && val !== property.defaultValue ? val : null;
        },
        toModel: function(filterModel, property) {
          var val = getParamVal(property);
          return val ? val : '';
        }
      },
      'boolean': {
        toParam: function(filterModel, property) {
          var val = filterModel.sortFilter[property.model];
          return val ? val.toString() : null;
        },
        toModel: function(filterModel, property) {
          var val = getParamVal(property);
          return Boolean(val);
        }
      },
      'inverse-boolean': {
        toParam: function(filterModel, property) {
          var val = filterModel.sortFilter[property.model];
{'fraction_non_alphanumeric': 0.12074568154609201, 'fraction_numerical': 0.0011971951428082776, 'mean_word_length': 1.8748770894788593, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '5010206', 'n_tokens_mistral': 3099, 'n_tokens_neox': 3051, 'n_words': 631}