Skip to content

Commit e1a0977

Browse files
committed
feat(script-sorting): sort alphabetically when higher rules dont apply
1 parent c52dd56 commit e1a0977

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/package/scripts/script-comparator.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ export default function compareScriptNames(a, b) {
4343
return 1;
4444
}
4545

46-
if (aBase.startsWith('lint:')) {
46+
if (aBase.startsWith('lint:') && !bBase.startsWith('lint:')) {
4747
return -1;
4848
}
4949

50-
if (bBase.startsWith('lint:')) {
50+
if (bBase.startsWith('lint:') && !aBase.startsWith('lint:')) {
5151
return 1;
5252
}
5353

54-
return 0;
54+
return a.localeCompare(b);
5555
}

src/package/scripts/script-comparator.test.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ describe('script name comparator', () => {
88
const A_BEFORE_B = -1;
99
const baseScriptName = any.word();
1010

11-
it('should consider undefined sort orders as equivalent', async () => {
12-
expect(compareScriptNames(any.word(), any.word())).toEqual(0);
11+
it('should sort alphabetically if no other rules apply', async () => {
12+
const a = `a${any.word()}`;
13+
const b = `b${any.word()}`;
14+
15+
expect(compareScriptNames(a, b)).toEqual(A_BEFORE_B);
16+
expect(compareScriptNames(b, a)).toEqual(A_AFTER_B);
1317
});
1418

1519
it('should sort `pre` scripts ahead of their related scripts', async () => {
@@ -47,6 +51,17 @@ describe('script name comparator', () => {
4751
expect(compareScriptNames(`pretest:${any.word()}`, `prelint:${any.word()}`)).toEqual(A_AFTER_B);
4852
});
4953

54+
it('should sort subscripts alphabetically', async () => {
55+
const a = `a${any.word()}`;
56+
const b = `b${any.word()}`;
57+
58+
expect(compareScriptNames(`lint:${a}`, `lint:${b}`)).toEqual(A_BEFORE_B);
59+
expect(compareScriptNames(`lint:${b}`, `lint:${a}`)).toEqual(A_AFTER_B);
60+
61+
expect(compareScriptNames(`test:${a}`, `test:${b}`)).toEqual(A_BEFORE_B);
62+
expect(compareScriptNames(`test:${b}`, `test:${a}`)).toEqual(A_AFTER_B);
63+
});
64+
5065
it('should sort undefined scripts below `lint:` scripts', async () => {
5166
expect(compareScriptNames(any.word(), `lint:${any.word()}`)).toEqual(A_AFTER_B);
5267
expect(compareScriptNames(`lint:${any.word()}`, any.word())).toEqual(A_BEFORE_B);

0 commit comments

Comments
 (0)