Skip to content

Commit

Permalink
Revert "refactor(collections): unify function overloads (denoland#3818)"
Browse files Browse the repository at this point in the history
This reverts commit 835f4f6.
  • Loading branch information
kt3k committed Nov 21, 2023
1 parent 998fb62 commit 8988642
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 10 deletions.
22 changes: 21 additions & 1 deletion collections/max_by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,27 @@
*/
export function maxBy<T>(
array: Iterable<T>,
selector: (el: T) => number | string | bigint | Date,
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),
): T | undefined {
let max: T | undefined = undefined;
let maxValue: ReturnType<typeof selector> | undefined = undefined;
Expand Down
12 changes: 11 additions & 1 deletion collections/max_of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,17 @@
* assertEquals(maxCount, 32);
* ```
*/
export function maxOf<T, S extends (el: T) => number | bigint>(
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)>(
array: Iterable<T>,
selector: S,
): ReturnType<S> | undefined {
Expand Down
22 changes: 21 additions & 1 deletion collections/min_by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,27 @@
*/
export function minBy<T>(
array: Iterable<T>,
selector: (el: T) => number | string | bigint | Date,
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),
): T | undefined {
let min: T | undefined = undefined;
let minValue: ReturnType<typeof selector> | undefined = undefined;
Expand Down
12 changes: 11 additions & 1 deletion collections/min_of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,17 @@
* assertEquals(minCount, 2);
* ```
*/
export function minOf<T, S extends (el: T) => number | bigint>(
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)>(
array: Iterable<T>,
selector: S,
): ReturnType<S> | undefined {
Expand Down
14 changes: 11 additions & 3 deletions collections/partition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@
* 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[]] {
const matches = [];
const rest = [];
): [T[], T[]];
export function partition(
array: Iterable<unknown>,
predicate: (el: unknown) => boolean,
): [unknown[], unknown[]] {
const matches: Array<unknown> = [];
const rest: Array<unknown> = [];

for (const element of array) {
if (predicate(element)) {
Expand Down
26 changes: 25 additions & 1 deletion collections/sort_by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,31 @@ export type SortByOptions = {
*/
export function sortBy<T>(
array: readonly T[],
selector: (el: T) => number | string | bigint | Date,
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),
options?: SortByOptions,
): T[] {
const len = array.length;
Expand Down
5 changes: 3 additions & 2 deletions collections/zip.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -28,6 +26,9 @@ import { minOf } from "./min_of.ts";
* );
* ```
*/

import { minOf } from "./min_of.ts";

export function zip<T extends unknown[]>(
...arrays: { [K in keyof T]: ReadonlyArray<T[K]> }
): T[] {
Expand Down

0 comments on commit 8988642

Please sign in to comment.