hk-bus-eta 1.7.6 → 1.8.2

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.
package/Ctb.js CHANGED
@@ -9,7 +9,7 @@ module.exports = {
9
9
  response => response.json()
10
10
  ).then(({data}) => (
11
11
  data
12
- .filter(eta => eta.eta && eta.dir === bound)
12
+ .filter(eta => eta.eta && bound.includes(eta.dir))
13
13
  // filter the eta by the stop sequence information
14
14
  // as the route data may not 100% match
15
15
  // use the nearest seq
package/Kmb.js CHANGED
@@ -11,8 +11,9 @@ module.exports = {
11
11
  data.filter(e =>
12
12
  e.eta !== null
13
13
  && e.dir === bound
14
- && e.seq === seq + 1 // api return 1-based seq
15
14
  )
15
+ .sort((a,b) => Math.abs(a.seq - seq) < Math.abs(b.seq - seq) ? -1 : 1 )
16
+ .filter((eta, idx, self) => eta.seq === self[0].seq)
16
17
  .map(e => ({
17
18
  eta: e.eta,
18
19
  remark: {
package/Nwfb.js CHANGED
@@ -9,7 +9,7 @@ module.exports = {
9
9
  response => response.json()
10
10
  ).then(({data}) => (
11
11
  data
12
- .filter(eta => eta.eta && eta.dir === bound)
12
+ .filter(eta => eta.eta && bound.includes(eta.dir))
13
13
  // filter the eta by the stop sequence information
14
14
  // as the route data may not 100% match
15
15
  // use the nearest seq
package/index.d.ts CHANGED
@@ -28,7 +28,7 @@ export type RouteListEntry = {
28
28
  [co in Company]: string[]
29
29
  },
30
30
  readonly bound: {
31
- [co in Company]: "O" | "I"
31
+ [co in Company]: "O" | "I" | "OI" | "IO"
32
32
  },
33
33
  readonly gtfsId: string,
34
34
  readonly nlbId: string
package/index.js CHANGED
@@ -10,37 +10,42 @@ const MtrApi = require('./Mtr')
10
10
  module.exports = {
11
11
  // seq is 0-based here
12
12
  fetchEtas: async ( {route, stops, bound, dest, seq, serviceType, co, nlbId, gtfsId, language }) => {
13
- let _etas = []
14
- for ( const company_id of co ) {
15
- if (company_id === 'kmb' && stops.kmb ){
16
- _etas = _etas.concat( await KmbApi.fetchEtas({
17
- route,
18
- stopId: stops.kmb[seq],
19
- seq,
20
- serviceType, bound: bound[company_id]
21
- }))
22
- } else if ( company_id === 'ctb' && stops.ctb ) {
23
- _etas = _etas.concat( await CtbApi.fetchEtas({stopId: stops.ctb[seq], route, bound: bound[company_id], seq }))
24
- } else if ( company_id === 'nwfb' && stops.nwfb ) {
25
- _etas = _etas.concat( await NwfbApi.fetchEtas({stopId: stops.nwfb[seq], route, bound: bound[company_id], seq }))
26
- } else if ( company_id === 'nlb' && stops.nlb ) {
27
- _etas = _etas.concat( await NlbApi.fetchEtas({stopId: stops.nlb[seq], nlbId}) )
28
- } else if ( company_id === 'lrtfeeder' && stops.lrtfeeder ) {
29
- _etas = _etas.concat( await LrtfeederApi.fetchEtas({stopId: stops.lrtfeeder[seq], route, language}))
30
- } else if ( company_id === 'gmb' && stops.gmb ) {
31
- _etas = _etas.concat( await GmbApi.fetchEtas({stopId: stops.gmb[seq], gtfsId, seq, bound: bound[company_id]}) )
32
- } else if ( company_id === 'lightRail' && stops.lightRail ) {
33
- _etas = _etas.concat( await LightRailApi.fetchEtas({ stopId: stops.lightRail[seq], route, dest }) )
34
- } else if ( company_id === 'mtr' && stops.mtr ) {
35
- _etas = _etas.concat( await MtrApi.fetchEtas({ stopId: stops.mtr[seq], route, bound: bound.mtr }) )
13
+ try {
14
+ let _etas = []
15
+ for ( const company_id of co ) {
16
+ if (company_id === 'kmb' && stops.kmb ){
17
+ _etas = _etas.concat( await KmbApi.fetchEtas({
18
+ route,
19
+ stopId: stops.kmb[seq],
20
+ seq,
21
+ serviceType, bound: bound[company_id]
22
+ }))
23
+ } else if ( company_id === 'ctb' && stops.ctb ) {
24
+ _etas = _etas.concat( await CtbApi.fetchEtas({stopId: stops.ctb[seq], route, bound: bound[company_id], seq }))
25
+ } else if ( company_id === 'nwfb' && stops.nwfb ) {
26
+ _etas = _etas.concat( await NwfbApi.fetchEtas({stopId: stops.nwfb[seq], route, bound: bound[company_id], seq }))
27
+ } else if ( company_id === 'nlb' && stops.nlb ) {
28
+ _etas = _etas.concat( await NlbApi.fetchEtas({stopId: stops.nlb[seq], nlbId}) )
29
+ } else if ( company_id === 'lrtfeeder' && stops.lrtfeeder ) {
30
+ _etas = _etas.concat( await LrtfeederApi.fetchEtas({stopId: stops.lrtfeeder[seq], route, language}))
31
+ } else if ( company_id === 'gmb' && stops.gmb ) {
32
+ _etas = _etas.concat( await GmbApi.fetchEtas({stopId: stops.gmb[seq], gtfsId, seq, bound: bound[company_id]}) )
33
+ } else if ( company_id === 'lightRail' && stops.lightRail ) {
34
+ _etas = _etas.concat( await LightRailApi.fetchEtas({ stopId: stops.lightRail[seq], route, dest }) )
35
+ } else if ( company_id === 'mtr' && stops.mtr ) {
36
+ _etas = _etas.concat( await MtrApi.fetchEtas({ stopId: stops.mtr[seq], route, bound: bound.mtr }) )
37
+ }
36
38
  }
37
- }
38
39
 
39
- return _etas.sort((a,b) => {
40
- if ( a.eta === '' ) return 1
41
- else if ( b.eta === '' ) return -1
42
- return a.eta < b.eta ? -1 : 1
43
- })
40
+ return _etas.sort((a,b) => {
41
+ if ( a.eta === '' ) return 1
42
+ else if ( b.eta === '' ) return -1
43
+ return a.eta < b.eta ? -1 : 1
44
+ })
45
+ } catch (err) {
46
+ console.error(err)
47
+ return []
48
+ }
44
49
  },
45
50
  fetchEtaObj: () => fetch("https://hkbus.github.io/hk-bus-crawling/routeFareList.min.json").then(r => r.json()),
46
51
  fetchEtaObjMd5: () => fetch('https://hkbus.github.io/hk-bus-crawling/routeFareList.md5').then(r => r.text())
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hk-bus-eta",
3
- "version": "1.7.6",
3
+ "version": "1.8.2",
4
4
  "description": "Query the ETA (Estimated Time of Arrival) of HK Bus",
5
5
  "main": "index.js",
6
6
  "scripts": {