-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: round also the delay value on punctuality layers (#1202)
* fix: round also the delay value on punctuality layers * fix: display nothing if no realtime for this vehivle * fix: make sure arrival time is not bigger than departure time (included delays) * fix: fix typo
- Loading branch information
Showing
4 changed files
with
104 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// We round the minutes down for departure times and up for arrival times. | ||
export const getDelaySecure = (milliseconds, isArrival) => { | ||
let timeInMs = milliseconds; | ||
if (timeInMs < 0) { | ||
timeInMs = 0; | ||
} | ||
const h = Math.floor(timeInMs / 3600000); | ||
const m = (isArrival ? Math.ceil : Math.floor)((timeInMs % 3600000) / 60000); | ||
|
||
return h * 3600000 + m * 60000; | ||
}; | ||
|
||
// We round the minutes down for departure times and up for arrival times. | ||
const getDelayString = (milliseconds, isArrival) => { | ||
let timeInMs = milliseconds; | ||
if (timeInMs < 0) { | ||
timeInMs = 0; | ||
} | ||
timeInMs = getDelaySecure(timeInMs, isArrival); | ||
const h = Math.floor(timeInMs / 3600000); | ||
const m = (timeInMs % 3600000) / 60000; | ||
|
||
if (h === 0 && m === 0) { | ||
return "+0m"; | ||
} | ||
if (h === 0) { | ||
return `+${m}m`; | ||
} | ||
|
||
let str = `+`; | ||
if (h > 0) { | ||
str += `${h}h`; | ||
} | ||
|
||
if (m > 0) { | ||
str += `${m}m`; | ||
} | ||
return str; | ||
}; | ||
|
||
export default getDelayString; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import getDelayString from "./getDelayString"; | ||
|
||
describe("getDelayString", () => { | ||
it('should return "+0m" when delay <= 0', () => { | ||
expect(getDelayString(0)).toBe("+0m"); | ||
expect(getDelayString(-1)).toBe("+0m"); | ||
}); | ||
|
||
it('should return "+Xm" when delay is between hours and 0', () => { | ||
expect(getDelayString(69000)).toBe("+1m"); | ||
expect(getDelayString(129000)).toBe("+2m"); | ||
}); | ||
|
||
it('should return "+XhXm" when delay is more than 1 hour', () => { | ||
expect(getDelayString(3659000)).toBe("+1h"); | ||
expect(getDelayString(3661000)).toBe("+1h1m"); | ||
expect(getDelayString(36610000)).toBe("+10h10m"); | ||
}); | ||
|
||
it("should ceil the result if it is an arrival time", () => { | ||
expect(getDelayString(3659000, true)).toBe("+1h1m"); | ||
expect(getDelayString(3659000)).toBe("+1h"); | ||
}); | ||
}); |