{"ast":null,"code":"import { constructFrom } from \"./constructFrom.mjs\";\nimport { isSaturday } from \"./isSaturday.mjs\";\nimport { isSunday } from \"./isSunday.mjs\";\nimport { isWeekend } from \"./isWeekend.mjs\";\nimport { toDate } from \"./toDate.mjs\";\n\n/**\n * @name addBusinessDays\n * @category Date Extension Helpers\n * @summary Add the specified number of business days (mon - fri) to the given date.\n *\n * @description\n * Add the specified number of business days (mon - fri) to the given date, ignoring weekends.\n *\n * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).\n *\n * @param date - The date to be changed\n * @param amount - The amount of business days to be added.\n *\n * @returns The new date with the business days added\n *\n * @example\n * // Add 10 business days to 1 September 2014:\n * const result = addBusinessDays(new Date(2014, 8, 1), 10)\n * //=> Mon Sep 15 2014 00:00:00 (skipped weekend days)\n */\nexport function addBusinessDays(date, amount) {\n  const _date = toDate(date);\n  const startedOnWeekend = isWeekend(_date);\n  if (isNaN(amount)) return constructFrom(date, NaN);\n  const hours = _date.getHours();\n  const sign = amount < 0 ? -1 : 1;\n  const fullWeeks = Math.trunc(amount / 5);\n  _date.setDate(_date.getDate() + fullWeeks * 7);\n\n  // Get remaining days not part of a full week\n  let restDays = Math.abs(amount % 5);\n\n  // Loops over remaining days\n  while (restDays > 0) {\n    _date.setDate(_date.getDate() + sign);\n    if (!isWeekend(_date)) restDays -= 1;\n  }\n\n  // If the date is a weekend day and we reduce a dividable of\n  // 5 from it, we land on a weekend date.\n  // To counter this, we add days accordingly to land on the next business day\n  if (startedOnWeekend && isWeekend(_date) && amount !== 0) {\n    // If we're reducing days, we want to add days until we land on a weekday\n    // If we're adding days we want to reduce days until we land on a weekday\n    if (isSaturday(_date)) _date.setDate(_date.getDate() + (sign < 0 ? 2 : -1));\n    if (isSunday(_date)) _date.setDate(_date.getDate() + (sign < 0 ? 1 : -2));\n  }\n\n  // Restore hours to avoid DST lag\n  _date.setHours(hours);\n  return _date;\n}\n\n// Fallback for modularized imports:\nexport default addBusinessDays;","map":null,"metadata":{},"sourceType":"module","externalDependencies":[]}