-
Notifications
You must be signed in to change notification settings - Fork 638
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(collections): implement sortBy (#1069)
- Loading branch information
Showing
2 changed files
with
153 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. | ||
|
||
/** | ||
* Returns all elements in the given collection, sorted by their result using the given selector | ||
* | ||
* Example: | ||
* | ||
* ```ts | ||
* import { sortBy } from "./sort_by.ts" | ||
* | ||
* const people = [ | ||
* { name: 'Anna', age: 34 }, | ||
* { name: 'Kim', age: 42 }, | ||
* { name: 'John', age: 23 }, | ||
* ] | ||
* const sortedByAge = sortBy(people, it => it.age) | ||
* | ||
* console.assert(sortedByAge === [ | ||
* { name: 'John', age: 23 }, | ||
* { name: 'Anna', age: 34 }, | ||
* { name: 'Kim', age: 42 }, | ||
* ]) | ||
* ``` | ||
*/ | ||
export function sortBy<T>( | ||
array: Array<T>, | ||
selector: | ||
| ((el: T) => number) | ||
| ((el: T) => string) | ||
| ((el: T) => bigint) | ||
| ((el: T) => Date), | ||
): Array<T> { | ||
const ret = Array.from(array); | ||
|
||
return ret.sort((a, b) => { | ||
const selectedA = selector(a); | ||
const selectedB = selector(b); | ||
|
||
if (selectedA > selectedB) { | ||
return 1; | ||
} | ||
|
||
if (selectedA < selectedB) { | ||
return -1; | ||
} | ||
|
||
return 0; | ||
}); | ||
} |
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,104 @@ | ||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { assertEquals } from "../testing/asserts.ts"; | ||
import { sortBy } from "./sort_by.ts"; | ||
|
||
function sortByTest<T>( | ||
input: [ | ||
Array<T>, | ||
| ((el: T) => number) | ||
| ((el: T) => string) | ||
| ((el: T) => bigint) | ||
| ((el: T) => Date), | ||
], | ||
expected: Array<T>, | ||
message?: string, | ||
) { | ||
const actual = sortBy(...input); | ||
assertEquals(actual, expected, message); | ||
} | ||
|
||
Deno.test({ | ||
name: "[collections/sortBy] no mutation", | ||
fn() { | ||
const array = ["a", "abc", "ba"]; | ||
sortBy(array, (it) => it.length); | ||
|
||
assertEquals(array, ["a", "abc", "ba"]); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "[collections/sortBy] empty input", | ||
fn() { | ||
sortByTest( | ||
[[], () => 5], | ||
[], | ||
); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "[collections/sortBy] identity selector", | ||
fn() { | ||
sortByTest( | ||
[ | ||
[2, 3, 1], | ||
(it) => it, | ||
], | ||
[1, 2, 3], | ||
); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "[collections/sortBy] sortings", | ||
fn() { | ||
const testArray = [ | ||
{ name: "benchmark", stage: 3 }, | ||
{ name: "test", stage: 2 }, | ||
{ name: "build", stage: 1 }, | ||
{ name: "deploy", stage: 4 }, | ||
]; | ||
|
||
sortByTest( | ||
[ | ||
testArray, | ||
(it) => it.stage, | ||
], | ||
[ | ||
{ name: "build", stage: 1 }, | ||
{ name: "test", stage: 2 }, | ||
{ name: "benchmark", stage: 3 }, | ||
{ name: "deploy", stage: 4 }, | ||
], | ||
); | ||
sortByTest( | ||
[ | ||
testArray, | ||
(it) => it.name, | ||
], | ||
[ | ||
{ name: "benchmark", stage: 3 }, | ||
{ name: "build", stage: 1 }, | ||
{ name: "deploy", stage: 4 }, | ||
{ name: "test", stage: 2 }, | ||
], | ||
); | ||
sortByTest( | ||
[ | ||
[ | ||
"February 1, 2022", | ||
"December 17, 1995", | ||
"June 12, 2012", | ||
], | ||
(it) => new Date(it), | ||
], | ||
[ | ||
"December 17, 1995", | ||
"June 12, 2012", | ||
"February 1, 2022", | ||
], | ||
); | ||
}, | ||
}); |