|
2 | 2 |
|
3 | 3 | const parseIsoDuration = require('parse-iso-duration')
|
4 | 4 |
|
5 |
| -const clone = obj => Object.assign({}, obj) |
| 5 | +const parseJourneyLeg = (ctx, l) => { // l = leg |
| 6 | + const {profile, opt} = ctx |
6 | 7 |
|
7 |
| -const createParseJourneyLeg = (profile, opt, data) => { |
8 |
| - // j = journey, l = leg |
9 |
| - const parseJourneyLeg = (j, l) => { |
10 |
| - const orig = l.Origin |
11 |
| - const dest = l.Destination |
| 8 | + const orig = l.Origin |
| 9 | + const dest = l.Destination |
12 | 10 |
|
13 |
| - const res = { |
14 |
| - origin: profile.parseLocation(profile, opt, data, orig), |
15 |
| - destination: profile.parseLocation(profile, opt, data, dest) |
16 |
| - } |
17 |
| - |
18 |
| - // todo: does `dest.rtAlighting` actually if the arrival is cancelled? |
19 |
| - const arr = profile.parseWhen(profile, dest.date, dest.rtDate, dest.time, dest.rtTime, dest.rtTz, !dest.rtAlighting) |
20 |
| - res.arrival = arr.when |
21 |
| - res.plannedArrival = arr.plannedWhen |
22 |
| - res.arrivalDelay = arr.delay |
23 |
| - if (arr.prognosedWhen) res.prognosedArrival = arr.prognosedWhen |
24 |
| - |
25 |
| - // todo: does `orig.rtBoarding` actually if the departure is cancelled? |
26 |
| - const dep = profile.parseWhen(profile, orig.date, orig.rtDate, orig.time, orig.rtTime, orig.tz, !orig.rtBoarding) |
27 |
| - res.departure = dep.when |
28 |
| - res.plannedDeparture = dep.plannedWhen |
29 |
| - res.departureDelay = dep.delay |
30 |
| - if (dep.prognosedWhen) res.prognosedDeparture = dep.prognosedWhen |
31 |
| - |
32 |
| - if (orig.rtBoarding === false || dest.rtAlighting === false) { |
33 |
| - res.cancelled = true |
34 |
| - Object.defineProperty(res, 'canceled', {value: true}) |
35 |
| - } |
| 11 | + const res = { |
| 12 | + origin: profile.parseLocation(ctx, orig), |
| 13 | + destination: profile.parseLocation(ctx, dest) |
| 14 | + } |
36 | 15 |
|
37 |
| - if (l.type === 'WALK' || l.type === 'TRSF') { |
38 |
| - res.public = true |
39 |
| - res.walking = true |
40 |
| - res.distance = l.dist || null |
41 |
| - // if (l.type === 'TRSF') res.transfer = true |
42 |
| - // todo: l.GisRef |
43 |
| - res.duration = l.duration ? parseIsoDuration(l.duration) / 1000 | 0 : null |
44 |
| - } else if (l.type === 'JNY') { |
45 |
| - // todo: does `dest.rtAlighting` actually say if the arrival is cancelled? |
46 |
| - const arrPl = profile.parsePlatform(profile, dest.track, dest.rtTrack, !dest.rtAlighting) |
47 |
| - res.arrivalPlatform = arrPl.platform |
48 |
| - res.plannedArrivalPlatform = arrPl.plannedPlatform |
49 |
| - if (arrPl.prognosedPlatform) res.prognosedArrivalPlatform = arrPl.prognosedPlatform |
50 |
| - |
51 |
| - // todo: does `orig.rtBoarding` actually if say the departure is cancelled? |
52 |
| - const depPl = profile.parsePlatform(profile, orig.track, orig.rtTrack, !orig.rtBoarding) |
53 |
| - res.departurePlatform = depPl.platform |
54 |
| - res.plannedDeparturePlatform = depPl.plannedPlatform |
55 |
| - if (depPl.prognosedPlatform) res.prognosedDeparturePlatform = depPl.prognosedPlatform |
56 |
| - |
57 |
| - // todo: pull `public` value from `profile.products` |
58 |
| - res.tripId = l.JourneyDetailRef && l.JourneyDetailRef.ref || null |
59 |
| - |
60 |
| - const product = {name: l.name, number: l.number, ...l.Product} |
61 |
| - res.line = profile.parseLine(profile, opt, data)(product) || null |
62 |
| - |
63 |
| - res.direction = l.direction && profile.parseStationName(l.direction) || null |
64 |
| - |
65 |
| - if (opt.stopovers && l.stops) { |
66 |
| - const parse = profile.parseStopover(profile, opt, data) |
67 |
| - res.stopovers = l.stops.map(st => parse(null, st)) |
68 |
| - } |
69 |
| - } |
| 16 | + // todo: does `dest.rtAlighting` actually if the arrival is cancelled? |
| 17 | + const arr = profile.parseWhen(ctx, dest.date, dest.rtDate, dest.time, dest.rtTime, dest.rtTz, !dest.rtAlighting) |
| 18 | + res.arrival = arr.when |
| 19 | + res.plannedArrival = arr.plannedWhen |
| 20 | + res.arrivalDelay = arr.delay |
| 21 | + if (arr.prognosedWhen) res.prognosedArrival = arr.prognosedWhen |
| 22 | + |
| 23 | + // todo: does `orig.rtBoarding` actually if the departure is cancelled? |
| 24 | + const dep = profile.parseWhen(ctx, orig.date, orig.rtDate, orig.time, orig.rtTime, orig.tz, !orig.rtBoarding) |
| 25 | + res.departure = dep.when |
| 26 | + res.plannedDeparture = dep.plannedWhen |
| 27 | + res.departureDelay = dep.delay |
| 28 | + if (dep.prognosedWhen) res.prognosedDeparture = dep.prognosedWhen |
| 29 | + |
| 30 | + if (orig.rtBoarding === false || dest.rtAlighting === false) { |
| 31 | + res.cancelled = true |
| 32 | + Object.defineProperty(res, 'canceled', {value: true}) |
| 33 | + } |
70 | 34 |
|
71 |
| - if (opt.polylines && l.Polyline) { |
72 |
| - const parse = profile.parsePolyline(profile, opt, data) |
73 |
| - res.polyline = parse(l.Polyline) || null |
| 35 | + if (l.type === 'WALK' || l.type === 'TRSF') { |
| 36 | + res.public = true |
| 37 | + res.walking = true |
| 38 | + res.distance = l.dist || null |
| 39 | + // if (l.type === 'TRSF') res.transfer = true |
| 40 | + // todo: l.GisRef |
| 41 | + res.duration = l.duration ? parseIsoDuration(l.duration) / 1000 | 0 : null |
| 42 | + } else if (l.type === 'JNY') { |
| 43 | + // todo: does `dest.rtAlighting` actually say if the arrival is cancelled? |
| 44 | + const arrPl = profile.parsePlatform(ctx, dest.track, dest.rtTrack, !dest.rtAlighting) |
| 45 | + res.arrivalPlatform = arrPl.platform |
| 46 | + res.plannedArrivalPlatform = arrPl.plannedPlatform |
| 47 | + if (arrPl.prognosedPlatform) res.prognosedArrivalPlatform = arrPl.prognosedPlatform |
| 48 | + |
| 49 | + // todo: does `orig.rtBoarding` actually if say the departure is cancelled? |
| 50 | + const depPl = profile.parsePlatform(ctx, orig.track, orig.rtTrack, !orig.rtBoarding) |
| 51 | + res.departurePlatform = depPl.platform |
| 52 | + res.plannedDeparturePlatform = depPl.plannedPlatform |
| 53 | + if (depPl.prognosedPlatform) res.prognosedDeparturePlatform = depPl.prognosedPlatform |
| 54 | + |
| 55 | + // todo: pull `public` value from `profile.products` |
| 56 | + res.tripId = l.JourneyDetailRef && l.JourneyDetailRef.ref || null |
| 57 | + |
| 58 | + // todo: is it num instead of number? |
| 59 | + const product = {name: l.name, number: l.number, ...l.Product} |
| 60 | + res.line = profile.parseLine(ctx, product) || null |
| 61 | + |
| 62 | + res.direction = l.direction && profile.parseStationName(ctx, l.direction) || null |
| 63 | + |
| 64 | + if (opt.stopovers && l.stops) { |
| 65 | + const parse = profile.parseStopover |
| 66 | + res.stopovers = l.stops.map(st => parse(ctx, st, date)) // todo: date |
74 | 67 | }
|
| 68 | + } |
75 | 69 |
|
76 |
| - if (opt.remarks && Array.isArray(l.notes)) { |
77 |
| - res.hints = l.notes.map(h => profile.parseHint(profile, h, data)) |
78 |
| - } |
| 70 | + if (opt.polylines && l.Polyline) { |
| 71 | + res.polyline = profile.parsePolyline(ctx, l.Polyline) || null |
| 72 | + } |
79 | 73 |
|
80 |
| - return res |
| 74 | + if (opt.remarks && Array.isArray(l.notes)) { |
| 75 | + res.hints = l.notes.map(h => profile.parseHint(ctx, h)) |
81 | 76 | }
|
82 | 77 |
|
83 |
| - return parseJourneyLeg |
| 78 | + return res |
84 | 79 | }
|
85 | 80 |
|
86 |
| -module.exports = createParseJourneyLeg |
| 81 | +module.exports = parseJourneyLeg |
0 commit comments