-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into allow-more-styling
- Loading branch information
Showing
8 changed files
with
202 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import '../test-utils/mock-window-url' | ||
import { extractMainHeadsigns } from '../../lib/util/pattern-viewer' | ||
|
||
function createStops(ids) { | ||
return ids.map((id) => ({ | ||
id, | ||
name: id | ||
})) | ||
} | ||
|
||
function editHeadsign(pattern) { | ||
pattern.headsign = `${pattern.headsign} (${pattern.lastStop})` | ||
} | ||
|
||
describe('util > pattern-viewer', () => { | ||
describe('extractMainHeadsigns', () => { | ||
it('should retain the essential patterns', () => { | ||
// Consider the following patterns P1, P2, P3 of the same route with the same headsigns: | ||
// Stops S1 S2 S3 S4 S5 S6 S7 --> direction of travel | ||
// P1: o--o--o--o--o | ||
// P2: o--o-----o--o | ||
// P3: o--o--o | ||
// | ||
// P3 should be removed because it is a subset of P1. | ||
// P1 and P2 should be kept. | ||
// Patterns are assumed in descending length order because | ||
// pre-sorting happened before extractMainHeadsigns is invoked (key order matters). | ||
const headsign = 'Everett via Lynnwood' | ||
const route = '512' | ||
const patterns = { | ||
P1: { | ||
headsign, | ||
id: 'P1', | ||
name: 'P1 Pattern name', | ||
patternGeometry: { | ||
length: 1404, | ||
points: 'p1-points' | ||
}, | ||
stops: createStops(['S1', 'S2', 'S3', 'S4', 'S5']) | ||
}, | ||
P2: { | ||
headsign, | ||
id: 'P2', | ||
name: 'P2 Pattern name', | ||
patternGeometry: { | ||
length: 1072, | ||
points: 'p2-points' | ||
}, | ||
stops: createStops(['S3', 'S4', 'S6', 'S7']) | ||
}, | ||
P3: { | ||
headsign, | ||
id: 'P3', | ||
name: 'P3 Pattern name', | ||
patternGeometry: { | ||
length: 987, | ||
points: 'p3-points' | ||
}, | ||
stops: createStops(['S3', 'S4', 'S5']) | ||
} | ||
} | ||
const headsignData = extractMainHeadsigns(patterns, route, editHeadsign) | ||
expect(headsignData.length).toBe(2) | ||
expect(headsignData[0].headsign).toBe(headsign) | ||
expect(headsignData[1].headsign).toBe(`${headsign} (S7)`) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Pattern } from '../components/util/types' | ||
|
||
import { extractHeadsignFromPattern } from './viewer' | ||
|
||
export interface PatternSummary { | ||
geometryLength: number | ||
headsign: string | ||
id: string | ||
lastStop?: string | ||
} | ||
|
||
export function extractMainHeadsigns( | ||
patterns: Record<string, Pattern>, | ||
shortName: string, | ||
editHeadsign: (pattern: PatternSummary) => void | ||
): PatternSummary[] { | ||
const mapped = Object.entries(patterns).map( | ||
([id, pat]): PatternSummary => ({ | ||
geometryLength: pat.patternGeometry?.length || 0, | ||
headsign: extractHeadsignFromPattern(pat, shortName), | ||
id, | ||
lastStop: pat.stops?.[pat.stops?.length - 1]?.name | ||
}) | ||
) | ||
|
||
// Address duplicate headsigns. | ||
return mapped.reduce((prev: PatternSummary[], cur) => { | ||
const amended = prev | ||
const alreadyExistingIndex = prev.findIndex( | ||
(h) => h.headsign === cur.headsign | ||
) | ||
// If the headsign is a duplicate, and the last stop of the pattern is not the headsign, | ||
// amend the headsign with the last stop name in parenthesis. | ||
// e.g. "Headsign (Last Stop)" | ||
if ( | ||
alreadyExistingIndex >= 0 && | ||
cur.lastStop && | ||
cur.headsign !== cur.lastStop | ||
) { | ||
editHeadsign(cur) | ||
|
||
// If there are only two total patterns, then we should rename | ||
// both of them | ||
if (amended.length === 1 && Object.entries(patterns).length === 2) { | ||
editHeadsign(amended[0]) | ||
amended.push(cur) | ||
return amended | ||
} | ||
} | ||
|
||
// With all remaining duplicate headsigns with the same last stops, only keep the pattern with the | ||
// longest geometry. | ||
if ( | ||
alreadyExistingIndex >= 0 && | ||
amended[alreadyExistingIndex].lastStop === cur.lastStop | ||
) { | ||
if (amended[alreadyExistingIndex].geometryLength < cur.geometryLength) { | ||
amended[alreadyExistingIndex] = cur | ||
} | ||
} else { | ||
amended.push(cur) | ||
} | ||
return amended | ||
}, []) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.