@@ -242,10 +242,18 @@ 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
+ let collate ;
248
+ if ( alphabetizeOptions . caseInsensitive ) {
249
+ if ( alphabetizeOptions . caseFirst === 'lower' ) {
250
+ collate = swapCase ;
251
+ } else {
252
+ collate = ( s ) => String ( s ) . toLowerCase ( ) ;
253
+ }
254
+ }
247
255
248
- return function importsSorter ( importA , importB ) {
256
+ function importsSorter ( importA , importB ) {
249
257
let result ;
250
258
251
259
if ( importA < importB ) {
@@ -257,10 +265,12 @@ function getSorter(ascending) {
257
265
}
258
266
259
267
return result * multiplier ;
260
- } ;
268
+ }
269
+ return collate ? ( a , b ) => importsSorter ( collate ( a ) , collate ( b ) ) : importsSorter ;
261
270
}
262
271
263
272
function swapCase ( input ) {
273
+ input = String ( input ) ;
264
274
let result = '' ;
265
275
for ( let i = 0 ; i < input . length ; i ++ ) {
266
276
const lower = input [ i ] . toLowerCase ( ) ;
@@ -280,14 +290,10 @@ function mutateRanksToAlphabetize(imported, alphabetizeOptions) {
280
290
281
291
const groupRanks = Object . keys ( groupedByRanks ) ;
282
292
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 ) ;
293
+ const sorterFn = getSorter ( alphabetizeOptions ) ;
288
294
// sort imports locally within their group
289
295
groupRanks . forEach ( function ( groupRank ) {
290
- groupedByRanks [ groupRank ] . sort ( comparator ) ;
296
+ groupedByRanks [ groupRank ] . sort ( sorterFn ) ;
291
297
} ) ;
292
298
293
299
// assign globally unique rank to each import
0 commit comments