<commit_before>import currencyFormatter from 'currency-formatter'
import yup from 'yup'

export function actUponConfirmation (confirmationMessage, action) {
  if (window.confirm(confirmationMessage)) {
    action()
  }
}

export function arrayCountRenderer (cell, row) {
  if (Array.isArray(cell)) return cell.length
}

/**
 * Calculate number of elements in array less than or equal to target values
 *
 * @param  {Number[]} arr     A sorted array of numbers
 * @param  {[Number]} target  The number to be less than or equal to
 * @return {int}              The number of elements in the array less than or equal to the target value
 */
export function calcNumLessThan (arr, target) {
  // do binary search
  let left = 0
  let right = arr.length - 1
  let mid

  while (left <= right) {
    mid = Math.floor((left + right) / 2)
    if (arr[mid] <= target) {
      left = mid + 1
    } else {
      right = mid - 1
    }
  }

  return left
}

export function capitalize (s) {
  return s.charAt(0).toUpperCase() + s.slice(1)
}

/**
 * Round a number to a fixed amount of decimal places
 *
 * @param  {Number} n      The number to round
 * @param  {Number} places The number of places to round to (default=2)
 * @return {Number}        The rounded number
 */
export function fixedRound (n, places) {
  places = places || 2
  const multiplier = Math.pow(10, places)
  return Math.round(n * multiplier) / multiplier
}

export function formatCurrency (n) {
  return currencyFormatter.format(n, { code: 'USD' })
}

export function formatDistance (cell, row) {
  return humanizeDistance(cell)
}

export function formatPercent (n) {
  return Math.round(n * 100)
}

export function formatPercentAsStr (n) {
  return `${formatPercent(n)}%`
}

export const geocodeResultToState = {
  address: result => result ? result.properties.label : undefined,
  city: result => result ? result.properties.locality : undefined,
  coordinate: result => {
    return result ? {
      lat: result.geometry.coordinates[1],
      lon: result.geometry.coordinates[0]
    } : undefined
  },
  country: result => result ? result.properties.country : undefined,
  county: result => result ? result.properties.county : undefined,
  geocodeConfidence: result => result ? result.properties.confidence : undefined,
  neighborhood: result => result ? result.properties.neighborhood : undefined,
  state: result => result ? result.properties.region : undefined
}

export const geocodeYupSchema = {
  address: yup.string().label('Address').required(),
  city: yup.string(),
  coordinate: yup.object({
    lon: yup.number().required(),
    lat: yup.number().required()
  }),
  country: yup.string(),
  county: yup.string(),
  geocodeConfidence: yup.number(),
  neighborhood: yup.string(),
  original_address: yup.string(),
  state: yup.string().required()
}

/**
 * Humanize distances
 * // TODO: more humanization
 *
 * @param  {float} d    Distance to humanize
 * @param  {int} places number of decimal places to round to
 * @return {string}     humanized distance
 */
export function humanizeDistance (d, places) {
  places = places || 1
  return `${fixedRound(d, places)} miles`
}
<commit_msg>refactor(geocode): add temp hack for geocoding<commit_after>import currencyFormatter from 'currency-formatter'
import yup from 'yup'

export function actUponConfirmation (confirmationMessage, action) {
  if (window.confirm(confirmationMessage)) {
    action()
  }
}

export function arrayCountRenderer (cell, row) {
  if (Array.isArray(cell)) return cell.length
}

/**
 * Calculate number of elements in array less than or equal to target values
 *
 * @param  {Number[]} arr     A sorted array of numbers
 * @param  {[Number]} target  The number to be less than or equal to
 * @return {int}              The number of elements in the array less than or equal to the target value
 */
export function calcNumLessThan (arr, target) {
  // do binary search
  let left = 0
  let right = arr.length - 1
  let mid

  while (left <= right) {
    mid = Math.floor((left + right) / 2)
    if (arr[mid] <= target) {
      left = mid + 1
    } else {
      right = mid - 1
    }
  }

  return left
}

export function capitalize (s) {
  return s.charAt(0).toUpperCase() + s.slice(1)
}

/**
 * Round a number to a fixed amount of decimal places
 *
 * @param  {Number} n      The number to round
 * @param  {Number} places The number of places to round to (default=2)
 * @return {Number}        The rounded number
 */
export function fixedRound (n, places) {
  places = places || 2
  const multiplier = Math.pow(10, places)
  return Math.round(n * multiplier) / multiplier
}

export function formatCurrency (n) {
  return currencyFormatter.format(n, { code: 'USD' })
}

export function formatDistance (cell, row) {
  return humanizeDistance(cell)
}

export function formatPercent (n) {
  return Math.round(n * 100)
}

export function formatPercentAsStr (n) {
  return `${formatPercent(n)}%`
}

export const geocodeResultToState = {
  address: result => (result ? result.properties.label : undefined),
  city: result => (result ? result.properties.locality : undefined),
  coordinate: result =>
    result
      ? {
        lat: result.geometry.coordinates[1],
        lon: result.geometry.coordinates[0]
      }
      : undefined,
  country: result => (result ? result.properties.country : undefined),
  county: result => (result ? result.properties.county : undefined),
  geocodeConfidence: result =>
    result
      ? result.properties.hasOwnProperty('confidence')
        ? result.properties.confidence
        : 0.2 // this is a stopgap because mapzen's geocoder isn't returning the confidence property in certain cases
      : undefined,
  neighborhood: result => (result ? result.properties.neighborhood : undefined),
  state: result => (result ? result.properties.region : undefined)
}

export const geocodeYupSchema = {
  address: yup.string().label('Address').required(),
  city: yup.string(),
  coordinate: yup.object({
    lon: yup.number().required(),
    lat: yup.number().required()
  }),
  country: yup.string(),
  county: yup.string(),
  geocodeConfidence: yup.number(),
  neighborhood: yup.string(),
  original_address: yup.string(),
  state: yup.string().required()
}

/**
 * Humanize distances
 * // TODO: more humanization
 *
 * @param  {float} d    Distance to humanize
 * @param  {int} places number of decimal places to round to
 * @return {string}     humanized distance
 */
export function humanizeDistance (d, places) {
  places = places || 1
  return `${fixedRound(d, places)} miles`
}

{'fraction_non_alphanumeric': 0.12048192771084337, 'fraction_numerical': 0.005490315693152357, 'mean_word_length': 3.5103163686382395, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, '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': '1163943', 'n_tokens_mistral': 1984, 'n_tokens_neox': 1947, 'n_words': 629}