Skip to content
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 __tests__/max-by-property.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { maxByProperty } from "@extremejs/utils";

it("should return the maximum of the elements provided by the value at the specified property", () => {
expect(maxByProperty([{ a: 1 }, { a: 2 }, { a: 3 }], "a")).toBe(3);

expect(maxByProperty([{ a: { b: 1 } }, { a: { b: 2 } }, { a: { b: 3 } }], "a.b"))
.toBe(3);

expect(maxByProperty([{ a: { b: 8 } }, { a: {} }, { }], "a.b"))
.toBe(8);
});
2 changes: 1 addition & 1 deletion __tests__/min-by-propery.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { minByProperty } from "@extremejs/utils";

it("should return the mininum of the elements provided by the value at the specified property", () => {
it("should return the minimum of the elements provided by the value at the specified property", () => {
expect(minByProperty([{ a: 1 }, { a: 2 }, { a: 3 }], "a")).toBe(1);

expect(minByProperty([{ a: { b: 1 } }, { a: { b: 2 } }, { a: { b: 3 } }], "a.b"))
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export { default as lowerCase } from "./lower-case.js";
export { default as lowerFirst } from "./lower-first.js";
export { default as max } from "./max.js";
export { default as maxByFn } from "./max-by-fn.js";
export { default as maxByProperty } from "./max-by-property.js";
export { default as mean } from "./mean.js";
export { default as meanByFn } from "./mean-by-fn.js";
export { default as meanByProperty } from "./mean-by-property.js";
Expand Down
21 changes: 21 additions & 0 deletions src/max-by-property.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import get, { type CalculatedPathT, type CalculatedPropertyT } from "./get.js";
import { type RecordT } from "./internals/index.js";
import maxByFn from "./max-by-fn.js";

/**
* Computes the `maximum` of the values of the `property` for each element in the array.
* @group Math
* @since 1.0.0
* @param values The array to iterate over.
* @param property The property to get the value per element.
* @returns The maximum value.
* @example
* minByProperty([{ a: 1 }, { a: 2 }, { a: 3 }], "a");
* // => 3
*/
export default function maxByProperty<Value extends RecordT>(
values: Value[],
property: CalculatedPropertyT<CalculatedPathT<Value>>,
): number {
return maxByFn(values, value => get(value, property, -Infinity as never));
}