编辑代码

// 平均值
function average(nums) {
    return nums.length === 0 ? 0 : (nums.reduce((a, b) => a + b) / nums.length).toFixed(2)
}
// 总和
function sum(nums) {
    return nums.length === 0 ? 0 : nums.reduce((a, b) => a + b).toFixed(2)
}
// 百分比
function percentage(num, total) {
    return num === 0 || total === 0 ? 0 : Math.round((num / total) * 10000) / 100.0
}
// 分组
function groupByKey(result, key1, key2) {
    return result.reduce((groups, item) => {
    const category = item[key1]
    const subCategory = item[key2]
    if (!groups[category]) {
        groups[category] = {}
    }
    if (!groups[category][subCategory]) {
        groups[category][subCategory] = []
    }
    groups[category][subCategory].push(item)
    return groups
    }, {})
}
// 假期
const holidaySetArray = [
    '2023-01-18', '2023-01-19', '2023-01-20', '2023-01-21', '2023-01-22', '2023-01-23', '2023-01-24', '2023-01-25', '2023-01-26', '2023-01-27', '2023-01-28', '2023-01-29',
    '2023-04-05', '2023-04-29', '2023-04-30',
    '2023-05-01', '2023-05-02', '2023-05-03',
    '2023-06-22', '2023-06-23', '2023-06-24',
    '2023-09-29', '2023-09-30',
    '2023-10-01', '2023-10-02', '2023-10-03', '2023-10-04', '2023-10-05', '2023-10-06'
]
// 调休
const fuckHolidaySetArray = [
    '2023-04-23',
    '2023-05-06',
    '2023-06-25',
    '2023-10-07',
    '2023-10-08'
]
// 是不是工作日
function isHoliday(startTime) {
    const date = dayjs(startTime).format('YYYY-MM-DD')
    const week = dayjs(startTime).day()
    if (holidaySetArray.includes(date)) {
        return true
    }
    if (fuckHolidaySetArray.includes(date)) {
        return false
    }
    return week === 0 || week === 6
}
// 当月总工作日数
function calculateWorkingDays(month) {
    const monthEndDayjs = dayjs(month).endOf('month')
    let workingDays = 0
    for (let day = 1; day <= monthEndDayjs.date(); day++) {
        if (!isHoliday(monthEndDayjs.date(day).toDate())) {
            workingDays++
        }
    }
    return workingDays
}