diff options
author | Rosyid Haryadi <rosyid_haryadi@protonmail.com> | 2023-10-02 16:04:51 +0700 |
---|---|---|
committer | Rosyid Haryadi <rosyid_haryadi@protonmail.com> | 2023-10-02 16:04:51 +0700 |
commit | 4defd9e08d1acab49e30058dc48f532b29a455c4 (patch) | |
tree | 67d6a8b2366f3b3dbfc6da8b8f871509ef72c8cc | |
parent | d9c63e0e81aeaa829d7e0ef2cdbf461a3fca3f46 (diff) |
refactor: more robust segment-multiplier relation
-rw-r--r-- | src/Result.jsx | 28 |
1 files changed, 13 insertions, 15 deletions
diff --git a/src/Result.jsx b/src/Result.jsx index c8a367e..0dc9b9a 100644 --- a/src/Result.jsx +++ b/src/Result.jsx @@ -60,13 +60,13 @@ function fetchApi(year) { } function segmentTime(duration, segments) { - const segmentSum = segments.reduce((partialSum, a) => partialSum + a, 0); - const segmentsFilled = [...segments, 24 - segmentSum]; + // const segmentSum = segments.reduce((partialSum, a) => partialSum + a, 0); + // const segmentsFilled = [...segments, 24 - segmentSum]; const segmentedHours = []; - for (const[index, segmentDuration] of segmentsFilled.entries()) { - if (index < segmentsFilled.length - 1) { - const hours = Math.min(duration, segmentDuration); + for (const[index, segmentDuration] of segments.entries()) { + if (index < segments.length - 1) { + const hours = Math.min(duration, segmentDuration.segment); segmentedHours.push(hours); duration -= hours; } else { @@ -92,25 +92,23 @@ function calculatePerDay(entry, baseSalary, holidayData) { const overtimeDuration = entry.finish.diff(entry.start, 'hour', true); let multiplier = 0; - // TODO: find better way - const multiplierMap = { + const segmentMap = { workDays: [ - 1.5, // jam pertama - 2 + {segment: 1, multiplier: 1.5}, // jam pertama + {segment: 23, multiplier: 2} ], holidays: [ - 2, // 8 jam pertama - 3, // jam ke 9 - 4 + {segment: 8, multiplier: 2}, // 8 jam pertama + {segment: 1, multiplier: 3}, // jam ke 9 + {segment: 15, multiplier: 4} ] } - const segmentPattern = isHoliday(entry.date) ? [8, 1, 15] : [1, 23]; + const segmentPattern = isHoliday(entry.date) ? segmentMap.holidays : segmentMap.workDays; const segmentedDuration = segmentTime(overtimeDuration, segmentPattern); - const usedMap = isHoliday(entry.date) ? multiplierMap.holidays : multiplierMap.workDays; for (let i = 0; i < segmentedDuration.length; i++) { if (segmentedDuration[i]) - multiplier += segmentedDuration[i] * usedMap[i]; + multiplier += segmentedDuration[i] * segmentPattern[i].multiplier; } return multiplier * hourlyPay; |