@tmlmobilidade/dates 20260616.229.43 → 20260616.1147.27

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.
@@ -4,6 +4,10 @@ export type MergedPathItem = Path & {
4
4
  dwell_time?: null | number;
5
5
  };
6
6
  export interface SegmentTravelTimes {
7
+ legSeconds: {
8
+ formatted: Array<string>;
9
+ raw: Array<null | number>;
10
+ };
7
11
  segmentTravelSeconds: {
8
12
  formatted: Array<string>;
9
13
  raw: Array<null | number>;
@@ -22,7 +26,7 @@ export interface SegmentTravelTimes {
22
26
  };
23
27
  }
24
28
  export declare function toNumberOrNull(value: unknown): null | number;
25
- export declare function computeSegmentSeconds(distanceMeters: null | number, speedKmh: null | number): number;
29
+ export declare function computeSegmentSeconds(distanceMeters: null | number, speedKmh: null | number): null | number;
26
30
  export declare function formatSecondsToTime(timeInSeconds: null | number): string;
27
31
  export declare function computeSegmentTravelTimes(mergedPath: MergedPathItem[]): SegmentTravelTimes;
28
32
  export declare function getMergedPath(basePath: Path[], newPath: StopsParameter['path']): MergedPathItem[];
@@ -12,11 +12,12 @@ export function toNumberOrNull(value) {
12
12
  return null;
13
13
  }
14
14
  export function computeSegmentSeconds(distanceMeters, speedKmh) {
15
- if (!distanceMeters || !speedKmh)
16
- return 0;
17
- // km/h -> m/s
15
+ if (distanceMeters === null || distanceMeters <= 0)
16
+ return null;
17
+ if (speedKmh === null || speedKmh <= 0)
18
+ return null;
18
19
  const speedMs = speedKmh * 1000 / 3600;
19
- return Math.round(distanceMeters / speedMs) || 0;
20
+ return Math.round(distanceMeters / speedMs);
20
21
  }
21
22
  export function formatSecondsToTime(timeInSeconds) {
22
23
  if (timeInSeconds === null || timeInSeconds === undefined)
@@ -37,12 +38,11 @@ export function formatSecondsToTime(timeInSeconds) {
37
38
  export function computeSegmentTravelTimes(mergedPath) {
38
39
  const segmentTravelSeconds = [];
39
40
  const stopDwellSeconds = [];
41
+ const legSeconds = [];
40
42
  for (let i = 0; i < mergedPath.length; i++) {
41
43
  const row = mergedPath[i];
42
- // Dwell applies at the stop itself
43
44
  const dwell = toNumberOrNull(row.dwell_time);
44
- stopDwellSeconds.push(dwell && dwell > 0 ? Math.round(dwell) : null);
45
- // Segment travel time applies from previous stop -> current stop
45
+ stopDwellSeconds.push(dwell !== null && dwell > 0 ? Math.round(dwell) : 0);
46
46
  if (i === 0) {
47
47
  segmentTravelSeconds.push(null);
48
48
  continue;
@@ -51,6 +51,14 @@ export function computeSegmentTravelTimes(mergedPath) {
51
51
  const speedKmh = toNumberOrNull(row.avg_speed);
52
52
  segmentTravelSeconds.push(computeSegmentSeconds(distanceMeters, speedKmh));
53
53
  }
54
+ for (let i = 0; i < mergedPath.length; i++) {
55
+ const nextTravelSeconds = segmentTravelSeconds[i + 1];
56
+ if (nextTravelSeconds === undefined || nextTravelSeconds === null) {
57
+ legSeconds.push(stopDwellSeconds[i] ?? 0);
58
+ continue;
59
+ }
60
+ legSeconds.push((stopDwellSeconds[i] ?? 0) + nextTravelSeconds);
61
+ }
54
62
  const totalTripSecondsWithoutStops = segmentTravelSeconds
55
63
  .filter((v) => typeof v === 'number')
56
64
  .reduce((a, b) => a + b, 0);
@@ -59,18 +67,31 @@ export function computeSegmentTravelTimes(mergedPath) {
59
67
  .reduce((a, b) => a + b, 0);
60
68
  const totalTripSecondsWithStops = totalTripSecondsWithoutStops + totalStopSeconds;
61
69
  return {
62
- segmentTravelSeconds: { formatted: segmentTravelSeconds.map(s => formatSecondsToTime(s)), raw: segmentTravelSeconds },
63
- stopDwellSeconds: { formatted: stopDwellSeconds.map(s => formatSecondsToTime(s)), raw: stopDwellSeconds },
64
- totalTripSecondsWithoutStops: { formatted: formatSecondsToTime(totalTripSecondsWithoutStops), raw: totalTripSecondsWithoutStops },
65
- totalTripSecondsWithStops: { formatted: formatSecondsToTime(totalTripSecondsWithStops), raw: totalTripSecondsWithStops },
70
+ legSeconds: {
71
+ formatted: legSeconds.map(s => formatSecondsToTime(s)),
72
+ raw: legSeconds,
73
+ },
74
+ segmentTravelSeconds: {
75
+ formatted: segmentTravelSeconds.map(s => formatSecondsToTime(s)),
76
+ raw: segmentTravelSeconds,
77
+ },
78
+ stopDwellSeconds: {
79
+ formatted: stopDwellSeconds.map(s => formatSecondsToTime(s)),
80
+ raw: stopDwellSeconds,
81
+ },
82
+ totalTripSecondsWithoutStops: {
83
+ formatted: formatSecondsToTime(totalTripSecondsWithoutStops),
84
+ raw: totalTripSecondsWithoutStops,
85
+ },
86
+ totalTripSecondsWithStops: {
87
+ formatted: formatSecondsToTime(totalTripSecondsWithStops),
88
+ raw: totalTripSecondsWithStops,
89
+ },
66
90
  };
67
91
  }
68
92
  export function getMergedPath(basePath, newPath) {
69
- const editedByStopId = new Map((newPath || [])
70
- .filter(p => p?.stop_id)
71
- .map(p => [p.stop_id, p]));
72
- return basePath.map((baseItem) => {
73
- const edit = editedByStopId.get(baseItem.stop_id);
93
+ return basePath.map((baseItem, i) => {
94
+ const edit = (newPath || [])[i];
74
95
  return {
75
96
  ...baseItem,
76
97
  avg_speed: edit?.avg_speed,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmlmobilidade/dates",
3
- "version": "20260616.229.43",
3
+ "version": "20260616.1147.27",
4
4
  "author": {
5
5
  "email": "iso@tmlmobilidade.pt",
6
6
  "name": "TML-ISO"