<commit_before>import { actionTypes as paginationActionTypes } from './pagination';

import debounce from 'lodash/debounce';
import isEmpty from 'lodash/isEmpty';
import isObject from 'lodash/isObject';

const UPDATE_ROLE     = 'search/role';
const UPDATE_TYPE     = 'search/type';
const UPDATE_KEYWORD  = 'search/keyword';
const UPDATE_SORT     = 'search/sort';
const UPDATE_VIEW     = 'search/view';
const SYNC_RESULTS    = 'search/results';
const PRE_SEARCH      = 'search/pre';
const ERROR_SEARCH    = 'search/error';
const RESET_QUERY     = 'search/reset';
const SUBMIT_SEARCH   = 'search/search';

const initialState = {
  role: {},
  type: {},
  sort_by: 'a-z',
  view: 'sellers',
  keyword: '',
  results: [],
  products: [],
  casestudies: [],
  querying: false,
  error: false,
  user_role: ''
}

const statusCheck = (response) => {
    if (response.status >= 200 && response.status < 300) {
        return response
    } else {
        var error = new Error(response.statusText)
        error.response = response
        throw error
    }
}

const removeFromObject = (object, predicate) => {
  return Object.keys(object)
    .filter(predicate)
    .reduce((result, key) => {
      return { ...result, [key]: object[key] }
    }, {});
}

export const scrubState = (state) => {
  let result = ['results', 'products', 'casestudies', 'querying', 'error', 'pagination'].reduce((obj, key) => {
    return removeFromObject(obj, (k) => key !== k)
  }, state);

  if (isEmpty(result.keyword)) {
    result = removeFromObject(result, (k) => k !== 'keyword')
  }

  return Object.keys(result).reduce((obj, key) => {
    let target = obj[key];
    let pruned = target;
    if (isObject(target)) {
      pruned = removeFromObject(target, (k) => target[k]);
    }

    return { ...obj, [key]: pruned }
  }, result);
}

export const falseValues = (target) => {

  if (isObject(target)) {
    return Object
      .keys(target)
      .reduce((result, key) => {
        return { ...result, [key]: false }
      }, {});
  }

  return false;
}

export const buildQueryString = ({ search = {}, pagination = {} } = {}) => {
  // Scrub results and querying from query, not valid filters.
  let query = { ...scrubState(search), ...pagination }

  return Object.keys(query).reduce((q, key) => {
    let target = query[key];
    let params = [];<commit_msg>URI encode the search keyword in the supplier search when building the query string. (#752)<commit_after>import { actionTypes as paginationActionTypes } from './pagination';

import debounce from 'lodash/debounce';
import isEmpty from 'lodash/isEmpty';
import isObject from 'lodash/isObject';

const UPDATE_ROLE     = 'search/role';
const UPDATE_TYPE     = 'search/type';
const UPDATE_KEYWORD  = 'search/keyword';
const UPDATE_SORT     = 'search/sort';
const UPDATE_VIEW     = 'search/view';
const SYNC_RESULTS    = 'search/results';
const PRE_SEARCH      = 'search/pre';
const ERROR_SEARCH    = 'search/error';
const RESET_QUERY     = 'search/reset';
const SUBMIT_SEARCH   = 'search/search';

const initialState = {
  role: {},
  type: {},
  sort_by: 'a-z',
  view: 'sellers',
  keyword: '',
  results: [],
  products: [],
  casestudies: [],
  querying: false,
  error: false,
  user_role: ''
}

const statusCheck = (response) => {
    if (response.status >= 200 && response.status < 300) {
        return response
    } else {
        var error = new Error(response.statusText)
        error.response = response
        throw error
    }
}

const removeFromObject = (object, predicate) => {
  return Object.keys(object)
    .filter(predicate)
    .reduce((result, key) => {
      return { ...result, [key]: object[key] }
    }, {});
}

export const scrubState = (state) => {
  let result = ['results', 'products', 'casestudies', 'querying', 'error', 'pagination'].reduce((obj, key) => {
    return removeFromObject(obj, (k) => key !== k)
  }, state);

  if (isEmpty(result.keyword)) {
    result = removeFromObject(result, (k) => k !== 'keyword')
  }

  return Object.keys(result).reduce((obj, key) => {
    let target = obj[key];
    let pruned = target;
    if (isObject(target)) {
      pruned = removeFromObject(target, (k) => target[k]);
    }

    return { ...obj, [key]: pruned }
  }, result);
}

export const falseValues = (target) => {

  if (isObject(target)) {
    return Object
      .keys(target)
      .reduce((result, key) => {
        return { ...result, [key]: false }
      }, {});
  }

  return false;
}

export const buildQueryString = ({ search = {}, pagination = {} } = {}) => {
  // Scrub results and querying from query, not valid filters.
  let query = { ...scrubState(search), ...pagination }

  return Object.keys(query).reduce((q, key) => {
    let target = query[key];
    let params = [];
    if (key === 'keyword') {
      target = encodeURIComponent(target)
    }
{'fraction_non_alphanumeric': 0.16408348832403388, 'fraction_numerical': 0.0030998140111593306, 'mean_word_length': 3.12617220801364, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, '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': '3422342', 'n_tokens_mistral': 1602, 'n_tokens_neox': 1539, 'n_words': 391}