Skip to content

Commit 395da9e

Browse files
committed
Fix types for groupBy shim
1 parent 40d9e8a commit 395da9e

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

src/global.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @todo [-object.groupby] When the `groupBy` method is available natively,
3+
* this declaration can be removed.
4+
*/
5+
declare module 'object.groupby' {
6+
import groupBy from 'object.groupby'
7+
8+
function groupBy<T>(
9+
array: T[],
10+
predicate: (item: T) => string | number,
11+
): Record<string | number, T[]>
12+
13+
export = groupBy
14+
}

src/utils/alphabetize-ranks.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function getSorter(order: OrderDirection) {
6464

6565
result *= multiplier
6666

67-
// In case the paths are equal (result === 0), sort them by importKind
67+
// In case the paths are equal (result === 0), sort them by importKind.
6868
if (!result && multiplierImportKind) {
6969
result =
7070
multiplierImportKind *
@@ -82,24 +82,20 @@ export function mutateRanksToAlphabetize(
8282
imported: ImportNodeObject[],
8383
alphabetizeOptions: OrderDirection,
8484
) {
85-
let groupedByRanks: Record<number, ImportNodeObject[]> = groupBy(
86-
imported,
87-
(item: ImportNodeObject) => item.rank,
88-
)
89-
85+
let groupedByRanks = groupBy(imported, (item) => item.rank)
9086
let sorterFunction = getSorter(alphabetizeOptions)
9187

92-
// Sort group keys so that they can be iterated on in order
88+
// Sort group keys so that they can be iterated on in order.
9389
let groupRanks = Object.keys(groupedByRanks).sort((a, b) => Number(a) - Number(b))
9490

95-
// Sort imports locally within their group
91+
// Sort imports locally within their group.
9692
for (let groupRank of groupRanks) {
9793
groupedByRanks[groupRank].sort(sorterFunction)
9894
}
9995

100-
// Assign globally unique rank to each import
96+
// Assign a globally unique rank to each import.
10197
let newRank = 0
102-
let alphabetizedRanks = groupRanks.reduce((accumulator, groupRank) => {
98+
let alphabetizedRanks = groupRanks.reduce<Record<string, number>>((accumulator, groupRank) => {
10399
for (let importedItem of groupedByRanks[groupRank]) {
104100
accumulator[`${importedItem.value}|${importedItem.node.importKind}`] =
105101
Number.parseInt(groupRank, 10) + newRank
@@ -109,10 +105,9 @@ export function mutateRanksToAlphabetize(
109105
return accumulator
110106
}, {})
111107

112-
// Mutate the original group-rank with alphabetized-rank
113-
// eslint-disable-next-line unicorn/no-array-for-each
114-
imported.forEach((importedItem) => {
108+
// Mutate the original group-rank with alphabetized-rank.
109+
for (let importedItem of imported) {
115110
importedItem.rank =
116111
alphabetizedRanks[`${importedItem.value}|${importedItem.node.importKind}`]
117-
})
112+
}
118113
}

0 commit comments

Comments
 (0)