Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(collections): unify function overloads #3818

Merged
merged 2 commits into from
Nov 20, 2023
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
22 changes: 1 addition & 21 deletions collections/max_by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 1 addition & 11 deletions collections/max_of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 1 addition & 21 deletions collections/min_by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 1 addition & 11 deletions collections/min_of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 3 additions & 11 deletions collections/partition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
26 changes: 1 addition & 25 deletions collections/sort_by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions collections/zip.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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[] {
Expand Down