Skip to content

Commit 5b68e4f

Browse files
committed
[Refactor] move collation normalization into getSorter()
1 parent dbb6506 commit 5b68e4f

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/rules/order.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,18 @@ function makeOutOfOrderReport(context, imported) {
242242
reportOutOfOrder(context, imported, outOfOrder, 'before');
243243
}
244244

245-
function getSorter(ascending) {
246-
const multiplier = ascending ? 1 : -1;
245+
function getSorter(ascending, caseInsensitive) {
246+
const multiplier = (ascending ? 1 : -1);
247+
const collate = caseInsensitive
248+
? caseInsensitive === 'invert'
249+
? swapCase
250+
: (s) => String(s).toLowerCase()
251+
: (s) => s;
247252

248253
return function importsSorter(importA, importB) {
254+
importA = collate(importA);
255+
importB = collate(importB);
256+
249257
let result;
250258

251259
if (importA < importB) {
@@ -281,13 +289,9 @@ function mutateRanksToAlphabetize(imported, alphabetizeOptions) {
281289
const groupRanks = Object.keys(groupedByRanks);
282290

283291
const sorterFn = getSorter(alphabetizeOptions.order === 'asc');
284-
const comparator =
285-
alphabetizeOptions.caseInsensitive === 'invert' ? (a, b) => sorterFn(swapCase(String(a)), swapCase(String(b)))
286-
: alphabetizeOptions.caseInsensitive ? (a, b) => sorterFn(String(a).toLowerCase(), String(b).toLowerCase())
287-
: (a, b) => sorterFn(a, b);
288292
// sort imports locally within their group
289293
groupRanks.forEach(function(groupRank) {
290-
groupedByRanks[groupRank].sort(comparator);
294+
groupedByRanks[groupRank].sort(sorterFn);
291295
});
292296

293297
// assign globally unique rank to each import

0 commit comments

Comments
 (0)