UNPKG

timeperiodjs

Version:
41 lines (40 loc) 1.81 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const IFilter_1 = require("../interfaces/IFilter"); const PeriodDateSorter_1 = __importDefault(require("../sorters/PeriodDateSorter")); class PeriodFilter extends IFilter_1.Filter { constructor(_basePeriod) { super(); this._basePeriod = _basePeriod; // config this._onlyFullyCovered = false; this._cutPeriodsBeyondTheBoundary = false; } filterPeriods(periods, conf) { let periodsToFilter = new PeriodDateSorter_1.default(periods).sort(); if (conf) { this._cutPeriodsBeyondTheBoundary = !!conf.cutPeriodsBeyondTheBoundary; this._onlyFullyCovered = !!conf.onlyFullyCovered; } if (this._onlyFullyCovered) { periodsToFilter = periodsToFilter.filter(period => period.start >= this._basePeriod.start && period.end <= this._basePeriod.end); } else { periodsToFilter = periodsToFilter.filter(period => period.end >= this._basePeriod.start && period.start <= this._basePeriod.end); } if (this._cutPeriodsBeyondTheBoundary) { for (let i = 0; i < periodsToFilter.length; i++) { const currentPeriod = periodsToFilter[i]; if (currentPeriod.start < this._basePeriod.start) currentPeriod.setStart = this._basePeriod.start; if (currentPeriod.end > this._basePeriod.end) currentPeriod.setEnd = this._basePeriod.end; } } return periodsToFilter; } } exports.default = PeriodFilter;