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

Make route comparator tolerate all kinds of data #124

Merged
merged 8 commits into from
Feb 5, 2020
189 changes: 189 additions & 0 deletions __tests__/util/__snapshots__/itinerary.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`util > itinerary routeComparator should prioritize routes with shortNames over those with just longNames 1`] = `
Array [
Object {
"longName": "B-line",
"shortName": "B",
"sortOrder": -999,
},
Object {
"longName": "A meandering route",
"sortOrder": -999,
},
]
`;

exports[`util > itinerary routeComparator should prioritize routes with valid integer shortNames 1`] = `
Array [
Object {
"longName": "Loop route",
"shortName": "2",
"sortOrder": -999,
},
Object {
"longName": "A-line",
"shortName": "A",
"sortOrder": -999,
Copy link
Member

Choose a reason for hiding this comment

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

I think this test might need to be renamed?

},
]
`;

exports[`util > itinerary routeComparator should prioritize routes with valid sortOrder 1`] = `
Array [
Object {
"longName": "Around town",
"shortName": "20",
"sortOrder": 2,
},
Object {
"longName": "Around another town",
"shortName": "3",
"sortOrder": -999,
},
]
`;

exports[`util > itinerary routeComparator should skip integer short name criteria when specified 1`] = `
Array [
Object {
"longName": "Variation of express route",
"shortName": "30",
"sortOrder": 2,
},
Object {
"longName": "Local route",
"shortName": "6",
"sortOrder": 2,
},
]
`;

exports[`util > itinerary routeComparator should sort routes based off of integer shortName 1`] = `
Array [
Object {
"longName": "Loop route",
"shortName": "2",
"sortOrder": -999,
},
Object {
"longName": "Around another town",
"shortName": "3",
"sortOrder": -999,
},
]
`;

exports[`util > itinerary routeComparator should sort routes based off of integer shortName with routes with same sortOrder 1`] = `
Array [
Object {
"longName": "Around town",
"shortName": "20",
"sortOrder": 2,
},
Object {
"longName": "Zig-zagging route",
"shortName": "30",
"sortOrder": 2,
},
]
`;

exports[`util > itinerary routeComparator should sort routes based off of longNames 1`] = `
Array [
Object {
"longName": "Express route",
"shortName": "30",
"sortOrder": 2,
},
Object {
"longName": "Variation of express route",
"shortName": "30",
"sortOrder": 2,
},
]
`;

exports[`util > itinerary routeComparator should sort routes based off of shortNames 1`] = `
Array [
Object {
"longName": "A-line",
"shortName": "A",
"sortOrder": -999,
},
Object {
"longName": "B-line",
"shortName": "B",
"sortOrder": -999,
},
]
`;

exports[`util > itinerary routeComparator should sort routes based off of sortOrder 1`] = `
Array [
Object {
"longName": "Around town",
"shortName": "20",
"sortOrder": 2,
},
Object {
"longName": "Across town",
"shortName": "10",
"sortOrder": 10,
},
]
`;

exports[`util > itinerary routeComparator should sort routes on all of the criteria at once 1`] = `
Array [
Object {
"longName": "Around town",
"shortName": "20",
"sortOrder": 2,
},
Object {
"longName": "Express route",
"shortName": "30",
"sortOrder": 2,
},
Object {
"longName": "Variation of express route",
"shortName": "30",
"sortOrder": 2,
},
Object {
"longName": "Zig-zagging route",
"shortName": "30",
"sortOrder": 2,
},
Object {
"longName": "Across town",
"shortName": "10",
"sortOrder": 10,
},
Object {
"longName": "Loop route",
"shortName": "2",
"sortOrder": -999,
},
Object {
"longName": "Around another town",
"shortName": "3",
"sortOrder": -999,
},
Object {
"longName": "A-line",
"shortName": "A",
"sortOrder": -999,
},
Object {
"longName": "B-line",
"shortName": "B",
"sortOrder": -999,
},
Object {
"longName": "A meandering route",
"sortOrder": -999,
},
]
`;
117 changes: 116 additions & 1 deletion __tests__/util/itinerary.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,122 @@
import {isTransit} from '../../lib/util/itinerary'
import {isTransit, makeRouteComparator} from '../../lib/util/itinerary'

describe('util > itinerary', () => {
it('isTransit should work', () => {
expect(isTransit('CAR')).toBeFalsy()
})

describe('routeComparator', () => {
const route1 = {
longName: 'Across town',
shortName: '10',
sortOrder: 10
}
const route2 = {
longName: 'Around town',
shortName: '20',
sortOrder: 2
}
const route3 = {
longName: 'Around another town',
shortName: '3',
sortOrder: -999
}
const route4 = {
longName: 'Loop route',
shortName: '2',
sortOrder: -999
}
const route5 = {
longName: 'A-line',
shortName: 'A',
sortOrder: -999
}
const route6 = {
longName: 'B-line',
shortName: 'B',
sortOrder: -999
}
const route7 = {
longName: 'A meandering route',
sortOrder: -999
}
const route8 = {
longName: 'Zig-zagging route',
shortName: '30',
sortOrder: 2
}
const route9 = {
longName: 'Express route',
shortName: '30',
sortOrder: 2
}
const route10 = {
longName: 'Variation of express route',
shortName: '30',
sortOrder: 2
}
const route11 = {
longName: 'Local route',
shortName: '6',
sortOrder: 2
}

function sortRoutes (...routes) {
routes.sort(makeRouteComparator())
return routes
}

it('should sort routes based off of sortOrder', () => {
expect(sortRoutes(route1, route2)).toMatchSnapshot()
})

it('should prioritize routes with valid sortOrder', () => {
expect(sortRoutes(route2, route3)).toMatchSnapshot()
})

it('should sort routes based off of integer shortName with routes with same sortOrder', () => {
expect(sortRoutes(route8, route2)).toMatchSnapshot()
})

it('should sort routes based off of integer shortName', () => {
expect(sortRoutes(route3, route4)).toMatchSnapshot()
})

it('should prioritize routes with valid integer shortNames', () => {
expect(sortRoutes(route4, route5)).toMatchSnapshot()
})

it('should sort routes based off of shortNames', () => {
expect(sortRoutes(route5, route6)).toMatchSnapshot()
})

it('should prioritize routes with shortNames over those with just longNames', () => {
expect(sortRoutes(route6, route7)).toMatchSnapshot()
})

it('should sort routes based off of longNames', () => {
expect(sortRoutes(route9, route10)).toMatchSnapshot()
})

it('should sort routes on all of the criteria at once', () => {
expect(sortRoutes(
route1,
route2,
route3,
route4,
route5,
route6,
route7,
route8,
route9,
route10
)).toMatchSnapshot()
})

it('should skip integer short name criteria when specified', () => {
const routes = [route10, route11]
routes.sort(makeRouteComparator(false))
expect(routes).toMatchSnapshot()
})
})
})
6 changes: 4 additions & 2 deletions lib/components/viewers/route-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Icon from '../narrative/icon'

import { setMainPanelContent, setViewedRoute } from '../../actions/ui'
import { findRoutes, findRoute } from '../../actions/api'
import { routeComparator } from '../../util/itinerary'
import { makeRouteComparator } from '../../util/itinerary'

function operatorIndexForRoute (operators, route) {
if (!route.agency) return 0
Expand Down Expand Up @@ -57,7 +57,9 @@ class RouteViewer extends Component {
setViewedRoute,
viewedRoute
} = this.props
const sortedRoutes = routes ? Object.values(routes).sort(routeComparator) : []
const sortedRoutes = routes
? Object.values(routes).sort(makeRouteComparator())
: []
const agencySortedRoutes = operators.length > 0
? sortedRoutes.sort((a, b) => {
return operatorIndexForRoute(operators, a) - operatorIndexForRoute(operators, b)
Expand Down
4 changes: 3 additions & 1 deletion lib/components/viewers/stop-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import LocationIcon from '../icons/location-icon'
import { setMainPanelContent, toggleAutoRefresh } from '../../actions/ui'
import { findStop, findStopTimesForStop } from '../../actions/api'
import { forgetStop, rememberStop, setLocation } from '../../actions/map'
import { routeComparator } from '../../util/itinerary'
import { makeRouteComparator } from '../../util/itinerary'
import { getShowUserSettings, getStopViewerConfig } from '../../util/state'
import { formatDuration, formatSecondsAfterMidnight, getTimeFormat, getUserTimezone } from '../../util/time'

const routeComparator = makeRouteComparator()

class StopViewer extends Component {
state = {}

Expand Down
Loading