<commit_before>import { Injectable } from "@angular/core";
import { AbstractControl, ValidatorFn } from "@angular/forms";
import { FormGroup, FormControl } from "@angular/forms/src/model";
import { Subscription } from "rxjs";

@Injectable()
export class FormService {
  constructor() {}

  dateValidator(dateMinControl: AbstractControl, dateMaxControl: AbstractControl): ValidatorFn {
    return (formGroup: FormGroup): { [key: string]: boolean } => {
      const dateMin = dateMinControl.value;
      const dateMax = dateMaxControl.value;
      if (dateMin && dateMax) {
        const formatedDateMin = new Date(dateMin.year, dateMin.month, dateMin.day);
        const formatedDateMax = new Date(dateMax.year, dateMax.month, dateMax.day);
        if (formatedDateMax < formatedDateMin) {
          return {
            invalidDate: true
          };
        } else {
          return null;
        }
      }
      return null;
    };
  }

  /**
   * Check that controlMin is < to controlMax
   * @param minControl
   * @param maxControl
   * @param validatorKeyName: name of the validator
   */
  minMaxValidator(
    minControl: AbstractControl,
    maxControl: AbstractControl,
    validatorKeyName: string
  ): ValidatorFn {
    return (formGroup: FormGroup): { [key: string]: boolean } => {
      const altMin = minControl.value;
      const altMax = maxControl.value;
      if (altMin && altMax && altMin > altMax) {
        return {
          [validatorKeyName]: true
        };
      } else {
        return null;
      }
    };
  }

  hourAndDateValidator(
    dateMinControl: AbstractControl,
    dateMaxControl: AbstractControl,
    hourMinControl: AbstractControl,
    hourMaxControl: AbstractControl
  ) {
    return (formGroup: FormGroup): { [key: string]: boolean } => {
      const invalidHour = this.invalidHour(
        dateMinControl,
        dateMaxControl,
        hourMinControl,
        hourMaxControl
      );
      return invalidHour
        ? {
            invalidHour: true
          }
        : null;
    };
  }

  invalidHour(
    dateMinControl: AbstractControl,
    dateMaxControl: AbstractControl,
    hourMinControl: AbstractControl,
    hourMaxControl: AbstractControl
  ) {
    const hourMin = hourMinControl.value;
    const hourMax = hourMaxControl.value;
    const dateMin = dateMinControl.value;
    const dateMax = dateMaxControl.value;
    // if hour min et pas hour max => set error
    if (hourMin && !hourMax) {
      return true;
      // if hour min et hour max => check validity
    } else if (dateMin && dateMax && hourMin && hourMax) {
      const formatedHourMin = hourMin.split(":").map(h => parseInt(h));
      const formatedHourMax = hourMax.split(":").map(h => parseInt(h));
      const formatedDateMin = new Date(dateMin.year, dateMin.month, dateMin.day);
      const formatedDateMax = new Date(dateMax.year, dateMax.month, dateMax.day);
      if (dateMin && dateMax) {
        formatedDateMin.setHours(formatedHourMin[0], formatedHourMin[1]);
        formatedDateMax.setHours(formatedHourMax[0], formatedHourMax[1]);
      }
      return formatedDateMin > formatedDateMax;
    }<commit_msg>initialize date with month index... - fix #1468<commit_after>import { Injectable } from "@angular/core";
import { AbstractControl, ValidatorFn } from "@angular/forms";
import { FormGroup, FormControl } from "@angular/forms/src/model";
import { Subscription } from "rxjs";

@Injectable()
export class FormService {
  constructor() {}

  dateValidator(dateMinControl: AbstractControl, dateMaxControl: AbstractControl): ValidatorFn {
    return (formGroup: FormGroup): { [key: string]: boolean } => {
      const dateMin = dateMinControl.value;
      const dateMax = dateMaxControl.value;
      if (dateMin && dateMax) {
        const formatedDateMin = new Date(dateMin.year, dateMin.month, dateMin.day);
        const formatedDateMax = new Date(dateMax.year, dateMax.month, dateMax.day);
        if (formatedDateMax < formatedDateMin) {
          return {
            invalidDate: true
          };
        } else {
          return null;
        }
      }
      return null;
    };
  }

  /**
   * Check that controlMin is < to controlMax
   * @param minControl
   * @param maxControl
   * @param validatorKeyName: name of the validator
   */
  minMaxValidator(
    minControl: AbstractControl,
    maxControl: AbstractControl,
    validatorKeyName: string
  ): ValidatorFn {
    return (formGroup: FormGroup): { [key: string]: boolean } => {
      const altMin = minControl.value;
      const altMax = maxControl.value;
      if (altMin && altMax && altMin > altMax) {
        return {
          [validatorKeyName]: true
        };
      } else {
        return null;
      }
    };
  }

  hourAndDateValidator(
    dateMinControl: AbstractControl,
    dateMaxControl: AbstractControl,
    hourMinControl: AbstractControl,
    hourMaxControl: AbstractControl
  ) {
    return (formGroup: FormGroup): { [key: string]: boolean } => {
      const invalidHour = this.invalidHour(
        dateMinControl,
        dateMaxControl,
        hourMinControl,
        hourMaxControl
      );
      return invalidHour
        ? {
            invalidHour: true
          }
        : null;
    };
  }

  invalidHour(
    dateMinControl: AbstractControl,
    dateMaxControl: AbstractControl,
    hourMinControl: AbstractControl,
    hourMaxControl: AbstractControl
  ) {
    const hourMin = hourMinControl.value;
    const hourMax = hourMaxControl.value;
    const dateMin = dateMinControl.value;
    const dateMax = dateMaxControl.value;
    
    // if hour min et pas hour max => set error
    if (hourMin && !hourMax) {
      return true;
      // if hour min et hour max => check validity
    } else if (dateMin && dateMax && hourMin && hourMax) {
      const formatedHourMin = hourMin.split(":").map(h => parseInt(h));
      const formatedHourMax = hourMax.split(":").map(h => parseInt(h));
      // Date month are initialized with month index ... 0 = janvier SO -1 .
      const formatedDateMin = new Date(dateMin.year, dateMin.month -1, dateMin.day);
      const formatedDateMax = new Date(dateMax.year, dateMax.month -1, dateMax.day);      
      
      if (dateMin && dateMax) {
        formatedDateMin.setHours(formatedHourMin[0], formatedHourMin[1]);
        formatedDateMax.setHours(formatedHourMax[0], formatedHourMax[1]);
      }

      
      return formatedDateMin > formatedDateMax;
    }
{'fraction_non_alphanumeric': 0.10551670551670551, 'fraction_numerical': 0.0024864024864024864, 'mean_word_length': 2.707373271889401, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5522224', 'n_tokens_mistral': 1892, 'n_tokens_neox': 1885, 'n_words': 457}