-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extracts the function used to sort packages as a utility function so it can be reused for sorting locales as well.
- Loading branch information
1 parent
4079228
commit 5f72555
Showing
3 changed files
with
35 additions
and
70 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
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,29 @@ | ||
const sortfn = (a: string, b: string, searchTerm: string) => { | ||
const x = a.toLowerCase(); | ||
const y = b.toLowerCase(); | ||
// check exact match first | ||
if (x === searchTerm) { | ||
return -1; | ||
} | ||
if (y === searchTerm) { | ||
return 1; | ||
} | ||
// check for packages that start with the search term | ||
if (x.startsWith(searchTerm) && !y.startsWith(searchTerm)) { | ||
return -1; | ||
} | ||
if (y.startsWith(searchTerm) && !x.startsWith(searchTerm)) { | ||
return 1; | ||
} | ||
// if both (or neither) start with the search term | ||
// sort alphabetically | ||
if (x < y) { | ||
return -1; | ||
} | ||
if (y < x) { | ||
return 1; | ||
} | ||
return 0; | ||
}; | ||
|
||
export default sortfn; |