All files / helpers time.helper.ts

12.5% Statements 3/24
0% Branches 0/7
50% Functions 1/2
12.5% Lines 3/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66                                                                                                                        3x 3x   3x    
import { DateTime } from "luxon";
import { Periodicity } from "../constants";
import { Timeline } from "../models/timeline.model";
 
export abstract class TimeHelper {
 
  static calculateTimeIntervals(timeline: Timeline, periodicity: Periodicity): Timeline[] {
 
    const internals: Timeline[] = []
 
    let timeCursor = DateTime.fromJSDate(timeline.begin);
    const timeLimit = DateTime.fromJSDate(timeline.end);
 
    while (timeCursor <= timeLimit) {
      
 
      const begin: Date = timeCursor.toJSDate();
 
      switch (periodicity) {
        case Periodicity.DAY:
          const endCandidateDay = timeCursor.plus({ days: 1 });
          endCandidateDay < timeLimit? timeCursor = endCandidateDay : null;
 
          break;
        case Periodicity.WEEK:
          const endCandidateWeek = timeCursor.plus({ weeks: 1 });
          break;
        case Periodicity.MONTH:
          const endCandidateMonth = timeCursor.plus({ months: 1 });
          break;
        case Periodicity.QUARTER:
        const endCandidateQuarter = timeCursor.plus({ quarter: 1 });
          break;
        case Periodicity.YEAR:
          const endCandidateYear = timeCursor.plus({ year: 1 });
 
 
          break;
      }
 
 
      const end = timeCursor.plus({ [periodicity]: 1 }).toJSDate();
 
      const interval =  new Timeline (
        begin,
        end
      )
 
 
      internals.push(interval);
    }
 
    return internals;
  }
 
  /**
   * Check if two dates points to the same moment in time
   * @returns {boolean} True is they do
   */
  static isSameMoment(alphaDate: Date, betaDate: Date): boolean {
    const alphaUnix = alphaDate.getTime()
    const betaUnix = betaDate.getTime()
 
    return alphaUnix == betaUnix
  }
}