-
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 (#28)
This PR removes our reliance on natural-sort, an [npm package last updated 9 years ago!](https://www.npmjs.com/package/javascript-natural-sort) Instead this uses [`String.localeCompare(…)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare) which is a built-in method supported on all Node versions we target. Further this is a standard String api, so some shim implementation will be present even if the user isn't running on Node! (eg. `deno` or other runtimes) Co-authored-by: Ian VanSchooten <ian.vanschooten@gmail.com>
- Loading branch information
Showing
6 changed files
with
37 additions
and
17 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,26 @@ | ||
import { expect, test } from 'vitest'; | ||
|
||
import { naturalSort } from '..'; | ||
|
||
test('should sort normal things alphabetically', () => { | ||
expect( | ||
['a', 'h', 'b', 'i', 'c', 'd', 'j', 'e', 'k', 'f', 'g'].sort((a, b) => | ||
naturalSort(a, b), | ||
), | ||
).toEqual(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']); | ||
}); | ||
|
||
test('should ignore capitalization differences', () => { | ||
// We have no option to cause case-sensitive sorting, so this is the "default" case! | ||
expect( | ||
['./ExampleView', './ExamplesList'].sort((a, b) => naturalSort(a, b)), | ||
).toEqual(['./ExamplesList', './ExampleView']); | ||
}); | ||
|
||
test('should sort things numerically', () => { | ||
expect( | ||
['a2', 'a3', 'a10', 'a1', 'a11', 'a9'].sort((a, b) => | ||
naturalSort(a, b), | ||
), | ||
).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,12 @@ | ||
import naturalSort from 'javascript-natural-sort'; | ||
export function naturalSort(a: string, b: string): number { | ||
const left = typeof a === 'string' ? a : String(a); | ||
|
||
export { naturalSort }; | ||
// 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