Skip to content

Commit eb5feee

Browse files
fix(util/viewer): Extract headsign if stop times don't have them.
1 parent d7b82f1 commit eb5feee

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

lib/components/viewers/stop-schedule-table.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import React, { Component } from 'react'
33
import { connect } from 'react-redux'
44
import styled from 'styled-components'
55

6+
import { isBlank } from '../../util/ui'
67
import { getFormattedStopTime, getStopTimesByPattern } from '../../util/viewer'
78

89
const { getTimeFormat } = coreUtils.time
@@ -25,13 +26,16 @@ const StyledTable = styled.table`
2526
}
2627
`
2728

28-
const RouteTd = styled.td`
29+
const DestHeader = styled.th`
30+
width: 100%;
31+
`
32+
const RouteCell = styled.td`
2933
font-weight: bold;
3034
`
31-
const DestTd = styled.td`
35+
const DestCell = styled.td`
3236
width: 100%;
3337
`
34-
const TimeTd = styled.td`
38+
const TimeCell = styled.td`
3539
font-weight: bold;
3640
text-align: right;
3741
white-space: nowrap;
@@ -50,17 +54,19 @@ class StopScheduleTable extends Component {
5054

5155
// Merge stop times, so that we can sort them across all route patterns.
5256
let mergedStopTimes = []
53-
Object.values(stopTimesByPattern).forEach(pattern => {
57+
Object.values(stopTimesByPattern).forEach(pattern => { //TODO:breakup pattern vars
5458
const filteredTimes = pattern.times
5559
//TODO refactor - Copied from util/viewers
5660
.filter(stopTime => {
5761
return stopTime.stopIndex < stopTime.stopCount - 1 // ensure that this isn't the last stop
5862
})
5963
.map(stopTime => {
60-
// Add a route attribute to each stop time for rendering route info.
64+
// Add the route attribute and headsign to each stop time for rendering route info.
65+
const headsign = isBlank(stopTime.headsign) ? pattern.pattern.headsign : stopTime.headsign
6166
return {
6267
...stopTime,
63-
route: pattern.route
68+
route: pattern.route,
69+
headsign
6470
}
6571
})
6672
mergedStopTimes = mergedStopTimes.concat(filteredTimes) // reduce?
@@ -79,21 +85,21 @@ class StopScheduleTable extends Component {
7985
<tr>
8086
<th>Block</th>
8187
<th>Route</th>
82-
<th>To</th>
88+
<DestHeader>To</DestHeader>
8389
<th>Departure</th>
8490
</tr>
8591
</thead>
8692
<tbody>
87-
{mergedStopTimes.map(stopTime => {
93+
{mergedStopTimes.map((stopTime, index) => {
8894
const { blockId, headsign, route } = stopTime
8995
const routeName = route.shortName ? route.shortName : route.longName
90-
const time = getFormattedStopTime(stopTime, homeTimezone, stopViewerArriving, timeFormat, true)
96+
const time = getFormattedStopTime(stopTime, homeTimezone, stopViewerArriving, timeFormat, true, false, false)
9197
return (
92-
<tr>
98+
<tr key={index}>
9399
<td>{blockId}</td>
94-
<RouteTd>{routeName}</RouteTd>
95-
<DestTd>{headsign}</DestTd>
96-
<TimeTd>{time}</TimeTd>
100+
<RouteCell>{routeName}</RouteCell>
101+
<DestCell>{headsign}</DestCell>
102+
<TimeCell>{time}</TimeCell>
97103
</tr>
98104
)
99105
})}

lib/util/viewer.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'moment-timezone'
44
import React from 'react'
55

66
import Icon from '../components/narrative/icon'
7+
import { isBlank } from './ui'
78

89
const {
910
formatDuration,
@@ -135,8 +136,18 @@ export function getStopTimesByPattern (stopData) {
135136
if (stopData && stopData.routes && stopData.stopTimes) {
136137
stopData.stopTimes.forEach(patternTimes => {
137138
const routeId = getRouteIdForPattern(patternTimes.pattern)
138-
const headsign = patternTimes.times[0] && patternTimes.times[0].headsign
139+
140+
let headsign = patternTimes.times[0] && patternTimes.times[0].headsign
141+
// In case stop time headsign is blank, extract headsign from the pattern 'desc' attribute
142+
// (format: '49 to <Destination> (<destid>)[ from <Origin> (<originid)]').
143+
if (isBlank(headsign)) {
144+
const matches = patternTimes.pattern.desc.match(/ to ([^(from)]+) \(.+\)/)
145+
if (matches) {
146+
headsign = matches[1]
147+
}
148+
}
139149
patternTimes.pattern.headsign = headsign
150+
140151
const id = `${routeId}-${headsign}`
141152
if (!(id in stopTimesByPattern)) {
142153
const route = stopData.routes.find(r => r.id === routeId)

0 commit comments

Comments
 (0)