2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-09-18 18:29:29 +00:00
|
|
|
// This module is browser compatible.
|
2021-08-31 06:29:18 +00:00
|
|
|
|
|
|
|
/**
|
2022-11-25 11:40:23 +00:00
|
|
|
* Returns the first element that is the largest value of the given function or
|
|
|
|
* undefined if there are no elements.
|
2021-08-31 06:29:18 +00:00
|
|
|
*
|
2024-05-20 07:34:47 +00:00
|
|
|
* @typeParam T The type of the elements in the array.
|
2024-05-06 07:51:20 +00:00
|
|
|
*
|
|
|
|
* @param array The array to find the maximum element in.
|
|
|
|
* @param selector The function to get the value to compare from each element.
|
|
|
|
*
|
|
|
|
* @returns The first element that is the largest value of the given function or
|
|
|
|
* undefined if there are no elements.
|
|
|
|
*
|
|
|
|
* @example Basic usage
|
2021-08-31 06:29:18 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { maxBy } from "@std/collections/max-by";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2021-08-31 06:29:18 +00:00
|
|
|
*
|
|
|
|
* const people = [
|
2022-11-25 11:40:23 +00:00
|
|
|
* { name: "Anna", age: 34 },
|
|
|
|
* { name: "Kim", age: 42 },
|
|
|
|
* { name: "John", age: 23 },
|
2021-08-31 06:29:18 +00:00
|
|
|
* ];
|
|
|
|
*
|
2024-05-06 07:51:20 +00:00
|
|
|
* const personWithMaxAge = maxBy(people, (person) => person.age);
|
2021-08-31 06:29:18 +00:00
|
|
|
*
|
2022-11-25 11:40:23 +00:00
|
|
|
* assertEquals(personWithMaxAge, { name: "Kim", age: 42 });
|
2021-08-31 06:29:18 +00:00
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
export function maxBy<T>(
|
2023-05-30 23:48:21 +00:00
|
|
|
array: Iterable<T>,
|
2023-11-21 12:37:43 +00:00
|
|
|
selector: (el: T) => number,
|
|
|
|
): T | undefined;
|
|
|
|
/**
|
|
|
|
* Returns the first element that is the largest value of the given function or
|
|
|
|
* undefined if there are no elements.
|
|
|
|
*
|
2024-05-20 07:34:47 +00:00
|
|
|
* @typeParam T The type of the elements in the array.
|
2024-05-06 07:51:20 +00:00
|
|
|
*
|
|
|
|
* @param array The array to find the maximum element in.
|
|
|
|
* @param selector The function to get the value to compare from each element.
|
|
|
|
*
|
|
|
|
* @returns The first element that is the largest value of the given function or
|
|
|
|
* undefined if there are no elements.
|
|
|
|
*
|
|
|
|
* @example Basic usage
|
2023-11-21 12:37:43 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { maxBy } from "@std/collections/max-by";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2023-11-21 12:37:43 +00:00
|
|
|
*
|
|
|
|
* const people = [
|
|
|
|
* { name: "Anna" },
|
|
|
|
* { name: "Kim" },
|
|
|
|
* { name: "John" },
|
|
|
|
* ];
|
|
|
|
*
|
2024-05-06 07:51:20 +00:00
|
|
|
* const personWithMaxName = maxBy(people, (person) => person.name);
|
|
|
|
*
|
|
|
|
* assertEquals(personWithMaxName, { name: "Kim" });
|
2023-11-21 12:37:43 +00:00
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
export function maxBy<T>(
|
|
|
|
array: Iterable<T>,
|
|
|
|
selector: (el: T) => string,
|
|
|
|
): T | undefined;
|
|
|
|
/**
|
|
|
|
* Returns the first element that is the largest value of the given function or
|
|
|
|
* undefined if there are no elements.
|
|
|
|
*
|
2024-05-20 07:34:47 +00:00
|
|
|
* @typeParam T The type of the elements in the array.
|
2024-05-06 07:51:20 +00:00
|
|
|
*
|
|
|
|
* @param array The array to find the maximum element in.
|
|
|
|
* @param selector The function to get the value to compare from each element.
|
|
|
|
*
|
|
|
|
* @returns The first element that is the largest value of the given function or
|
|
|
|
* undefined if there are no elements.
|
|
|
|
*
|
|
|
|
* @example Basic usage
|
2023-11-21 12:37:43 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { maxBy } from "@std/collections/max-by";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2023-11-21 12:37:43 +00:00
|
|
|
*
|
|
|
|
* const people = [
|
|
|
|
* { name: "Anna", age: 34n },
|
|
|
|
* { name: "Kim", age: 42n },
|
|
|
|
* { name: "John", age: 23n },
|
|
|
|
* ];
|
|
|
|
*
|
2024-05-06 07:51:20 +00:00
|
|
|
* const personWithMaxAge = maxBy(people, (person) => person.age);
|
2023-11-21 12:37:43 +00:00
|
|
|
*
|
|
|
|
* assertEquals(personWithMaxAge, { name: "Kim", age: 42n });
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
export function maxBy<T>(
|
|
|
|
array: Iterable<T>,
|
|
|
|
selector: (el: T) => bigint,
|
|
|
|
): T | undefined;
|
|
|
|
/**
|
|
|
|
* Returns the first element that is the largest value of the given function or
|
|
|
|
* undefined if there are no elements.
|
|
|
|
*
|
2024-05-20 07:34:47 +00:00
|
|
|
* @typeParam T The type of the elements in the array.
|
2024-05-06 07:51:20 +00:00
|
|
|
*
|
|
|
|
* @param array The array to find the maximum element in.
|
|
|
|
* @param selector The function to get the value to compare from each element.
|
|
|
|
*
|
|
|
|
* @returns The first element that is the largest value of the given function or
|
|
|
|
* undefined if there are no elements.
|
|
|
|
*
|
|
|
|
* @example Basic usage
|
2023-11-21 12:37:43 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { maxBy } from "@std/collections/max-by";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2023-11-21 12:37:43 +00:00
|
|
|
*
|
|
|
|
* const people = [
|
|
|
|
* { name: "Anna", startedAt: new Date("2020-01-01") },
|
|
|
|
* { name: "Kim", startedAt: new Date("2021-03-01") },
|
|
|
|
* { name: "John", startedAt: new Date("2020-03-01") },
|
|
|
|
* ];
|
|
|
|
*
|
2024-05-06 07:51:20 +00:00
|
|
|
* const personWithLastStartedAt = maxBy(people, (person) => person.startedAt);
|
|
|
|
*
|
|
|
|
* assertEquals(personWithLastStartedAt, { name: "Kim", startedAt: new Date("2021-03-01") });
|
2023-11-21 12:37:43 +00:00
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
export function maxBy<T>(
|
|
|
|
array: Iterable<T>,
|
|
|
|
selector: (el: T) => Date,
|
|
|
|
): T | undefined;
|
|
|
|
export function maxBy<T>(
|
|
|
|
array: Iterable<T>,
|
|
|
|
selector:
|
|
|
|
| ((el: T) => number)
|
|
|
|
| ((el: T) => string)
|
|
|
|
| ((el: T) => bigint)
|
|
|
|
| ((el: T) => Date),
|
2021-08-31 06:29:18 +00:00
|
|
|
): T | undefined {
|
2024-05-07 07:11:55 +00:00
|
|
|
let max: T | undefined;
|
|
|
|
let maxValue: ReturnType<typeof selector> | undefined;
|
2021-08-31 06:29:18 +00:00
|
|
|
|
2021-09-21 06:05:51 +00:00
|
|
|
for (const current of array) {
|
2021-08-31 06:29:18 +00:00
|
|
|
const currentValue = selector(current);
|
|
|
|
|
|
|
|
if (maxValue === undefined || currentValue > maxValue) {
|
|
|
|
max = current;
|
|
|
|
maxValue = currentValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return max;
|
|
|
|
}
|