Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(compat): implement meanBy #1081

Merged
merged 2 commits into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions benchmarks/performance/meanBy.bench.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { bench, describe } from 'vitest';
import { meanBy as meanByToolkit_ } from 'es-toolkit';
import { meanBy as meanByToolkitCompat_ } from 'es-toolkit/compat';
import { meanBy as meanByLodash_ } from 'lodash';

const meanByToolkit = meanByToolkit_;
const meanByToolkitCompat = meanByToolkitCompat_;
const meanByLodash = meanByLodash_;

describe('meanBy', () => {
Expand All @@ -11,6 +13,11 @@ describe('meanBy', () => {
meanByToolkit(items, x => x.a);
});

bench('es-toolkit/compat/meanBy', () => {
const items = [{ a: 1 }, { a: 2 }, { a: 3 }];
meanByToolkitCompat(items, x => x.a);
});

bench('lodash/meanBy', () => {
const items = [{ a: 1 }, { a: 2 }, { a: 3 }];
meanByLodash(items, x => x.a);
Expand All @@ -24,6 +31,10 @@ describe('meanBy/largeArray', () => {
meanByToolkit(largeArray, x => x.a);
});

bench('es-toolkit/compat/meanBy', () => {
meanByToolkitCompat(largeArray, x => x.a);
});

bench('lodash/meanBy', () => {
meanByLodash(largeArray, x => x.a);
});
Expand Down
1 change: 1 addition & 0 deletions src/compat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export { inRange } from './math/inRange.ts';
export { max } from './math/max.ts';
export { maxBy } from './math/maxBy.ts';
export { mean } from './math/mean.ts';
export { meanBy } from './math/meanBy.ts';
export { min } from './math/min.ts';
export { minBy } from './math/minBy.ts';
export { multiply } from './math/multiply.ts';
Expand Down
30 changes: 30 additions & 0 deletions src/compat/math/meanBy.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { describe, expect, it } from 'vitest';
import { meanBy } from './meanBy';
import { slice } from '../_internal/slice';

describe('meanBy', () => {
const objects = [{ a: 2 }, { a: 3 }, { a: 1 }];

it('should work with an `iteratee`', () => {
const actual = meanBy(objects, object => object.a);

expect(actual).toEqual(2);
});

it('should provide correct `iteratee` arguments', () => {
let args: any;

// @ts-expect-error - invalid args
meanBy(objects, function () {
args || (args = slice.call(arguments));
});

expect(args).toEqual([{ a: 2 }]);
});

it('should work with `_.property` shorthands', () => {
const arrays = [[2], [3], [1]];
expect(meanBy(arrays, 0)).toBe(2);
expect(meanBy(objects, 'a')).toBe(2);
});
});
35 changes: 35 additions & 0 deletions src/compat/math/meanBy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { meanBy as meanByToolkit } from '../../math/meanBy.ts';
import { iteratee as iterateeToolkit } from '../util/iteratee.ts';

/**
* Calculates the average of an array of numbers when applying
* the `iteratee` function to each element.
*
* If the array is empty, this function returns `NaN`.
*
* @template T - The type of elements in the array.
* @param {T[]} items An array to calculate the average.
* @param {((element: T) => number) | keyof T | [keyof T, unknown] | Partial<T>} iteratee
* The criteria used to determine the maximum value.
* - If a **function** is provided, it extracts a numeric value from each element.
* - If a **string** is provided, it is treated as a key to extract values from the objects.
* - If a **[key, value]** pair is provided, it matches elements with the specified key-value pair.
* - If an **object** is provided, it matches elements that contain the specified properties.
* @returns {number} The average of all the numbers as determined by the `iteratee` function.
*
* @example
* meanBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a); // Returns: 2
* meanBy([], x => x.a); // Returns: NaN
* meanBy([[2], [3], [1]], 0); // Returns: 2
* meanBy([{ a: 2 }, { a: 3 }, { a: 1 }], 'a'); // Returns: 2
*/
export function meanBy<T>(
items: ArrayLike<T> | null | undefined,
iteratee: ((element: T) => number) | keyof T | [keyof T, unknown] | Partial<T>
): number {
if (items == null) {
return NaN;
}

return meanByToolkit(Array.from(items), iterateeToolkit(iteratee));
}