refactor(collections): unify function overloads (#3818)

* refactor(collections): unify function overloads

* partition
This commit is contained in:
Asher Gomez 2023-11-20 11:04:19 +11:00 committed by GitHub
parent 2148aad410
commit 835f4f6fe5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 10 additions and 103 deletions

View File

@ -23,27 +23,7 @@
*/
export function maxBy<T>(
array: Iterable<T>,
selector: (el: T) => number,
): T | undefined;
export function maxBy<T>(
array: Iterable<T>,
selector: (el: T) => string,
): T | undefined;
export function maxBy<T>(
array: Iterable<T>,
selector: (el: T) => bigint,
): T | undefined;
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),
selector: (el: T) => number | string | bigint | Date,
): T | undefined {
let max: T | undefined = undefined;
let maxValue: ReturnType<typeof selector> | undefined = undefined;

View File

@ -22,17 +22,7 @@
* assertEquals(maxCount, 32);
* ```
*/
export function maxOf<T>(
array: Iterable<T>,
selector: (el: T) => number,
): number | undefined;
export function maxOf<T>(
array: Iterable<T>,
selector: (el: T) => bigint,
): bigint | undefined;
export function maxOf<T, S extends ((el: T) => number) | ((el: T) => bigint)>(
export function maxOf<T, S extends (el: T) => number | bigint>(
array: Iterable<T>,
selector: S,
): ReturnType<S> | undefined {

View File

@ -23,27 +23,7 @@
*/
export function minBy<T>(
array: Iterable<T>,
selector: (el: T) => number,
): T | undefined;
export function minBy<T>(
array: Iterable<T>,
selector: (el: T) => string,
): T | undefined;
export function minBy<T>(
array: Iterable<T>,
selector: (el: T) => bigint,
): T | undefined;
export function minBy<T>(
array: Iterable<T>,
selector: (el: T) => Date,
): T | undefined;
export function minBy<T>(
array: Iterable<T>,
selector:
| ((el: T) => number)
| ((el: T) => string)
| ((el: T) => bigint)
| ((el: T) => Date),
selector: (el: T) => number | string | bigint | Date,
): T | undefined {
let min: T | undefined = undefined;
let minValue: ReturnType<typeof selector> | undefined = undefined;

View File

@ -21,17 +21,7 @@
* assertEquals(minCount, 2);
* ```
*/
export function minOf<T>(
array: Iterable<T>,
selector: (el: T) => number,
): number | undefined;
export function minOf<T>(
array: Iterable<T>,
selector: (el: T) => bigint,
): bigint | undefined;
export function minOf<T, S extends ((el: T) => number) | ((el: T) => bigint)>(
export function minOf<T, S extends (el: T) => number | bigint>(
array: Iterable<T>,
selector: S,
): ReturnType<S> | undefined {

View File

@ -18,20 +18,12 @@
* assertEquals(odd, [5, 7, 9]);
* ```
*/
export function partition<T, U extends T>(
array: Iterable<T>,
predicate: (el: T) => el is U,
): [U[], Exclude<T, U>[]];
export function partition<T>(
array: Iterable<T>,
predicate: (el: T) => boolean,
): [T[], T[]];
export function partition(
array: Iterable<unknown>,
predicate: (el: unknown) => boolean,
): [unknown[], unknown[]] {
const matches: Array<unknown> = [];
const rest: Array<unknown> = [];
): [T[], T[]] {
const matches = [];
const rest = [];
for (const element of array) {
if (predicate(element)) {

View File

@ -43,31 +43,7 @@ export type SortByOptions = {
*/
export function sortBy<T>(
array: readonly T[],
selector: (el: T) => number,
options?: SortByOptions,
): T[];
export function sortBy<T>(
array: readonly T[],
selector: (el: T) => string,
options?: SortByOptions,
): T[];
export function sortBy<T>(
array: readonly T[],
selector: (el: T) => bigint,
options?: SortByOptions,
): T[];
export function sortBy<T>(
array: readonly T[],
selector: (el: T) => Date,
options?: SortByOptions,
): T[];
export function sortBy<T>(
array: readonly T[],
selector:
| ((el: T) => number)
| ((el: T) => string)
| ((el: T) => bigint)
| ((el: T) => Date),
selector: (el: T) => number | string | bigint | Date,
options?: SortByOptions,
): T[] {
const len = array.length;

View File

@ -1,6 +1,8 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
import { minOf } from "./min_of.ts";
/**
* Builds N-tuples of elements from the given N arrays with matching indices,
* stopping when the smallest array's end is reached.
@ -26,9 +28,6 @@
* );
* ```
*/
import { minOf } from "./min_of.ts";
export function zip<T extends unknown[]>(
...arrays: { [K in keyof T]: ReadonlyArray<T[K]> }
): T[] {