Skip to content

Commit

Permalink
fix: bug with unsorted dates
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusk committed Jan 24, 2022
1 parent 4e6bd71 commit c035213
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.prettierrc
9 changes: 9 additions & 0 deletions TypeScript/TollCalculator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ describe("TollCalculator", () => {
expect(fee).toBe(16);
});

test("It handles passing billings in unsorted order", () => {
const fee = tollCalculator.getTotalTollFee(Vehicle.Car, [
new Date(Date.UTC(1995, 11, 12, 9, 45)),
new Date(Date.UTC(1995, 11, 12, 8, 40)),
]);

expect(fee).toBe(16);
});

test("In case there are multiple billings in one hour, it only bills the largest one", () => {
const fee = tollCalculator.getTotalTollFee(Vehicle.Car, [
new Date(Date.UTC(1995, 11, 12, 8, 20)),
Expand Down
2 changes: 1 addition & 1 deletion TypeScript/TollCalculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class TollCalculator {
let tempFee = null;
let totalFee = 0;

dates.forEach((date: Date) => {
dates.sort().forEach((date: Date) => {
if (!this.isSameDay(date, startDate)) {
throw "Billings must be in the same day.";
}
Expand Down

0 comments on commit c035213

Please sign in to comment.