Skip to content

Commit

Permalink
feat: Enhance objectSortKeys props.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Sep 17, 2023
1 parent 8e693c2 commit d94956f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
2 changes: 1 addition & 1 deletion core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ export interface JsonViewProps<T extends object> extends React.DetailedHTMLProps
/** Define the root node name. @default undefined */
keyName?: string | number;
/** Whether sort keys through `String.prototype.localeCompare()` @default false */
objectSortKeys?: boolean | ((a: string, b: string) => number);
objectSortKeys?: boolean | ((keyA: string, keyB: string, valueA: T, valueB: T) => number);
/** Set the indent-width for nested objects @default 15 */
indentWidth?: number;
/** When set to `true`, `objects` and `arrays` are labeled with size @default true */
Expand Down
4 changes: 3 additions & 1 deletion core/src/comps/KeyValues.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export const KeyValues = <T extends object>(props: KeyValuesProps<T>) => {
entries =
objectSortKeys === true
? entries.sort(([a], [b]) => (typeof a === 'string' && typeof b === 'string' ? a.localeCompare(b) : 0))
: entries.sort(([a], [b]) => (typeof a === 'string' && typeof b === 'string' ? objectSortKeys(a, b) : 0));
: entries.sort(([a, valA], [b, valB]) =>
typeof a === 'string' && typeof b === 'string' ? objectSortKeys(a, b, valA, valB) : 0,
);
}

const style = {
Expand Down
40 changes: 40 additions & 0 deletions core/src/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import renderer from 'react-test-renderer';
import { screen, render, waitFor, fireEvent } from '@testing-library/react';
import JsonView from './';

const avatar = 'https://i.imgur.com/MK3eW3As.jpg';
Expand Down Expand Up @@ -47,3 +48,42 @@ it('renders <JsonView /> test case', () => {
expect(tree).toHaveProperty('props.onMouseEnter');
expect(tree).toHaveProperty('props.onMouseLeave');
});

it('renders <JsonView objectSortKeys /> test case', () => {
render(
<JsonView value={{ b: 1, a: 2 }} objectSortKeys>
<JsonView.KeyName data-testid="keyname" />
</JsonView>,
);
const keyname = screen.getAllByTestId('keyname')[0];
expect(keyname.innerHTML).toEqual('a');
});

it('renders <JsonView objectSortKeys={false} /> test case', () => {
render(
<JsonView value={{ b: 1, a: 2 }} objectSortKeys={false}>
<JsonView.KeyName data-testid="keyname" />
</JsonView>,
);
const keyname = screen.getAllByTestId('keyname')[0];
expect(keyname.innerHTML).toEqual('b');
});

it('renders <JsonView objectSortKeys={() => {}} /> test case', () => {
render(
<JsonView
value={{ bool: 1, a: 2 }}
objectSortKeys={(a, b, valA, valB) => {
expect(a).toEqual('a');
expect(b).toEqual('bool');
expect(valA).toEqual(2);
expect(valB).toEqual(1);
return a.localeCompare(b);
}}
>
<JsonView.KeyName data-testid="keyname" />
</JsonView>,
);
const keyname = screen.getAllByTestId('keyname')[0];
expect(keyname.innerHTML).toEqual('a');
});
2 changes: 1 addition & 1 deletion core/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export interface JsonViewProps<T extends object>
/** Define the root node name. @default undefined */
keyName?: string | number;
/** Whether sort keys through `String.prototype.localeCompare()` @default false */
objectSortKeys?: boolean | ((a: string, b: string) => number);
objectSortKeys?: boolean | ((keyA: string, keyB: string, valueA: T, valueB: T) => number);
/** Set the indent-width for nested objects @default 15 */
indentWidth?: number;
/** When set to `true`, `objects` and `arrays` are labeled with size @default true */
Expand Down

0 comments on commit d94956f

Please sign in to comment.