Skip to content

Commit bc8390f

Browse files
forivallljharb
authored andcommitted
[New] order: add caseInsensitive: 'invert' option
1 parent e1ed323 commit bc8390f

File tree

3 files changed

+67
-5
lines changed

3 files changed

+67
-5
lines changed

docs/rules/order.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,12 @@ import index from './';
211211
import sibling from './foo';
212212
```
213213

214-
### `alphabetize: {order: asc|desc|ignore, caseInsensitive: true|false}`:
214+
### `alphabetize: {order: asc|desc|ignore, caseInsensitive: true|false|'invert'}`:
215215

216216
Sort the order within each group in alphabetical manner based on **import path**:
217217

218218
- `order`: use `asc` to sort in ascending order, and `desc` to sort in descending order (default: `ignore`).
219-
- `caseInsensitive`: use `true` to ignore case, and `false` to consider case (default: `false`).
219+
- `caseInsensitive`: use `true` to ignore case, `false` to consider case, and `'invert'` to sort lowercase before uppercase (default: `false`).
220220

221221
Example setting:
222222
```js

src/rules/order.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,15 @@ function getSorter(ascending) {
265265
}
266266
}
267267

268+
function swapCase(input) {
269+
let result = ''
270+
for (let i = 0; i < input.length; i++) {
271+
const lower = input[i].toLowerCase()
272+
result += input[i] === lower ? input[i].toUpperCase() : lower
273+
}
274+
return result
275+
}
276+
268277
function mutateRanksToAlphabetize(imported, alphabetizeOptions) {
269278
const groupedByRanks = imported.reduce(function(acc, importedItem) {
270279
if (!Array.isArray(acc[importedItem.rank])) {
@@ -277,7 +286,10 @@ function mutateRanksToAlphabetize(imported, alphabetizeOptions) {
277286
const groupRanks = Object.keys(groupedByRanks)
278287

279288
const sorterFn = getSorter(alphabetizeOptions.order === 'asc')
280-
const comparator = alphabetizeOptions.caseInsensitive ? (a, b) => sorterFn(String(a).toLowerCase(), String(b).toLowerCase()) : (a, b) => sorterFn(a, b)
289+
const comparator =
290+
alphabetizeOptions.caseInsensitive === 'invert' ? (a, b) => sorterFn(swapCase(String(a)), swapCase(String(b)))
291+
: alphabetizeOptions.caseInsensitive ? (a, b) => sorterFn(String(a).toLowerCase(), String(b).toLowerCase())
292+
: (a, b) => sorterFn(a, b)
281293
// sort imports locally within their group
282294
groupRanks.forEach(function(groupRank) {
283295
groupedByRanks[groupRank].sort(comparator)
@@ -544,8 +556,10 @@ module.exports = {
544556
type: 'object',
545557
properties: {
546558
caseInsensitive: {
547-
type: 'boolean',
548-
default: false,
559+
anyOf: [
560+
{ type: 'boolean', default: false },
561+
{ type: 'string', enum: ['invert'] },
562+
],
549563
},
550564
order: {
551565
enum: ['ignore', 'asc', 'desc'],

tests/src/rules/order.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2039,6 +2039,54 @@ ruleTester.run('order', rule, {
20392039
message: '`foo` import should occur before import of `Bar`',
20402040
}],
20412041
}),
2042+
// Option alphabetize {order: 'asc': caseInsensitive: 'invert'}
2043+
test({
2044+
code: `
2045+
import a from 'bar';
2046+
import b from 'Foo';
2047+
import c from 'foo';
2048+
2049+
import index from './';
2050+
`,
2051+
output: `
2052+
import a from 'bar';
2053+
import c from 'foo';
2054+
import b from 'Foo';
2055+
2056+
import index from './';
2057+
`,
2058+
options: [{
2059+
groups: ['external', 'index'],
2060+
alphabetize: {order: 'asc', caseInsensitive: 'invert'},
2061+
}],
2062+
errors: [{
2063+
message: '`foo` import should occur before import of `Foo`',
2064+
}],
2065+
}),
2066+
// Option alphabetize {order: 'desc': caseInsensitive: 'invert'}
2067+
test({
2068+
code: `
2069+
import a from 'foo';
2070+
import b from 'Foo';
2071+
import c from 'bar';
2072+
2073+
import index from './';
2074+
`,
2075+
output: `
2076+
import b from 'Foo';
2077+
import a from 'foo';
2078+
import c from 'bar';
2079+
2080+
import index from './';
2081+
`,
2082+
options: [{
2083+
groups: ['external', 'index'],
2084+
alphabetize: {order: 'desc', caseInsensitive: 'invert'},
2085+
}],
2086+
errors: [{
2087+
message: '`Foo` import should occur before import of `foo`',
2088+
}],
2089+
}),
20422090
// Alphabetize with parent paths
20432091
test({
20442092
code: `

0 commit comments

Comments
 (0)