Skip to content

Commit

Permalink
Merge pull request #4 from rankmyapp/fix/business-diff-calc
Browse files Browse the repository at this point in the history
FIx/Business Diff Calc
  • Loading branch information
AleLira authored Aug 6, 2021
2 parents 955e1e7 + 89f166e commit a30591f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dayjs-business-time",
"version": "1.0.3",
"version": "1.0.4",
"author": "Alexsander Lira",
"main": "dist/index.js",
"types": "types/index.d.ts",
Expand Down
4 changes: 1 addition & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,7 @@ const businessTime = (
break;
} else if (from.isSameOrAfter(start) && from.isSameOrBefore(end)) {
diff += end.diff(from, 'minutes');
} else {
diff += end.diff(start, 'minutes');
}
}
}

return diff ? diff * multiplier : 0;
Expand Down
24 changes: 22 additions & 2 deletions test/businessHoursDiff.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,32 @@ describe('Business Hours Diff', () => {
});

it('should get the -3.5 business hours diff between 2 times in the same day with timezone', () => {
const start = dayjs('2021-05-17T15:30:00-03:00');
const end = dayjs('2021-05-17T12:00:00-03:00');
const start = dayjs('2021-05-17T13:30:00-03:00');
const end = dayjs('2021-05-17T10:00:00-03:00');

const diff = start.businessHoursDiff(end);

expect(diff).toBeDefined();
expect(diff).toBe(-3.5);
});

it('should get the 1 business hour diff between 2 times in the same business hours segment in a day with 2 segments', () => {
const start = dayjs('2021-05-26 16:00:00');
const end = dayjs('2021-05-26 17:00:00');

const diff = start.businessHoursDiff(end);

expect(diff).toBeDefined();
expect(diff).toBe(1);
});

it('should get the 9.5 business hour diff between 2 times in 2 business days with a day with 2 segments', () => {
const start = dayjs('2021-05-25 14:30:00');
const end = dayjs('2021-05-26 17:00:00');

const diff = start.businessHoursDiff(end);

expect(diff).toBeDefined();
expect(diff).toBe(9.5);
});
});

0 comments on commit a30591f

Please sign in to comment.