<commit_before>import LikertScale from "components/graphs/LikertScale"
import PropTypes from "prop-types"
import React from "react"
import { Col, ControlLabel, FormGroup } from "react-bootstrap"

const aggregationPropTypes = {
  values: PropTypes.arrayOf(
    PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.array])
  ),
  aggregationType: PropTypes.string
}

const arrayOfNumbers = arr => arr.map(x => Number(x))

const NUMBER_AGG = {
  sum: arr => arrayOfNumbers(arr).reduce((a, b) => a + b),
  avg: arr => arrayOfNumbers(arr).reduce((a, b) => a + b) / arr.length,
  min: arr => Math.min(...arrayOfNumbers(arr)),
  max: arr => Math.max(...arrayOfNumbers(arr))
}

const NumberAggWidget = ({ values, aggregationType, ...otherWidgetProps }) =>
  values?.length ? <div>{NUMBER_AGG[aggregationType](values)}</div> : null<commit_msg>NCI-Agency/anet#2926: Refactor AggregationWidget number aggregation<commit_after>import LikertScale from "components/graphs/LikertScale"
import PropTypes from "prop-types"
import React from "react"
import { Col, ControlLabel, FormGroup } from "react-bootstrap"

const aggregationPropTypes = {
  values: PropTypes.arrayOf(
    PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.array])
  ),
  aggregationType: PropTypes.string
}

const arrayOfNumbers = arr =>
  arr.filter(n => !isNaN(parseFloat(n)) && isFinite(n)).map(n => Number(n))

const NUMBER_AGG = {
  sum: arr => arr.reduce((a, b) => a + b),
  avg: arr => arr.reduce((a, b) => a + b) / arr.length,
  min: arr => Math.min(...arr),
  max: arr => Math.max(...arr)
}

const NumberAggWidget = ({ values, aggregationType, ...otherWidgetProps }) =>
  values?.length ? (
    <div>{NUMBER_AGG[aggregationType](arrayOfNumbers(values))}</div>
  ) : null
{'fraction_non_alphanumeric': 0.1598639455782313, 'fraction_numerical': 0.0022675736961451248, 'mean_word_length': 4.883333333333334, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 26, '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': '6804835', 'n_tokens_mistral': 596, 'n_tokens_neox': 582, 'n_words': 127}