Skip to content
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
40 changes: 30 additions & 10 deletions src/components/routes/ViewAllRoutesModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,37 @@

function filterRoutes() {
const lowerCaseQuery = query.toLowerCase();
filteredRoutes = routes.filter((route) => {
const shortName = route.shortName?.toLowerCase();
const longNameOrDescription = (route.longName || route.description || '').toLowerCase();
const agencyName = route.agencyInfo?.name?.toLowerCase();
filteredRoutes = routes
.filter((route) => {
const shortName = route.shortName?.toLowerCase();
const longNameOrDescription = (route.longName || route.description || '').toLowerCase();
const agencyName = route.agencyInfo?.name?.toLowerCase();

return (
shortName?.includes(lowerCaseQuery) ||
longNameOrDescription.includes(lowerCaseQuery) ||
agencyName?.includes(lowerCaseQuery)
);
});
return (
shortName?.includes(lowerCaseQuery) ||
longNameOrDescription.includes(lowerCaseQuery) ||
agencyName?.includes(lowerCaseQuery)
);
})
.sort((a, b) => {
const getNumericValue = (route) => {
if (!route.shortName) return Number.MAX_SAFE_INTEGER; // Put routes without shortName at the end

const matches = route.shortName.match(/(\d+)/);
return matches ? parseInt(matches[1], 10) : Number.MAX_SAFE_INTEGER;
};

const aNumeric = getNumericValue(a);
const bNumeric = getNumericValue(b);

// Case 1: Both have numeric values, sort numerically
if (aNumeric !== bNumeric) {
return aNumeric - bNumeric;
}

// Case 2: One has numeric value, the other doesn't, numeric comes first
return (a.shortName || '').localeCompare(b.shortName || '');
});
}
</script>

Expand Down
17 changes: 16 additions & 1 deletion src/components/routes/__tests__/ViewAllRoutesModal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,22 @@ describe('ViewAllRoutesModal', () => {
.filter((btn) => btn.className.includes('route-item'));
if (routeButtons.length > 0) {
await user.click(routeButtons[0]);
expect(mockHandleModalRouteClick).toHaveBeenCalledWith(mockRoutesListData[0]);

const sortedRoutes = [...mockRoutesListData].sort((a, b) => {
const getNumericValue = (route) => {
if (!route.shortName) return 999999;
const matches = route.shortName.match(/(\d+)/);
return matches ? parseInt(matches[1], 10) : 999999;
};
const aNumeric = getNumericValue(a);
const bNumeric = getNumericValue(b);
if (aNumeric !== bNumeric) {
return aNumeric - bNumeric;
}
return (a.shortName || '').localeCompare(b.shortName || '');
});

expect(mockHandleModalRouteClick).toHaveBeenCalledWith(sortedRoutes[0]);
}
});

Expand Down