Skip to content

Commit dbb6506

Browse files
committed
[New] order: add caseInsensitive: 'invert' option
1 parent e871a9a commit dbb6506

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
@@ -216,12 +216,12 @@ import index from './';
216216
import sibling from './foo';
217217
```
218218

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

221221
Sort the order within each group in alphabetical manner based on **import path**:
222222

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

226226
Example setting:
227227
```js

src/rules/order.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,15 @@ function getSorter(ascending) {
260260
};
261261
}
262262

263+
function swapCase(input) {
264+
let result = '';
265+
for (let i = 0; i < input.length; i++) {
266+
const lower = input[i].toLowerCase();
267+
result += input[i] === lower ? input[i].toUpperCase() : lower;
268+
}
269+
return result;
270+
}
271+
263272
function mutateRanksToAlphabetize(imported, alphabetizeOptions) {
264273
const groupedByRanks = imported.reduce(function(acc, importedItem) {
265274
if (!Array.isArray(acc[importedItem.rank])) {
@@ -272,7 +281,10 @@ function mutateRanksToAlphabetize(imported, alphabetizeOptions) {
272281
const groupRanks = Object.keys(groupedByRanks);
273282

274283
const sorterFn = getSorter(alphabetizeOptions.order === 'asc');
275-
const comparator = alphabetizeOptions.caseInsensitive ? (a, b) => sorterFn(String(a).toLowerCase(), String(b).toLowerCase()) : (a, b) => sorterFn(a, b);
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);
276288
// sort imports locally within their group
277289
groupRanks.forEach(function(groupRank) {
278290
groupedByRanks[groupRank].sort(comparator);
@@ -556,8 +568,10 @@ module.exports = {
556568
type: 'object',
557569
properties: {
558570
caseInsensitive: {
559-
type: 'boolean',
560-
default: false,
571+
anyOf: [
572+
{ type: 'boolean', default: false },
573+
{ type: 'string', enum: ['invert'] },
574+
],
561575
},
562576
order: {
563577
enum: ['ignore', 'asc', 'desc'],

tests/src/rules/order.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2121,6 +2121,54 @@ ruleTester.run('order', rule, {
21212121
message: '`foo` import should occur before import of `Bar`',
21222122
}],
21232123
}),
2124+
// Option alphabetize {order: 'asc': caseInsensitive: 'invert'}
2125+
test({
2126+
code: `
2127+
import a from 'bar';
2128+
import b from 'Foo';
2129+
import c from 'foo';
2130+
2131+
import index from './';
2132+
`,
2133+
output: `
2134+
import a from 'bar';
2135+
import c from 'foo';
2136+
import b from 'Foo';
2137+
2138+
import index from './';
2139+
`,
2140+
options: [{
2141+
groups: ['external', 'index'],
2142+
alphabetize: { order: 'asc', caseInsensitive: 'invert' },
2143+
}],
2144+
errors: [{
2145+
message: '`foo` import should occur before import of `Foo`',
2146+
}],
2147+
}),
2148+
// Option alphabetize {order: 'desc': caseInsensitive: 'invert'}
2149+
test({
2150+
code: `
2151+
import a from 'foo';
2152+
import b from 'Foo';
2153+
import c from 'bar';
2154+
2155+
import index from './';
2156+
`,
2157+
output: `
2158+
import b from 'Foo';
2159+
import a from 'foo';
2160+
import c from 'bar';
2161+
2162+
import index from './';
2163+
`,
2164+
options: [{
2165+
groups: ['external', 'index'],
2166+
alphabetize: { order: 'desc', caseInsensitive: 'invert' },
2167+
}],
2168+
errors: [{
2169+
message: '`Foo` import should occur before import of `foo`',
2170+
}],
2171+
}),
21242172
// Alphabetize with parent paths
21252173
test({
21262174
code: `

0 commit comments

Comments
 (0)