Skip to content

Commit

Permalink
imp: merged mutateRanksToAlphabetizeV2 to original
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrewer5 committed Dec 27, 2019
1 parent 2dc4100 commit 23be85c
Showing 1 changed file with 2 additions and 35 deletions.
37 changes: 2 additions & 35 deletions src/rules/order.js
Expand Up @@ -267,7 +267,7 @@ function importsSorterDesc(importA, importB) {
return 0
}

function mutateRanksToAlphabetizeV2(imported, alphabetizeOptions) {
function mutateRanksToAlphabetize(imported, alphabetizeOptions) {
const groupedByRanks = imported.reduce(function(acc, importedItem) {
if (!Array.isArray(acc[importedItem.rank])) {
acc[importedItem.rank] = []
Expand Down Expand Up @@ -304,39 +304,6 @@ function mutateRanksToAlphabetizeV2(imported, alphabetizeOptions) {
})
}

function mutateRanksToAlphabetize(imported, order) {
const groupedByRanks = imported.reduce(function(acc, importedItem) {
if (!Array.isArray(acc[importedItem.rank])) {
acc[importedItem.rank] = []
}
acc[importedItem.rank].push(importedItem.name)
return acc
}, {})

const groupRanks = Object.keys(groupedByRanks)

const sorterFn = order === 'asc' ? importsSorterAsc : importsSorterDesc
// sort imports locally within their group
groupRanks.forEach(function(groupRank) {
groupedByRanks[groupRank].sort(sorterFn)
})

// assign globally unique rank to each import
let newRank = 0
const alphabetizedRanks = groupRanks.sort().reduce(function(acc, groupRank) {
groupedByRanks[groupRank].forEach(function(importedItemName) {
acc[importedItemName] = newRank
newRank += 1
})
return acc
}, {})

// mutate the original group-rank with alphabetized-rank
imported.forEach(function(importedItem) {
importedItem.rank = alphabetizedRanks[importedItem.name]
})
}

// DETECTING

function computePathRank(ranks, pathGroups, path, maxPosition) {
Expand Down Expand Up @@ -646,7 +613,7 @@ module.exports = {
}

if (alphabetize.order !== 'ignore') {
mutateRanksToAlphabetizeV2(imported, alphabetize)
mutateRanksToAlphabetize(imported, alphabetize)
}

makeOutOfOrderReport(context, imported)
Expand Down

0 comments on commit 23be85c

Please sign in to comment.