hk-bus-eta 1.7.2 → 1.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/Ctb.js +18 -10
  2. package/Nwfb.js +18 -10
  3. package/index.js +2 -2
  4. package/package.json +1 -1
package/Ctb.js CHANGED
@@ -2,20 +2,28 @@ const utils = require('./utils');
2
2
 
3
3
  module.exports = {
4
4
  co: 'ctb',
5
- fetchEtas: ({stopId, route, bound }) => (
5
+ fetchEtas: ({stopId, route, bound, seq }) => (
6
6
  fetch(`https://rt.data.gov.hk//v1/transport/citybus-nwfb/eta/CTB/${stopId}/${route}`, {
7
7
  cache: utils.isSafari ? 'default' : 'no-store'
8
8
  }).then(
9
9
  response => response.json()
10
- ).then(({data}) => data.filter(eta => eta.eta && eta.dir === bound).map(e => ({
11
- eta: e.eta,
12
- remark: {
13
- zh: e.rmk_tc,
14
- en: e.rmk_en
15
- },
16
- co: 'ctb'
17
- }))
18
- )
10
+ ).then(({data}) => (
11
+ data
12
+ .filter(eta => eta.eta && eta.dir === bound)
13
+ // filter the eta by the stop sequence information
14
+ // as the route data may not 100% match
15
+ // use the nearest seq
16
+ .sort((a,b) => Math.abs(a.seq - seq) < Math.abs(b.seq - seq) ? -1 : 1 )
17
+ .filter((eta, idx, self) => eta.seq === self[0].seq)
18
+ .map(e => ({
19
+ eta: e.eta,
20
+ remark: {
21
+ zh: e.rmk_tc,
22
+ en: e.rmk_en
23
+ },
24
+ co: 'ctb'
25
+ }))
26
+ ))
19
27
  ),
20
28
  fetchStopEtas: ( stopId ) => (
21
29
  fetch(`https://rt.data.gov.hk/v1/transport/batch/stop-eta/CTB/${stopId}`, {
package/Nwfb.js CHANGED
@@ -2,20 +2,28 @@ const utils = require('./utils');
2
2
 
3
3
  module.exports = {
4
4
  co: 'nwfb',
5
- fetchEtas: ({stopId, route, bound }) => (
5
+ fetchEtas: ({stopId, route, bound, seq }) => (
6
6
  fetch(`https://rt.data.gov.hk//v1/transport/citybus-nwfb/eta/NWFB/${stopId}/${route}`, {
7
7
  cache: utils.isSafari ? 'default' : 'no-store'
8
8
  }).then(
9
9
  response => response.json()
10
- ).then(({data}) => data.filter(eta => eta.eta && eta.dir === bound).map(e => ({
11
- eta: e.eta,
12
- remark: {
13
- zh: e.rmk_tc,
14
- en: e.rmk_en
15
- },
16
- co: 'nwfb'
17
- }))
18
- )
10
+ ).then(({data}) => (
11
+ data
12
+ .filter(eta => eta.eta && eta.dir === bound)
13
+ // filter the eta by the stop sequence information
14
+ // as the route data may not 100% match
15
+ // use the nearest seq
16
+ .sort((a,b) => Math.abs(a.seq - seq) < Math.abs(b.seq - seq) ? -1 : 1 )
17
+ .filter((eta, idx, self) => eta.seq === self[0].seq)
18
+ .map(e => ({
19
+ eta: e.eta,
20
+ remark: {
21
+ zh: e.rmk_tc,
22
+ en: e.rmk_en
23
+ },
24
+ co: 'nwfb'
25
+ }))
26
+ ))
19
27
  ),
20
28
  fetchStopEtas: ( stopId ) => (
21
29
  fetch(`https://rt.data.gov.hk/v1/transport/batch/stop-eta/NWFB/${stopId}`, {
package/index.js CHANGED
@@ -20,9 +20,9 @@ module.exports = {
20
20
  serviceType, bound: bound[company_id]})
21
21
  )
22
22
  } else if ( company_id === 'ctb' && stops.ctb ) {
23
- _etas = _etas.concat( await CtbApi.fetchEtas({stopId: stops.ctb[seq], route, bound: bound[company_id] }))
23
+ _etas = _etas.concat( await CtbApi.fetchEtas({stopId: stops.ctb[seq], route, bound: bound[company_id], seq }))
24
24
  } else if ( company_id === 'nwfb' && stops.nwfb ) {
25
- _etas = _etas.concat( await NwfbApi.fetchEtas({stopId: stops.nwfb[seq], route, bound: bound[company_id] }))
25
+ _etas = _etas.concat( await NwfbApi.fetchEtas({stopId: stops.nwfb[seq], route, bound: bound[company_id], seq }))
26
26
  } else if ( company_id === 'nlb' && stops.nlb ) {
27
27
  _etas = _etas.concat( await NlbApi.fetchEtas({stopId: stops.nlb[seq], nlbId}) )
28
28
  } else if ( company_id === 'lrtfeeder' && stops.lrtfeeder ) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hk-bus-eta",
3
- "version": "1.7.2",
3
+ "version": "1.7.3",
4
4
  "description": "Query the ETA (Estimated Time of Arrival) of HK Bus",
5
5
  "main": "index.js",
6
6
  "scripts": {