@jbouduin/holidays-lib
Version:
Get World-Wide Holidays
48 lines (47 loc) • 1.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Mover = void 0;
var configuration_1 = require("../configuration");
var Mover = /** @class */ (function () {
// </editor-fold>
// <editor-fold desc='Constructor & C°'>
function Mover(calendarHelper) {
this.calendarHelper = calendarHelper;
}
// </editor-fold>
// <editor-fold desc='IMover interface methods'>
Mover.prototype.moveDate = function (moves, date) {
var _this = this;
var cnt = 0;
var original = date;
var applicableMoves = moves.filter(function (move) { return _this.mustMove(move, date); });
while (applicableMoves.length > 0 && cnt < 32) {
cnt++;
date = this.applyMove(applicableMoves[0], date);
applicableMoves = moves.filter(function (move) { return _this.mustMove(move, date); });
}
return cnt === 32 ? original : date;
};
// </editor-fold>
// <editor-fold desc='Private helper methods'>
Mover.prototype.mustMove = function (move, date) {
var day = date.getDay();
if (move.condition === configuration_1.Condition.IS_WEEKEND) {
return day === 0 || day === 6;
}
else {
return day === move.condition;
}
};
Mover.prototype.applyMove = function (move, date) {
var diff = date.getDay() > move.weekday ?
move.weekday - date.getDay() + 7 :
move.weekday - date.getDay();
if (move.moveTo === configuration_1.MoveTo.PREVIOUS) {
diff = diff - 7;
}
return this.calendarHelper.addDays(date, diff);
};
return Mover;
}());
exports.Mover = Mover;