From 89886429e4e68f1de0d27ac6ab82eaf53da53cab Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Tue, 21 Nov 2023 15:55:22 +0900 Subject: [PATCH] Revert "refactor(collections): unify function overloads (#3818)" This reverts commit 835f4f6fe5e0b55eea15460e924645f190d4200d. --- collections/max_by.ts | 22 +++++++++++++++++++++- collections/max_of.ts | 12 +++++++++++- collections/min_by.ts | 22 +++++++++++++++++++++- collections/min_of.ts | 12 +++++++++++- collections/partition.ts | 14 +++++++++++--- collections/sort_by.ts | 26 +++++++++++++++++++++++++- collections/zip.ts | 5 +++-- 7 files changed, 103 insertions(+), 10 deletions(-) diff --git a/collections/max_by.ts b/collections/max_by.ts index 911118e372bc..972f7a1d1b48 100644 --- a/collections/max_by.ts +++ b/collections/max_by.ts @@ -23,7 +23,27 @@ */ export function maxBy( array: Iterable, - selector: (el: T) => number | string | bigint | Date, + selector: (el: T) => number, +): T | undefined; +export function maxBy( + array: Iterable, + selector: (el: T) => string, +): T | undefined; +export function maxBy( + array: Iterable, + selector: (el: T) => bigint, +): T | undefined; +export function maxBy( + array: Iterable, + selector: (el: T) => Date, +): T | undefined; +export function maxBy( + array: Iterable, + selector: + | ((el: T) => number) + | ((el: T) => string) + | ((el: T) => bigint) + | ((el: T) => Date), ): T | undefined { let max: T | undefined = undefined; let maxValue: ReturnType | undefined = undefined; diff --git a/collections/max_of.ts b/collections/max_of.ts index 70ddab9c688f..6004d6403e2c 100644 --- a/collections/max_of.ts +++ b/collections/max_of.ts @@ -22,7 +22,17 @@ * assertEquals(maxCount, 32); * ``` */ -export function maxOf number | bigint>( +export function maxOf( + array: Iterable, + selector: (el: T) => number, +): number | undefined; + +export function maxOf( + array: Iterable, + selector: (el: T) => bigint, +): bigint | undefined; + +export function maxOf number) | ((el: T) => bigint)>( array: Iterable, selector: S, ): ReturnType | undefined { diff --git a/collections/min_by.ts b/collections/min_by.ts index 4d44d6932f12..3bc69e5fc599 100644 --- a/collections/min_by.ts +++ b/collections/min_by.ts @@ -23,7 +23,27 @@ */ export function minBy( array: Iterable, - selector: (el: T) => number | string | bigint | Date, + selector: (el: T) => number, +): T | undefined; +export function minBy( + array: Iterable, + selector: (el: T) => string, +): T | undefined; +export function minBy( + array: Iterable, + selector: (el: T) => bigint, +): T | undefined; +export function minBy( + array: Iterable, + selector: (el: T) => Date, +): T | undefined; +export function minBy( + array: Iterable, + selector: + | ((el: T) => number) + | ((el: T) => string) + | ((el: T) => bigint) + | ((el: T) => Date), ): T | undefined { let min: T | undefined = undefined; let minValue: ReturnType | undefined = undefined; diff --git a/collections/min_of.ts b/collections/min_of.ts index dfde5e850c86..64ae52d10d05 100644 --- a/collections/min_of.ts +++ b/collections/min_of.ts @@ -21,7 +21,17 @@ * assertEquals(minCount, 2); * ``` */ -export function minOf number | bigint>( +export function minOf( + array: Iterable, + selector: (el: T) => number, +): number | undefined; + +export function minOf( + array: Iterable, + selector: (el: T) => bigint, +): bigint | undefined; + +export function minOf number) | ((el: T) => bigint)>( array: Iterable, selector: S, ): ReturnType | undefined { diff --git a/collections/partition.ts b/collections/partition.ts index 7745a150b18a..2888d4f067a9 100644 --- a/collections/partition.ts +++ b/collections/partition.ts @@ -18,12 +18,20 @@ * assertEquals(odd, [5, 7, 9]); * ``` */ +export function partition( + array: Iterable, + predicate: (el: T) => el is U, +): [U[], Exclude[]]; export function partition( array: Iterable, predicate: (el: T) => boolean, -): [T[], T[]] { - const matches = []; - const rest = []; +): [T[], T[]]; +export function partition( + array: Iterable, + predicate: (el: unknown) => boolean, +): [unknown[], unknown[]] { + const matches: Array = []; + const rest: Array = []; for (const element of array) { if (predicate(element)) { diff --git a/collections/sort_by.ts b/collections/sort_by.ts index 3cc682877f6b..d38846da1d86 100644 --- a/collections/sort_by.ts +++ b/collections/sort_by.ts @@ -43,7 +43,31 @@ export type SortByOptions = { */ export function sortBy( array: readonly T[], - selector: (el: T) => number | string | bigint | Date, + selector: (el: T) => number, + options?: SortByOptions, +): T[]; +export function sortBy( + array: readonly T[], + selector: (el: T) => string, + options?: SortByOptions, +): T[]; +export function sortBy( + array: readonly T[], + selector: (el: T) => bigint, + options?: SortByOptions, +): T[]; +export function sortBy( + array: readonly T[], + selector: (el: T) => Date, + options?: SortByOptions, +): T[]; +export function sortBy( + array: readonly T[], + selector: + | ((el: T) => number) + | ((el: T) => string) + | ((el: T) => bigint) + | ((el: T) => Date), options?: SortByOptions, ): T[] { const len = array.length; diff --git a/collections/zip.ts b/collections/zip.ts index c396030909b4..d5ec404330c8 100644 --- a/collections/zip.ts +++ b/collections/zip.ts @@ -1,8 +1,6 @@ // 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. @@ -28,6 +26,9 @@ import { minOf } from "./min_of.ts"; * ); * ``` */ + +import { minOf } from "./min_of.ts"; + export function zip( ...arrays: { [K in keyof T]: ReadonlyArray } ): T[] {