-
Notifications
You must be signed in to change notification settings - Fork 581
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(collections): add minWith (#1169)
- Loading branch information
1 parent
1878a81
commit ca23a1e
Showing
4 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. | ||
|
||
/** | ||
* Returns the first element having the smallest value according to the provided comparator or undefined if there are no elements | ||
* | ||
* Example: | ||
* | ||
* ```ts | ||
* import { minWith } from "./min_with.ts"; | ||
* import { assertEquals } from "../testing/asserts.ts"; | ||
* | ||
* const people = ["Kim", "Anna", "John"]; | ||
* const smallestName = minWith(people, (a, b) => a.length - b.length); | ||
* | ||
* assertEquals(smallestName, "Kim"); | ||
* ``` | ||
*/ | ||
export function minWith<T>( | ||
array: readonly T[], | ||
comparator: (a: T, b: T) => number, | ||
): T | undefined { | ||
let min: T | undefined = undefined; | ||
|
||
for (const current of array) { | ||
if (min === undefined || comparator(current, min) < 0) { | ||
min = current; | ||
} | ||
} | ||
|
||
return min; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { assertEquals } from "../testing/asserts.ts"; | ||
import { minWith } from "./min_with.ts"; | ||
|
||
function minWithTest<T>( | ||
input: [T[], (a: T, b: T) => number], | ||
expected: T | undefined, | ||
message?: string, | ||
) { | ||
const actual = minWith(...input); | ||
assertEquals(actual, expected, message); | ||
} | ||
|
||
Deno.test({ | ||
name: "[collections/minWith] no mutation", | ||
fn() { | ||
const input = [[1, 3], [6, 1, 3], [4]]; | ||
minWith(input, (a, b) => a.length - b.length); | ||
|
||
assertEquals(input, [[1, 3], [6, 1, 3], [4]]); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "[collections/minWith] empty input", | ||
fn() { | ||
minWithTest<string>([[], (a, b) => a.length - b.length], undefined); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "[collections/minWith] array of arrays", | ||
fn() { | ||
minWithTest([[[1, 3], [6, 1, 3], [4]], (a, b) => a.length - b.length], [4]); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "[collections/minWith] array of strings", | ||
fn() { | ||
minWithTest( | ||
[["Kim", "Anna", "John"], (a, b) => a.length - b.length], | ||
"Kim", | ||
); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "[collections/minWith] array of objects", | ||
fn() { | ||
minWithTest( | ||
[ | ||
[ | ||
{ name: "Kim", age: 24 }, | ||
{ name: "Anna", age: 20 }, | ||
{ name: "John", age: 43 }, | ||
], | ||
(a, b) => a.age - b.age, | ||
], | ||
{ name: "Anna", age: 20 }, | ||
); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "[collections/minWith] duplicates", | ||
fn() { | ||
minWithTest([["John", "Kim", "Kim"], (a, b) => a.length - b.length], "Kim"); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters