-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: drop javascript-natural-sort dep
Minor problem: localeCompare appears to always interleave capital & lowercase letters. I don't know how to do both numeric-sort AND case-sensitive at the same time using pure locale-compare.
- Loading branch information
Showing
9 changed files
with
109 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { naturalSort } from '..'; | ||
|
||
it('should sort normal things alphabetically', () => { | ||
expect( | ||
['a', 'h', 'b', 'i', 'c', 'd', 'j', 'e', 'k', 'f', 'g'].sort((a, b) => | ||
naturalSort(a, b, { importOrderCaseInsensitive: false }), | ||
), | ||
).toEqual(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']); | ||
}); | ||
|
||
it('should treat capital letters as alphabetically earlier than lowercase if case sensitive (or unspecified)', () => { | ||
expect( | ||
['./ExampleView', './ExamplesList'].sort((a, b) => | ||
naturalSort(a, b, { importOrderCaseInsensitive: false }), | ||
), | ||
).toMatchInlineSnapshot( | ||
['./ExampleView', './ExamplesList'], | ||
` | ||
Array [ | ||
"./ExampleView", | ||
"./ExamplesList", | ||
] | ||
`, | ||
); | ||
expect( | ||
['./ExampleView', './ExamplesList'].sort((a, b) => | ||
naturalSort(a, b, {}), | ||
), | ||
).toMatchInlineSnapshot( | ||
['./ExampleView', './ExamplesList'], | ||
` | ||
Array [ | ||
"./ExampleView", | ||
"./ExamplesList", | ||
] | ||
`, | ||
); | ||
}); | ||
|
||
it('should ignore capitalization differences if case-insensitive', () => { | ||
expect( | ||
['./ExampleView', './ExamplesList'].sort((a, b) => | ||
naturalSort(a, b, { importOrderCaseInsensitive: true }), | ||
), | ||
).toEqual(['./ExamplesList', './ExampleView']); | ||
}); | ||
|
||
it('should sort things numerically', () => { | ||
expect( | ||
['a2', 'a3', 'a10', 'a1', 'a11', 'a9'].sort((a, b) => | ||
naturalSort(a, b, { importOrderCaseInsensitive: true }), | ||
), | ||
).toEqual(['a1', 'a2', 'a3', 'a9', 'a10', 'a11']); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,26 @@ | ||
import naturalSort from 'javascript-natural-sort'; | ||
import type { PrettierOptions } from '../types'; | ||
|
||
export { naturalSort }; | ||
export type NaturalSortOptions = Partial< | ||
Pick<PrettierOptions, 'importOrderCaseInsensitive'> | ||
>; | ||
|
||
export function naturalSort( | ||
a: string, | ||
b: string, | ||
{ importOrderCaseInsensitive }: NaturalSortOptions, | ||
): number { | ||
const left = typeof a === 'string' ? a : String(a); | ||
|
||
if (!importOrderCaseInsensitive) { | ||
return left < b ? -1 : left > b ? 1 : 0; | ||
} | ||
|
||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/Collator#syntax | ||
const sortOptions: Intl.CollatorOptions = { | ||
sensitivity: 'base', | ||
numeric: true, | ||
caseFirst: 'lower', | ||
}; | ||
|
||
return left.localeCompare(b, 'en', sortOptions); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,22 @@ | ||
import { ImportDeclaration } from '@babel/types'; | ||
|
||
import { naturalSort } from '../natural-sort'; | ||
import { NaturalSortOptions, naturalSort } from '../natural-sort'; | ||
|
||
/** | ||
* This function returns import nodes with alphabetically sorted module | ||
* specifiers | ||
* @param node Import declaration node | ||
*/ | ||
export const getSortedImportSpecifiers = (node: ImportDeclaration) => { | ||
export const getSortedImportSpecifiers = ( | ||
node: ImportDeclaration, | ||
options: NaturalSortOptions, | ||
) => { | ||
node.specifiers.sort((a, b) => { | ||
if (a.type !== b.type) { | ||
return a.type === 'ImportDefaultSpecifier' ? -1 : 1; | ||
} | ||
|
||
return naturalSort(a.local.name, b.local.name); | ||
return naturalSort(a.local.name, b.local.name, options); | ||
}); | ||
return node; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters