<commit_before>Constants = require '/imports/constants.coffee'
import { pluralize } from '/imports/ui/helpers'

###
# cleanUrl - takes an existing url and removes the last match of the applied
#   regular expressions.
#
# @param {string} existingUrl, the url to be cleaned
# @param {array} [regexps], (optional) an array of compiled regular expressions
# @returns {string} cleanedUrl, an url that has been cleaned
###
export cleanUrl = (existingUrl, regexps) ->
  regexps = regexps || [new RegExp('^(https?:\/\/)', 'i'), new RegExp('^(www\.)', 'i')]
  cleanedUrl = existingUrl
  regexps.forEach (r) ->
    found = false
    match = cleanedUrl.match(r)
    if match && match.length > 0
      cleanedUrl = cleanedUrl.replace(match[match.length-1], '')
  return cleanedUrl

###
# formatUrl - takes an cleaned url and adds 'http' so that a browser can open
#
# @param {string} existingUrl, the url to be formatted
# @returns {string} formattedUrl, an url that has 'http' added
###
export formatUrl = (existingUrl) ->
  regexp = new RegExp('^(https?:\/\/)', 'i')
  if regexp.test existingUrl
    return existingUrl
  else
    return 'http://' + existingUrl

checkIncidentTypeValue = (form, input) ->
  if not form[input].value.trim()
    messageText = 'count'
    if input is 'specify'
      messageText = 'incident type'
    toastr.error("Please enter a valid #{messageText}.")
    false
  else
    true

export incidentReportFormToIncident = (form) ->
  $form = $(form)
  if $form.find('#singleDate').hasClass('active')
    rangeType = 'day'
    $pickerContainer = $form.find('#singleDatePicker')
  else
    rangeType = 'precise'
    $pickerContainer = $form.find('#rangePicker')

  picker = $pickerContainer.data('daterangepicker')

  incidentType = $form.find('input[name="incidentType"]:checked').val()
  incidentStatus = $form.find('input[name="incidentStatus"]:checked').val()

  incident =
    species: form.species.value
    travelRelated: form.travelRelated.checked
    approximate: form.approximate.checked
    locations: []
    status: incidentStatus
    dateRange:
      type: rangeType
      start: moment.utc(picker.startDate.format("YYYY-MM-DD")).toDate()
      end: moment.utc(picker.endDate.format("YYYY-MM-DD")).toDate()
      cumulative: form.cumulative.checked

  switch incidentType || ''
    when 'cases'
      incident.cases = parseInt(form.count.value, 10)
    when 'deaths'
      incident.deaths = parseInt(form.count.value, 10)
    when 'other'
      incident.specify = form.specify.value.trim()
    else
      toastr.error("Unknown incident type [#{incidentType}]")
      return

  articleSourceUrl = form.articleSourceUrl
  if articleSourceUrl
    incident.url = articleSourceUrl.value
  else
    for child in $(form.articleSource).select2('data')
      if child.selected
        incident.url = child.text.trim()
  for option in $(form).find('#incident-disease-select2').select2('data')
    incident.resolvedDisease =
      id: option.id<commit_msg>Fix incident report form - fix incidentReportFormToIncident<commit_after>Constants = require '/imports/constants.coffee'
Articles = require '/imports/collections/articles.coffee'
import { pluralize } from '/imports/ui/helpers'

###
# cleanUrl - takes an existing url and removes the last match of the applied
#   regular expressions.
#
# @param {string} existingUrl, the url to be cleaned
# @param {array} [regexps], (optional) an array of compiled regular expressions
# @returns {string} cleanedUrl, an url that has been cleaned
###
export cleanUrl = (existingUrl, regexps) ->
  regexps = regexps || [new RegExp('^(https?:\/\/)', 'i'), new RegExp('^(www\.)', 'i')]
  cleanedUrl = existingUrl
  regexps.forEach (r) ->
    found = false
    match = cleanedUrl.match(r)
    if match && match.length > 0
      cleanedUrl = cleanedUrl.replace(match[match.length-1], '')
  return cleanedUrl

###
# formatUrl - takes an cleaned url and adds 'http' so that a browser can open
#
# @param {string} existingUrl, the url to be formatted
# @returns {string} formattedUrl, an url that has 'http' added
###
export formatUrl = (existingUrl) ->
  regexp = new RegExp('^(https?:\/\/)', 'i')
  if regexp.test existingUrl
    return existingUrl
  else
    return 'http://' + existingUrl

checkIncidentTypeValue = (form, input) ->
  if not form[input].value.trim()
    messageText = 'count'
    if input is 'specify'
      messageText = 'incident type'
    toastr.error("Please enter a valid #{messageText}.")
    false
  else
    true

export incidentReportFormToIncident = (form) ->
  $form = $(form)
  if $form.find('#singleDate').hasClass('active')
    rangeType = 'day'
    $pickerContainer = $form.find('#singleDatePicker')
  else
    rangeType = 'precise'
    $pickerContainer = $form.find('#rangePicker')

  picker = $pickerContainer.data('daterangepicker')

  incidentType = $form.find('input[name="incidentType"]:checked').val()
  incidentStatus = $form.find('input[name="incidentStatus"]:checked').val()

  incident =
    species: form.species.value
    travelRelated: form.travelRelated.checked
    approximate: form.approximate.checked
    locations: []
    status: incidentStatus
    dateRange:
      type: rangeType
      start: moment.utc(picker.startDate.format("YYYY-MM-DD")).toDate()
      end: moment.utc(picker.endDate.format("YYYY-MM-DD")).toDate()
      cumulative: form.cumulative.checked

  switch incidentType || ''
    when 'cases'
      incident.cases = parseInt(form.count.value, 10)
    when 'deaths'
      incident.deaths = parseInt(form.count.value, 10)
    when 'other'
      incident.specify = form.specify.value.trim()
    else
      toastr.error("Unknown incident type [#{incidentType}]")
      return

  articleSourceUrl = form.articleSourceUrl
  if articleSourceUrl
    incident.articleId = Articles.findOne(url: articleSourceUrl)._id
  else
    for child in $(form.articleSource).select2('data')
      if child.selected
        incident.articleId = Articles.findOne(url: child.text.trim())._id
  for option in $(form).find('#incident-disease-select2').select2('data')
    incident.resolvedDisease =
      id: option.id
{'fraction_non_alphanumeric': 0.13666121112929625, 'fraction_numerical': 0.0029459901800327334, 'mean_word_length': 3.7704918032786887, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6095932', 'n_tokens_mistral': 1903, 'n_tokens_neox': 1877, 'n_words': 473}