Skip to content

Commit 3e1d204

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

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/rules/order.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,20 @@ 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(alphabetizeOptions) {
246+
const multiplier = alphabetizeOptions.order === 'asc' ? 1 : -1;
247+
const caseInsensitive = alphabetizeOptions.caseInsensitive;
248+
const collate =
249+
caseInsensitive === 'invert'
250+
? swapCase
251+
: caseInsensitive
252+
? (s) => String(s).toLowerCase()
253+
: (s) => s;
247254

248255
return function importsSorter(importA, importB) {
256+
importA = collate(importA);
257+
importB = collate(importB);
258+
249259
let result;
250260

251261
if (importA < importB) {
@@ -261,6 +271,7 @@ function getSorter(ascending) {
261271
}
262272

263273
function swapCase(input) {
274+
input = String(input);
264275
let result = '';
265276
for (let i = 0; i < input.length; i++) {
266277
const lower = input[i].toLowerCase();
@@ -280,14 +291,10 @@ function mutateRanksToAlphabetize(imported, alphabetizeOptions) {
280291

281292
const groupRanks = Object.keys(groupedByRanks);
282293

283-
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);
294+
const sorterFn = getSorter(alphabetizeOptions);
288295
// sort imports locally within their group
289296
groupRanks.forEach(function(groupRank) {
290-
groupedByRanks[groupRank].sort(comparator);
297+
groupedByRanks[groupRank].sort(sorterFn);
291298
});
292299

293300
// assign globally unique rank to each import

0 commit comments

Comments
 (0)