bus-mj 2.2.5 → 2.2.6

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/getBus.js +30 -7
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bus-mj",
3
- "version": "2.2.5",
3
+ "version": "2.2.6",
4
4
  "description": "package for BUS station in Mahajanga",
5
5
  "type": "module",
6
6
  "main": "index.js",
package/src/getBus.js CHANGED
@@ -66,7 +66,10 @@ export function findBusByOneStop(stop_id) {
66
66
  */
67
67
  export function findBusByStopLabel(stop_label) {
68
68
  return relations.filter((relation) => {
69
- return relation.members.some((member) => member.label && member.label.toUpperCase() == stop_label.toUpperCase());
69
+ return relation.members.some(
70
+ (member) =>
71
+ member.label && member.label.toUpperCase() == stop_label.toUpperCase()
72
+ );
70
73
  });
71
74
  }
72
75
 
@@ -76,10 +79,30 @@ export function findBusByStopLabel(stop_label) {
76
79
  * @param {Bus[]} __relation list of bus to filter
77
80
  * @returns { Bus[] }
78
81
  */
79
- export function __findBusByStopLabel(stop_label, __relation) {
80
- return __relation.filter((rel) => {
81
- return rel.members.some((member) => member.label && member.label.toUpperCase() == stop_label.toUpperCase());
82
- });
82
+ export function __findBusByStopLabel(stop_label, relations, beginLabel) {
83
+ return relations
84
+ .map((rel) => {
85
+ const beginIndex = rel.members.findIndex(
86
+ (m) => m.label?.toUpperCase() === beginLabel.toUpperCase()
87
+ );
88
+
89
+ const endIndex = rel.members.findIndex(
90
+ (m) => m.label?.toUpperCase() === stop_label.toUpperCase()
91
+ );
92
+
93
+ if (beginIndex === -1 || endIndex === -1) {
94
+ return null;
95
+ }
96
+
97
+ const start = Math.min(beginIndex, endIndex);
98
+ const end = Math.max(beginIndex, endIndex);
99
+
100
+ return {
101
+ ...rel,
102
+ members: rel.members.slice(start, end + 1),
103
+ };
104
+ })
105
+ .filter(Boolean);
83
106
  }
84
107
 
85
108
  /**
@@ -103,7 +126,7 @@ export function findBusByTwoStop(begin, end) {
103
126
  */
104
127
  export function findBusByTwoStopLabel(begin, end) {
105
128
  let firstRelationsFound = findBusByStopLabel(begin);
106
- return __findBusByStopLabel(end, firstRelationsFound);
129
+ return __findBusByStopLabel(end, firstRelationsFound, begin);
107
130
  }
108
131
 
109
132
  /**
@@ -159,4 +182,4 @@ export function findZoneAll() {
159
182
  (value, index, self) =>
160
183
  self.indexOf(value) === index && value != undefined
161
184
  );
162
- }
185
+ }