@@ -242,10 +242,20 @@ function makeOutOfOrderReport(context, imported) {
242
242
reportOutOfOrder ( context , imported , outOfOrder , 'before' ) ;
243
243
}
244
244
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 ;
247
254
248
255
return function importsSorter ( importA , importB ) {
256
+ importA = collate ( importA ) ;
257
+ importB = collate ( importB ) ;
258
+
249
259
let result ;
250
260
251
261
if ( importA < importB ) {
@@ -261,6 +271,7 @@ function getSorter(ascending) {
261
271
}
262
272
263
273
function swapCase ( input ) {
274
+ input = String ( input ) ;
264
275
let result = '' ;
265
276
for ( let i = 0 ; i < input . length ; i ++ ) {
266
277
const lower = input [ i ] . toLowerCase ( ) ;
@@ -280,14 +291,10 @@ function mutateRanksToAlphabetize(imported, alphabetizeOptions) {
280
291
281
292
const groupRanks = Object . keys ( groupedByRanks ) ;
282
293
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 ) ;
288
295
// sort imports locally within their group
289
296
groupRanks . forEach ( function ( groupRank ) {
290
- groupedByRanks [ groupRank ] . sort ( comparator ) ;
297
+ groupedByRanks [ groupRank ] . sort ( sorterFn ) ;
291
298
} ) ;
292
299
293
300
// assign globally unique rank to each import
0 commit comments