1 | import dayjs from '../date'; |
||
2 | |||
3 | const WEEKEND_INDEXES = new Set([ 0, 6 ]); // eslint-disable-line no-magic-numbers |
||
4 | |||
5 | export function workingDays({ include = [], exclude = [], to, from }) { |
||
6 | const start = dayjs.min([ dayjs(from), ...include ]); |
||
7 | const end = dayjs.max([ dayjs(to), ...include ]); |
||
8 | const totalDays = dayjs(end).diff(dayjs(start), 'day') + 1; |
||
9 | const days = []; |
||
10 | |||
11 | for (let i = 0; i < totalDays; i++) { |
||
12 | const day = dayjs(from).add(i, 'days').startOf('day'); |
||
13 | |||
14 | let insert = !WEEKEND_INDEXES.has(day.day()); |
||
15 | |||
16 | if (exclude.some(d => d.isSame(day, 'day'))) { |
||
17 | insert = false; |
||
18 | } |
||
19 | |||
20 | if (day > dayjs(to) || day < dayjs(from)) { |
||
21 | insert = false; |
||
22 | } |
||
23 | |||
24 | if (include.some(d => d.isSame(day, 'day'))) { |
||
25 | insert = true; |
||
26 | } |
||
27 | |||
28 | if (insert) days.push(day); |
||
0 ignored issues
–
show
|
|||
29 | } |
||
30 | |||
31 | return days; |
||
32 | } |
||
33 |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.