<commit_before>/**
 * Utility function averageObjectListValue
 *
 * Site Kit by Google, Copyright 2020 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Internal dependencies
 */
import sumObjectListValue from './sum-object-list-value.js';

/**
 * Returns the average of a given fieldName in a list.
 *
 * @since n.e.x.t
 *
 * @param {Array}  list      Array of objects or arrays.
 * @param {string} fieldName The name of the field to be averaged.
 *
 * @return {number} The average.
 */
export default function averageObjectListValue( list, fieldName ) {
	// Filter the list to remove any entries that don't have fieldName so our length is correct for division.
	const filteredList = list.filter( ( item ) => {
		return Object.keys( item ).includes( fieldName );
	} );

	const allIntegers = filteredList.every( ( item ) => {
		return Number.isInteger( item[ fieldName ] );
	} );

	// Ensure that we don't divide empty arrays by 0.
	const average = filteredList.length ? sumObjectListValue( list, fieldName ) / filteredList.length : 0;

	if ( average > 0 && allIntegers ) {
		return Math.round( average );
	}<commit_msg>Update averageObjectListValue to not filter items.<commit_after>/**
 * Utility function averageObjectListValue
 *
 * Site Kit by Google, Copyright 2020 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * External dependencies
 */
import get from 'lodash/get';
import mean from 'lodash/mean';

/**
 * Returns the average of a given fieldName in a list.
 *
 * @since n.e.x.t
 *
 * @param {Array}  list      Array of objects or arrays.
 * @param {string} fieldName The name of the field to be averaged.
 *
 * @return {number} The average.
 */
export default function averageObjectListValue( list, fieldName ) {
	if ( ! list?.length ) {
		return 0;
	}

	const values = list.map( ( item ) => get( item, fieldName, 0 ) );
	const average = mean( values );

	if ( Number.isInteger( average ) ) {
		return average;
	}

	if ( values.every( Number.isInteger ) ) {
		return Math.round( average );
	}
{'fraction_non_alphanumeric': 0.09872922776148582, 'fraction_numerical': 0.006842619745845552, 'mean_word_length': 3.528023598820059, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '2755597', 'n_tokens_mistral': 876, 'n_tokens_neox': 837, 'n_words': 369}