Skip to content

Commit fa97ee8

Browse files
committed
rest.exe: adapt to ctx-based parse fns
see #140 see fb7a565..4d11f34
1 parent d8b0f2d commit fa97ee8

File tree

9 files changed

+194
-202
lines changed

9 files changed

+194
-202
lines changed

parse-rest/arrival-or-departure.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
// todo: d.JourneyStatus
44
// todo: d.prognosisType
55

6-
const createParseArrOrDep = (profile, opt, data, type) => {
7-
const parseArrOrDep = (d) => {
6+
const createParseArrOrDep = (type) => {
7+
const parseArrOrDep = (ctx, d) => {
8+
const {profile, opt} = ctx
9+
810
const product = {
911
name: d.name,
1012
number: d.trainNumber,
@@ -14,16 +16,17 @@ const createParseArrOrDep = (profile, opt, data, type) => {
1416

1517
const res = {
1618
tripId: d.JourneyDetailRef && d.JourneyDetailRef.ref || null,
17-
stop: profile.parseLocation(profile, opt, data, {
19+
stop: profile.parseLocation(ctx, {
1820
type: d.type,
1921
name: d.stop,
2022
id: d.stopid,
2123
extId: d.stopExtId
2224
}),
23-
line: profile.parseLine(profile, opt, data)(product) || null,
25+
line: profile.parseLine(ctx, product) || null,
2426
direction: type === 'departure' && d.direction || null, // todo: arrivals
2527
// todo: is there `d.rtDate` & `d.tz` & `d.rtTz`?
26-
...profile.parseWhen(profile, d.date, null, d.time, d.rtTime, null, !!d.cancelled),
28+
...profile.parseWhen(ctx, d.date, null, d.time, d.rtTime, null, !!d.cancelled),
29+
// todo: use profile.parsePlatform
2730
platform: d.track || null
2831
}
2932

parse-rest/hint.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
'use strict'
22

33
const omit = require('lodash/omit')
4+
// todo: get the original parseHint differently
45
const _parseHint = require('../parse/hint')
56

6-
const parseHint = (profile, hint, _) => {
7-
return _parseHint(profile, {
7+
const parseHint = (ctx, hint) => {
8+
return _parseHint(ctx, {
89
...omit(hint, ['value']),
910
code: hint.key,
1011
txtN: hint.value
1112
// todo: map hint.routeIdxFrom & hint.routeIdxTo
12-
}, _)
13+
})
1314
}
1415

1516
module.exports = parseHint

parse-rest/journey-leg.js

+66-71
Original file line numberDiff line numberDiff line change
@@ -2,85 +2,80 @@
22

33
const parseIsoDuration = require('parse-iso-duration')
44

5-
const clone = obj => Object.assign({}, obj)
5+
const parseJourneyLeg = (ctx, l) => { // l = leg
6+
const {profile, opt} = ctx
67

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
1210

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+
}
3615

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+
}
7034

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
7467
}
68+
}
7569

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+
}
7973

80-
return res
74+
if (opt.remarks && Array.isArray(l.notes)) {
75+
res.hints = l.notes.map(h => profile.parseHint(ctx, h))
8176
}
8277

83-
return parseJourneyLeg
78+
return res
8479
}
8580

86-
module.exports = createParseJourneyLeg
81+
module.exports = parseJourneyLeg

parse-rest/journey.js

+14-20
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,22 @@
11
'use strict'
22

3-
const parseScheduledDays = require('../parse/scheduled-days')
3+
const parseJourney = (ctx, j) => { // j = journey
4+
const {profile, opt} = ctx
45

5-
const createParseJourney = (profile, opt) => {
6-
const parseLeg = profile.parseJourneyLeg(profile, opt)
7-
8-
const parseJourney = (j) => {
9-
const legs = j.legs.map(leg => parseLeg(j, leg))
10-
const res = {
11-
type: 'journey',
12-
legs,
13-
// todo: refreshToken
14-
// todo: cycle
15-
// todo: remarks?
16-
}
17-
18-
if (opt.scheduledDays && j.serviceDays) {
19-
res.scheduledDays = parseScheduledDays(profile, j.serviceDays.sDaysB)
20-
}
6+
const res = {
7+
type: 'journey',
8+
legs: j.legs.map(leg => profile.parseJourneyLeg(ctx, leg)),
9+
// todo: refreshToken
10+
// todo: cycle
11+
// todo: remarks?
12+
}
2113

22-
return res
14+
if (opt.scheduledDays && j.serviceDays) {
15+
// todo: year
16+
res.scheduledDays = profile.parseScheduledDays(ctx, j.serviceDays.sDaysB, year)
2317
}
2418

25-
return parseJourney
19+
return res
2620
}
2721

28-
module.exports = createParseJourney
22+
module.exports = parseJourney

parse-rest/location.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ const {parse} = require('qs')
44

55
const leadingZeros = /^0+/
66

7-
const parseLocation = (profile, opt, _, l) => {
7+
const parseLocation = (ctx, l) => {
8+
const {profile, opt} = ctx
9+
810
const id = parse(l.id, {delimiter: '@'})
911
const latitude = 'number' === typeof l.lat ? l.lat : (id.Y ? id.Y / 100000 : null)
1012
const longitude = 'number' === typeof l.long ? l.long : (id.X ? id.X / 100000 : null)
@@ -19,19 +21,18 @@ const parseLocation = (profile, opt, _, l) => {
1921
const stop = {
2022
type: 'stop',
2123
id: res.id,
22-
name: l.name || id.O ? profile.parseStationName(l.name || id.O) : null,
24+
name: l.name || id.O ? profile.parseStationName(ctx, l.name || id.O) : null,
2325
location: 'number' === typeof res.latitude ? res : null
2426
}
2527

2628
if (opt.linesOfStops && Array.isArray(l.productAtStop)) {
27-
const parse = profile.parseLine(profile, opt, _)
28-
stop.lines = l.productAtStop.map(p => parse({
29+
stop.lines = l.productAtStop.map(p => profile.parseLine(ctx, {
2930
...p, prodCtx: {...p, ...p.prodCtx}
3031
}))
3132
}
3233

3334
if (l.hasMainMast) {
34-
stop.station = parseLocation(profile, opt, _, {
35+
stop.station = parseLocation(ctx, {
3536
type: 'ST',
3637
id: l.mainMastId,
3738
extId: l.mainMastExtId

parse-rest/polyline.js

+16-19
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
'use strict'
22

3-
const createParsePolyline = (profile, opt, _) => {
4-
const parsePolyline = (p) => {
5-
const coordinates = []
6-
for (let i = 0; i < p.crd.length; i += 2) {
7-
coordinates.push({
8-
type: 'Feature',
9-
properties: {},
10-
geometry: {
11-
type: 'Point',
12-
coordinates: [p.crd[i], p.crd[i + 1]]
13-
}
14-
})
15-
}
3+
const parsePolyline = (ctx, p) => {
4+
const coordinates = []
5+
for (let i = 0; i < p.crd.length; i += 2) {
6+
coordinates.push({
7+
type: 'Feature',
8+
properties: {},
9+
geometry: {
10+
type: 'Point',
11+
coordinates: [p.crd[i], p.crd[i + 1]]
12+
}
13+
})
14+
}
1615

17-
return {
18-
type: 'FeatureCollection',
19-
features: coordinates
20-
}
16+
return {
17+
type: 'FeatureCollection',
18+
features: coordinates
2119
}
22-
return parsePolyline
2320
}
2421

25-
module.exports = createParsePolyline
22+
module.exports = parsePolyline

parse-rest/trip.js

+32-34
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,38 @@ const sortBy = require('lodash/sortBy')
44
const last = require('lodash/last')
55
const pick = require('lodash/pick')
66

7-
const createParseTrip = (profile, opt, data) => {
8-
const parseTrip = (t) => {
9-
const product = t.products && t.products[0] && t.products[0].Product
10-
const direction = t.directions && t.directions[0] && t.directions[0].value
11-
12-
const parseS = profile.parseStopover(profile, opt, data)
13-
const stopovers = sortBy(t.stops, 'routeIdx').map(st => parseS(null, st))
14-
const dep = stopovers[0]
15-
const arr = last(stopovers)
16-
17-
return {
18-
origin: dep.stop,
19-
destination: arr.stop,
20-
line: product ? profile.parseLine(profile, opt, data)(product) : null,
21-
direction: direction || null,
22-
23-
...pick(dep, [
24-
'departure',
25-
'plannedDeparture',
26-
'departureDelay',
27-
'prognosedDeparture'
28-
]),
29-
...pick(arr, [
30-
'arrival',
31-
'plannedArrival',
32-
'arrivalDelay',
33-
'prognosedArrival'
34-
]),
35-
36-
stopovers
37-
}
7+
const parseTrip = (ctx, t) => {
8+
const {profile} = ctx
9+
10+
const product = t.products && t.products[0] && t.products[0].Product
11+
const direction = t.directions && t.directions[0] && t.directions[0].value
12+
13+
const stopovers = sortBy(t.stops, 'routeIdx')
14+
.map(st => profile.parseStopover(ctx, st, date)) // todo: date
15+
const dep = stopovers[0]
16+
const arr = last(stopovers)
17+
18+
return {
19+
origin: dep.stop,
20+
destination: arr.stop,
21+
line: product ? profile.parseLine(ctx, product) : null,
22+
direction: direction || null,
23+
24+
...pick(dep, [
25+
'departure',
26+
'plannedDeparture',
27+
'departureDelay',
28+
'prognosedDeparture'
29+
]),
30+
...pick(arr, [
31+
'arrival',
32+
'plannedArrival',
33+
'arrivalDelay',
34+
'prognosedArrival'
35+
]),
36+
37+
stopovers
3838
}
39-
40-
return parseTrip
4139
}
4240

43-
module.exports = createParseTrip
41+
module.exports = parseTrip

0 commit comments

Comments
 (0)