From a1f07b0eae348f98d78a27ccf8e7dc031296ace6 Mon Sep 17 00:00:00 2001 From: callmeroot Date: Sat, 7 Dec 2024 21:08:22 +0530 Subject: [PATCH 1/5] fixed case insensitive sorting for Ktable --- lib/KTable/useSorting/index.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/KTable/useSorting/index.js b/lib/KTable/useSorting/index.js index a2b9c95a5..3d90d9f0f 100644 --- a/lib/KTable/useSorting/index.js +++ b/lib/KTable/useSorting/index.js @@ -29,7 +29,7 @@ export default function useSorting(headers, rows, defaultSort, disableBuiltinSor 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; @@ -40,7 +40,13 @@ export default function useSorting(headers, rows, defaultSort, disableBuiltinSor ); } - return _.orderBy(rows.value, [row => row[sortKey.value]], [sortOrder.value]); + return _.orderBy(rows.value, [row => { + const value = row[sortKey.value]; + if (typeof value === 'string') { + return value.toString().toLocaleLowerCase(); + } + return value; + }], [sortOrder.value]); }); /** @@ -85,4 +91,4 @@ export default function useSorting(headers, rows, defaultSort, disableBuiltinSor handleSort, getAriaSort, }; -} +} \ No newline at end of file From e7e7725391df295f73c3c9d58dd3cb8b6ae0a2b2 Mon Sep 17 00:00:00 2001 From: callmeroot Date: Sat, 7 Dec 2024 21:21:59 +0530 Subject: [PATCH 2/5] fixed linting error --- lib/KTable/useSorting/index.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/KTable/useSorting/index.js b/lib/KTable/useSorting/index.js index 3d90d9f0f..ebe80ba52 100644 --- a/lib/KTable/useSorting/index.js +++ b/lib/KTable/useSorting/index.js @@ -40,13 +40,19 @@ export default function useSorting(headers, rows, defaultSort, disableBuiltinSor ); } - return _.orderBy(rows.value, [row => { - const value = row[sortKey.value]; - if (typeof value === 'string') { - return value.toString().toLocaleLowerCase(); - } - return value; - }], [sortOrder.value]); + return _.orderBy( + rows.value, + [ + row => { + const value = row[sortKey.value]; + if (typeof value === 'string') { + return value.toString().toLocaleLowerCase(); + } + return value; + }, + ], + [sortOrder.value] + ); }); /** @@ -91,4 +97,4 @@ export default function useSorting(headers, rows, defaultSort, disableBuiltinSor handleSort, getAriaSort, }; -} \ No newline at end of file +} From 37e89b114da25cc6b840d01cfb0d2a024848daaa Mon Sep 17 00:00:00 2001 From: callmeroot Date: Tue, 10 Dec 2024 13:11:41 +0530 Subject: [PATCH 3/5] added unit tests for string sorting --- lib/KTable/useSorting/__tests__/index.spec.js | 20 ++++++------- lib/KTable/useSorting/index.js | 30 +++++++++++-------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/lib/KTable/useSorting/__tests__/index.spec.js b/lib/KTable/useSorting/__tests__/index.spec.js index 42a8be538..9de16699d 100644 --- a/lib/KTable/useSorting/__tests__/index.spec.js +++ b/lib/KTable/useSorting/__tests__/index.spec.js @@ -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)], ]); @@ -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)], ]); }); @@ -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)], ]); }); @@ -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)], ]); @@ -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)], ]); }); }); @@ -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)], ]); }); @@ -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)], ]); @@ -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); @@ -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)], ]); }); diff --git a/lib/KTable/useSorting/index.js b/lib/KTable/useSorting/index.js index c952d7688..b228b17b2 100644 --- a/lib/KTable/useSorting/index.js +++ b/lib/KTable/useSorting/index.js @@ -24,6 +24,20 @@ 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) => { + const value = row[index]; + if (typeof value === 'string') { + return value.toString().toLocaleLowerCase(); + } + 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. @@ -37,22 +51,14 @@ export default function useSorting(headers, rows, defaultSort, disableBuiltinSor return _.orderBy( rows.value, - [row => row[defaultSort.value.index]], - [defaultSort.value.direction], + [row => getSortValue(row, defaultSort.value.index)], + [defaultSort.value.direction] ); } return _.orderBy( rows.value, - [ - row => { - const value = row[sortKey.value]; - if (typeof value === 'string') { - return value.toString().toLocaleLowerCase(); - } - return value; - }, - ], + [row => getSortValue(row, sortKey.value)], [sortOrder.value] ); }); @@ -99,4 +105,4 @@ export default function useSorting(headers, rows, defaultSort, disableBuiltinSor handleSort, getAriaSort, }; -} +} \ No newline at end of file From 0e53bba823a9fd8d184298dee60514a93d9af556 Mon Sep 17 00:00:00 2001 From: callmeroot Date: Tue, 10 Dec 2024 13:58:45 +0530 Subject: [PATCH 4/5] fixed linting error --- lib/KTable/useSorting/index.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/KTable/useSorting/index.js b/lib/KTable/useSorting/index.js index b228b17b2..523bcb8fa 100644 --- a/lib/KTable/useSorting/index.js +++ b/lib/KTable/useSorting/index.js @@ -52,15 +52,11 @@ export default function useSorting(headers, rows, defaultSort, disableBuiltinSor return _.orderBy( rows.value, [row => getSortValue(row, defaultSort.value.index)], - [defaultSort.value.direction] + [defaultSort.value.direction], ); } - return _.orderBy( - rows.value, - [row => getSortValue(row, sortKey.value)], - [sortOrder.value] - ); + return _.orderBy(rows.value, [row => getSortValue(row, sortKey.value)], [sortOrder.value]); }); /** @@ -105,4 +101,4 @@ export default function useSorting(headers, rows, defaultSort, disableBuiltinSor handleSort, getAriaSort, }; -} \ No newline at end of file +} From b8456dedb69e553e0dd91561da63f2c3e33c39bf Mon Sep 17 00:00:00 2001 From: callmeroot Date: Mon, 16 Dec 2024 12:31:53 +0530 Subject: [PATCH 5/5] added check for null --- lib/KTable/useSorting/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/KTable/useSorting/index.js b/lib/KTable/useSorting/index.js index 523bcb8fa..5893003b5 100644 --- a/lib/KTable/useSorting/index.js +++ b/lib/KTable/useSorting/index.js @@ -32,7 +32,7 @@ export default function useSorting(headers, rows, defaultSort, disableBuiltinSor */ const getSortValue = (row, index) => { const value = row[index]; - if (typeof value === 'string') { + if (typeof value === 'string' && value != null) { return value.toString().toLocaleLowerCase(); } return value;