All files / datepicker/components si-compare-adapter.ts

100% Statements 17/17
100% Branches 1/1
100% Functions 15/15
100% Lines 17/17

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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108                                                                            378x       1534x       50849x       138x       7x                 194x       67x       4903x       84x       888x           63x       4x       5672x       4x 1x   3x       1044x      
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import {
  isAfter,
  isAfterMonth,
  isAfterYear,
  isBetween,
  isBetweenMonth,
  isBetweenYears,
  isSameDate,
  isSameMonth,
  isSameOrBefore,
  isSameOrBeforeMonth,
  isSameOrBeforeYear,
  isSameOrBetween,
  isSameOrBetweenMonth,
  isSameOrBetweenYears,
  isSameYear
} from '../date-time-helper';
 
/**
 * Compare the dates based on the active view.
 */
export interface CompareAdapter {
  isAfter(current: Date, start: Date): boolean;
  isBetween(current: Date, from?: Date, to?: Date): boolean;
  isEqual(current: Date, other?: Date): boolean;
  isEqualOrBefore(current: Date, other: Date): boolean;
  isEqualOrBetween(current: Date, from?: Date, to?: Date): boolean;
}
 
/**
 * Compare dates in the month view.
 */
export class DayCompareAdapter implements CompareAdapter {
  isAfter(current: Date, start: Date): boolean {
    return isAfter(current, start);
  }
 
  isBetween(current: Date, from?: Date | undefined, to?: Date | undefined): boolean {
    return isBetween(current, from, to);
  }
 
  isEqual(current: Date, other: Date): boolean {
    return isSameDate(current, other);
  }
 
  isEqualOrBefore(current: Date, other: Date): boolean {
    return isSameOrBefore(current, other);
  }
 
  isEqualOrBetween(current: Date, from?: Date, to?: Date): boolean {
    return isSameOrBetween(current, from, to);
  }
}
 
/**
 * Compare dates in the year view.
 */
export class MonthCompareAdapter implements CompareAdapter {
  isAfter(current: Date, start: Date): boolean {
    return isAfterMonth(current, start);
  }
 
  isBetween(current: Date, from?: Date | undefined, to?: Date | undefined): boolean {
    return isBetweenMonth(current, from, to);
  }
 
  isEqual(current: Date, other: Date): boolean {
    return isSameMonth(current, other);
  }
 
  isEqualOrBefore(current: Date, other: Date): boolean {
    return isSameOrBeforeMonth(current, other);
  }
 
  isEqualOrBetween(current: Date, from?: Date, to?: Date): boolean {
    return isSameOrBetweenMonth(current, from, to);
  }
}
 
export class YearCompareAdapter implements CompareAdapter {
  isAfter(current: Date, other: Date): boolean {
    return isAfterYear(current, other);
  }
 
  isBetween(current: Date, from?: Date | undefined, to?: Date | undefined): boolean {
    return isBetweenYears(current, from, to);
  }
 
  isEqual(current: Date, other: Date): boolean {
    return isSameYear(current, other);
  }
 
  isEqualOrBefore(current: Date, other?: Date): boolean {
    if (!other) {
      return false;
    }
    return isSameOrBeforeYear(current, other);
  }
 
  isEqualOrBetween(current: Date, from?: Date, to?: Date): boolean {
    return isSameOrBetweenYears(current, from, to);
  }
}