@jonstuebe/allot-utils
Version:
Util functions for Allot
63 lines • 2.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const date_fns_1 = require("date-fns");
const utils_1 = require("./utils");
function getFutureBillDates({ due, startOn }, numDates = 3) {
if (due.weekly) {
let weekly = due.weekly;
if (date_fns_1.getDay(startOn) !== weekly) {
throw new Error("startOn must have the same day of week as the due date of the bill");
}
return new Array(numDates).fill(startOn).map((date, index) => {
return date_fns_1.addWeeks(date, index);
});
}
else if (due.monthly) {
let monthly = due.monthly;
return new Array(numDates).fill(startOn).map((_date, index) => {
const date = date_fns_1.addMonths(startOn, index);
const daysInMonth = date_fns_1.getDaysInMonth(date);
return date_fns_1.setDate(date, monthly > daysInMonth ? daysInMonth : monthly);
});
}
else if (due.yearly) {
let [yearMonth, yearDayOfMonth] = due.yearly;
// these should be zero indexed
const yearlyDate = new Date(startOn.getFullYear(), yearMonth, yearDayOfMonth);
if (!date_fns_1.isEqual(startOn, yearlyDate)) {
throw new Error("startOn must have the same month and day as the due date of the bill");
}
return new Array(numDates).fill(yearlyDate).map((_date, index) => {
return date_fns_1.addYears(yearlyDate, index);
});
}
throw new Error("No schedule added");
}
exports.getFutureBillDates = getFutureBillDates;
function addFutureBillDates(bill, numDates = 3) {
return Object.assign(Object.assign({}, bill), { dueDates: getFutureBillDates(bill, numDates) });
}
exports.addFutureBillDates = addFutureBillDates;
function getBillAmountForPayPeriod(bill, payPeriod) {
const dates = getBillDatesForPayPeriod(bill, payPeriod);
return bill.amount * dates.length;
}
exports.getBillAmountForPayPeriod = getBillAmountForPayPeriod;
function isBillInPayPeriod(bill, payPeriod) {
return getBillDatesForPayPeriod(bill, payPeriod).length > 0;
}
exports.isBillInPayPeriod = isBillInPayPeriod;
function getBillDatesForPayPeriod({ dueDates }, { start, end }) {
if (dueDates.length === 0) {
throw new Error("No due dates found in bill. Have you added them?");
}
return dueDates.filter(dueDate => {
return utils_1.isBetween(dueDate, start, end, true);
});
}
exports.getBillDatesForPayPeriod = getBillDatesForPayPeriod;
function createBill(bill) {
return Object.assign({ dueDates: [] }, bill);
}
exports.createBill = createBill;
//# sourceMappingURL=bill.js.map