Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Support case variants for insensitive lexical sort #189

Merged
merged 3 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prettier-plugin-sort-json",
"version": "3.1.0",
"version": "3.1.1",
"description": "Prettier plugin to sort JSON files alphanumerically by key",
"repository": {
"type": "git",
Expand Down
52 changes: 52 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,58 @@ test('should sort an unsorted JSON object with a complex custom sort', async (t)
t.snapshot(output);
});

test('should sort an unsorted JSON object with complex case sensitivies, preferring determinsistic order', async (t) => {
const fixture = {
c: 6,
a: 2,
C: 5,
B: 3,
b: 4,
A: 1,
};

const input = JSON.stringify(fixture, null, 2);
const output = await format(input, {
filepath: 'foo.json',
parser: 'json',
plugins: [SortJsonPlugin],
...{
jsonSortOrder: JSON.stringify({
'/(.+)/': 'caseInsensitiveLexical',
}),
},
});

t.snapshot(output);
});

test('should sort an unsorted JSON object with complex case sensitivies, preferring original sort order', async (t) => {
const fixture = {
c: 6,
a: 2,
C: 5,
B: 3,
b: 4,
A: 1,
};

const input = JSON.stringify(fixture, null, 2);
const output = await format(input, {
filepath: 'foo.json',
parser: 'json',
plugins: [SortJsonPlugin],
...{
jsonSortOrder: JSON.stringify({
'/^[Aa]/': 'none',
'/^[Bb]/': 'none',
'/^[Cc]/': 'none',
}),
},
});

t.snapshot(output);
});

test('should validate a sorted JSON object with the none order', async (t) => {
const fixture = {
a: 1,
Expand Down
28 changes: 28 additions & 0 deletions src/index.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,34 @@ Generated by [AVA](https://avajs.dev).
}␊
`

## should sort an unsorted JSON object with complex case sensitivies, preferring determinsistic order

> Snapshot 1

`{␊
"A": 1,␊
"a": 2,␊
"B": 3,␊
"b": 4,␊
"C": 5,␊
"c": 6␊
}␊
`

## should sort an unsorted JSON object with complex case sensitivies, preferring original sort order

> Snapshot 1

`{␊
"a": 2,␊
"A": 1,␊
"B": 3,␊
"b": 4,␊
"c": 6,␊
"C": 5␊
}␊
`

## should validate a sorted JSON object with the none order

> Snapshot 1
Expand Down
Binary file modified src/index.test.ts.snap
Binary file not shown.
10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ import { parsers as babelParsers } from 'prettier/plugins/babel';
* @returns A number indicating which element should come first.
*/
function lexicalSort(a: string, b: string) {
return a > b ? 1 : -1;
if (a > b) {
return 1;
}
if (a < b) {
return -1;
}
return 0;
}

const integerPrefixRegex = /^(\d+)/u;
Expand Down Expand Up @@ -78,7 +84,7 @@ function reverseSort(sortFunction: (a: string, b: string) => number) {
*/
function caseInsensitiveSort(sortFunction: (a: string, b: string) => number) {
return (a: string, b: string) => {
return sortFunction(a.toLowerCase(), b.toLowerCase());
return sortFunction(a.toLowerCase(), b.toLowerCase()) || sortFunction(a, b);
};
}

Expand Down
Loading