<commit_before>{CrudConfig, makeSimpleStore, extendConfig} = require './helpers'
_ = require 'underscore'

LOADING = 'loading'
QUERY_START_STRING = '?q='

combineQueries = (multipleUrls) ->
  params = {}
  tags = _.map(multipleUrls, (url) ->
    queryString = decodeURIComponent(url.split(QUERY_START_STRING)[1])
    [param, value] = queryString.split(':')
    params[param] ?= []
    params[param].push(value)

    return value if param is 'tag'
  )
  urlsAndTags = _.object(multipleUrls, tags)
  queryStrings = _.map(params, (values, paramKey) ->
    "#{paramKey}:#{values.join(',')}"
  )
  queryString = queryStrings.join(' ')

  {queryString, urlsAndTags}

getMultipleUrl = (multipleUrls, baseUrl) ->
  {queryString} = combineQueries(multipleUrls)

  "#{baseUrl}?q=#{queryString}"

ReferenceBookExerciseConfig = {
  _toSeparate: {}

  loadMultiple: (multipleUrls, baseUrl) ->
    {queryString, urlsAndTags} = combineQueries(multipleUrls, baseUrl)
    url = "#{baseUrl}?q=#{queryString}"

    @_toSeparate[url] = urlsAndTags

  _loaded: (obj, id) ->
    if @_toSeparate[id]?
      _.each(@_toSeparate[id], (tag, url) =>
        exercise = _.find(obj.items, (item) ->
          item.tags.indexOf(tag) > -1
        )
        exerciseObj =
          items: [exercise]
        @loaded(exerciseObj, url)
      )

      delete @_toSeparate[id]
      @emit('loaded.multiple')
    else
      @emit("loaded.#{id}")

    obj


  exports:
    isQueued: (id) ->
      _.chain(@_toSeparate)
        .find((urlsAndTags) ->
          urlsAndTags[id]?
        )
        .isObject()
        .value()

    getMultipleUrl: (multipleUrls) ->
      baseUrl = _.first(multipleUrls).split(QUERY_START_STRING)[0]
      url = getMultipleUrl(multipleUrls, baseUrl)
      @loadMultiple(multipleUrls, baseUrl) unless @_toSeparate[url]?

      url
}

extendConfig(ReferenceBookExerciseConfig, new CrudConfig())
{actions, store} = makeSimpleStore(ReferenceBookExerciseConfig)
module.exports = {ReferenceBookExerciseActions:actions, ReferenceBookExerciseStore:store}
<commit_msg>format multiple exercise query properly when it comes to double quotes<commit_after>{CrudConfig, makeSimpleStore, extendConfig} = require './helpers'
_ = require 'underscore'

LOADING = 'loading'
QUERY_START_STRING = '?q='

combineQueries = (multipleUrls) ->
  params = {}
  tags = _.map(multipleUrls, (url) ->
    queryString = decodeURIComponent(url.split(QUERY_START_STRING)[1])
    [param, value] = queryString.split(':')
    # trim individual tag of double quotes
    value = value.replace(/^"(.+(?="$))"$/, '$1')
    params[param] ?= []
    params[param].push(value)

    return value if param is 'tag'
  )
  urlsAndTags = _.object(multipleUrls, tags)
  queryStrings = _.map(params, (values, paramKey) ->
    # join values and wrap all values with a single pair of double quotes
    "#{paramKey}:\"#{values.join(',')}\""
  )
  queryString = queryStrings.join(' ')

  {queryString, urlsAndTags}

getMultipleUrl = (multipleUrls, baseUrl) ->
  {queryString} = combineQueries(multipleUrls)

  "#{baseUrl}?q=#{queryString}"

ReferenceBookExerciseConfig = {
  _toSeparate: {}

  loadMultiple: (multipleUrls, baseUrl) ->
    {queryString, urlsAndTags} = combineQueries(multipleUrls, baseUrl)
    url = "#{baseUrl}?q=#{queryString}"

    @_toSeparate[url] = urlsAndTags

  _loaded: (obj, id) ->
    if @_toSeparate[id]?
      _.each(@_toSeparate[id], (tag, url) =>
        exercise = _.find(obj.items, (item) ->
          item.tags.indexOf(tag) > -1
        )
        exerciseObj =
          items: [exercise]
        @loaded(exerciseObj, url)
      )

      delete @_toSeparate[id]
      @emit('loaded.multiple')
    else
      @emit("loaded.#{id}")

    obj


  exports:
    isQueued: (id) ->
      _.chain(@_toSeparate)
        .find((urlsAndTags) ->
          urlsAndTags[id]?
        )
        .isObject()
        .value()

    getMultipleUrl: (multipleUrls) ->
      baseUrl = _.first(multipleUrls).split(QUERY_START_STRING)[0]
      url = getMultipleUrl(multipleUrls, baseUrl)
      @loadMultiple(multipleUrls, baseUrl) unless @_toSeparate[url]?

      url
}

extendConfig(ReferenceBookExerciseConfig, new CrudConfig())
{actions, store} = makeSimpleStore(ReferenceBookExerciseConfig)
module.exports = {ReferenceBookExerciseActions:actions, ReferenceBookExerciseStore:store}

{'fraction_non_alphanumeric': 0.14503464203233257, 'fraction_numerical': 0.0016166281755196305, 'mean_word_length': 3.511458333333333, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '6257403', 'n_tokens_mistral': 1466, 'n_tokens_neox': 1435, 'n_words': 260}