Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fixed case-sensitive sorting for KTable #854

Merged
merged 7 commits into from
Dec 16, 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
20 changes: 10 additions & 10 deletions lib/KTable/useSorting/__tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('useSorting', () => {

rows = ref([
['John', 30, new Date(1990, 5, 15)],
['Jane', 25, new Date(1995, 10, 20)],
['jane', 25, new Date(1995, 10, 20)],
['Alice', 28, new Date(1992, 8, 10)],
]);

Expand Down Expand Up @@ -52,7 +52,7 @@ describe('useSorting', () => {
const { sortedRows } = useSorting(headers, rows, defaultSort, disableBuiltinSorting);
expect(sortedRows.value).toEqual([
['Alice', 28, new Date(1992, 8, 10)],
['Jane', 25, new Date(1995, 10, 20)],
['jane', 25, new Date(1995, 10, 20)],
['John', 30, new Date(1990, 5, 15)],
]);
});
Expand All @@ -66,7 +66,7 @@ describe('useSorting', () => {
const { sortedRows } = useSorting(headers, rows, defaultSort, disableBuiltinSorting);
expect(sortedRows.value).toEqual([
['John', 30, new Date(1990, 5, 15)],
['Jane', 25, new Date(1995, 10, 20)],
['jane', 25, new Date(1995, 10, 20)],
['Alice', 28, new Date(1992, 8, 10)],
]);
});
Expand All @@ -79,7 +79,7 @@ describe('useSorting', () => {

const { sortedRows } = useSorting(headers, rows, defaultSort, disableBuiltinSorting);
expect(sortedRows.value).toEqual([
['Jane', 25, new Date(1995, 10, 20)],
['jane', 25, new Date(1995, 10, 20)],
['Alice', 28, new Date(1992, 8, 10)],
['John', 30, new Date(1990, 5, 15)],
]);
Expand All @@ -95,7 +95,7 @@ describe('useSorting', () => {
expect(sortedRows.value).toEqual([
['John', 30, new Date(1990, 5, 15)],
['Alice', 28, new Date(1992, 8, 10)],
['Jane', 25, new Date(1995, 10, 20)],
['jane', 25, new Date(1995, 10, 20)],
]);
});
});
Expand Down Expand Up @@ -144,7 +144,7 @@ describe('useSorting', () => {
handleSort(0); // Sort by 'Name'
expect(sortedRows.value).toEqual([
['Alice', 28, new Date(1992, 8, 10)],
['Jane', 25, new Date(1995, 10, 20)],
['jane', 25, new Date(1995, 10, 20)],
['John', 30, new Date(1990, 5, 15)],
]);
});
Expand All @@ -159,7 +159,7 @@ describe('useSorting', () => {

handleSort(1); // Sort by 'Age'
expect(sortedRows.value).toEqual([
['Jane', 25, new Date(1995, 10, 20)],
['jane', 25, new Date(1995, 10, 20)],
['Alice', 28, new Date(1992, 8, 10)],
['John', 30, new Date(1990, 5, 15)],
]);
Expand All @@ -169,14 +169,14 @@ describe('useSorting', () => {
expect(sortedRows.value).toEqual([
['John', 30, new Date(1990, 5, 15)],
['Alice', 28, new Date(1992, 8, 10)],
['Jane', 25, new Date(1995, 10, 20)],
['jane', 25, new Date(1995, 10, 20)],
]);
expect(sortOrder.value).toBe(SORT_ORDER_DESC);

handleSort(1); // Sort by 'Age' again to default order
expect(sortedRows.value).toEqual([
['John', 30, new Date(1990, 5, 15)],
['Jane', 25, new Date(1995, 10, 20)],
['jane', 25, new Date(1995, 10, 20)],
['Alice', 28, new Date(1992, 8, 10)],
]);
expect(sortOrder.value).toBe(null);
Expand All @@ -194,7 +194,7 @@ describe('useSorting', () => {
expect(sortedRows.value).toEqual([
['John', 30, new Date(1990, 5, 15)],
['Alice', 28, new Date(1992, 8, 10)],
['Jane', 25, new Date(1995, 10, 20)],
['jane', 25, new Date(1995, 10, 20)],
]);
});

Expand Down
20 changes: 17 additions & 3 deletions lib/KTable/useSorting/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,39 @@ export default function useSorting(headers, rows, defaultSort, disableBuiltinSor
const sortKey = ref(null);
const sortOrder = ref(null);

/**
* Helper function to get the value for sorting.
* @param {Object} row - The row object.
* @param {Number} index - The index of the column to sort by.
* @returns {any} - The value to be used for sorting.
*/
const getSortValue = (row, index) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat

const value = row[index];
if (typeof value === 'string' && value != null) {
return value.toString().toLocaleLowerCase();
BabyElias marked this conversation as resolved.
Show resolved Hide resolved
}
return value;
};

/**
* Computed property that returns the sorted rows based on the current sort key and order.
* If local sorting is disabled or no sort key is set, it returns the original rows.
*/
const sortedRows = computed(() => {
if (disableBuiltinSorting.value) return rows.value;

// No sort key or value has been explicity set till now
// No sort key or value has been explicitly set till now
if (sortKey.value === null || sortOrder.value === null) {
if (defaultSort.value.index === -1) return rows.value;

return _.orderBy(
rows.value,
[row => row[defaultSort.value.index]],
[row => getSortValue(row, defaultSort.value.index)],
[defaultSort.value.direction],
);
}

return _.orderBy(rows.value, [row => row[sortKey.value]], [sortOrder.value]);
return _.orderBy(rows.value, [row => getSortValue(row, sortKey.value)], [sortOrder.value]);
});

/**
Expand Down
Loading