<commit_before>import { addDays } from 'date-fns';
import { mixinOptionalProps } from './helpers';
import { isObject, isArray, isFunction, has } from './_';
import Locale from './locale';

const millisecondsPerDay = 24 * 60 * 60 * 1000;

export default class DateInfo {
  constructor(config, { order = 0, locale, timezone, isFullDay } = {}) {
    this.isDateInfo = true;
    this.order = order;
    this.locale = locale instanceof Locale ? locale : new Locale(locale);
    this.timezone = timezone;
    this.firstDayOfWeek = this.locale.firstDayOfWeek;

    // Adjust config for simple dates
    if (!isObject(config)) {
      const date = this.locale.normalizeDate(config);
      if (isFullDay) {
        config = {
          start: date,
          end: date,
        };
      } else {
        config = {
          startOn: date,
          endOn: date,
        };
      }
    }

    let start = null;
    let end = null;
    if (config.start) {
      start = this.locale.normalizeDate(config.start, {
        ...this.opts,
        time: '00:00:00',
      });
    } else if (config.startOn) {
      start = this.locale.normalizeDate(config.startOn, this.opts);
    }
    if (config.end) {
      end = this.locale.normalizeDate(config.end, {
        ...this.opts,
        time: '23:59:59',
      });
    } else if (config.endOn) {
      end = this.locale.normalizeDate(config.endOn, this.opts);
    }

    // Reconfigure start and end dates if needed
    if (start && end && start > end) {
      const temp = start;
      start = end;
      end = temp;
    } else if (start && config.span >= 1) {
      end = addDays(start, config.span - 1);
    }

    // Assign start and end dates
    this.start = start;
    this.startTime = start ? start.getTime() : NaN;
    this.end = end;
    this.endTime = end ? end.getTime() : NaN;

    // Assign spans
    if (start && end) {
      this.daySpan = this.diffInDays(start, end);
      this.weekSpan = this.diffInWeeks(start, end);
      this.monthSpan = this.diffInMonths(start, end);
      this.yearSpan = this.diffInYears(start, end);
    }

    // Assign 'and' condition
    const andOpt = mixinOptionalProps(config, {}, DateInfo.patternProps);
    if (andOpt.assigned) {
      this.on = { and: andOpt.target };
    }
    // Assign 'or' conditions
    if (config.on) {
      const or = (isArray(config.on) ? config.on : [config.on])
        .map(o => {
          if (isFunction(o)) return o;
          const opt = mixinOptionalProps(o, {}, DateInfo.patternProps);
          return opt.assigned ? opt.target : null;
        })
        .filter(o => o);
      if (or.length) this.on = { ...this.on, or };
    }
    // Assign flag if date is complex
    this.isComplex = !!this.on;
  }

  get isDate() {
    return this.startTime && this.startTime === this.endTime;
  }

  get isRange() {
    return !this.isDate;
  }

  get opts() {
    return {
      order: this.order,
      locale: this.locale,
      timezone: this.timezone,
    };
  }

  toDateInfo(date) {
    return date.isDateInfo ? date : new DateInfo(date, this.opts);
  }

  startOfWeek(date) {
    const day = date.getDay() + 1;
    const daysToAdd =
      day >= this.firstDayOfWeek
        ? this.firstDayOfWeek - day
        : -(7 - (this.firstDayOfWeek - day));
    return addDays(date, daysToAdd);
  }

  diffInDays(d1, d2) {
    return Math.round((d2 - d1) / millisecondsPerDay);
  }

  diffInWeeks(d1, d2) {
    return this.diffInDays(this.startOfWeek(d1), this.startOfWeek(d2));
  }

  diffInYears(d1, d2) {
    return d2.getUTCFullYear() - d1.getUTCFullYear();
  }

  diffInMonths(d1, d2) {
    return this.diffInYears(d1, d2) * 12 + (d2.getMonth() - d1.getMonth());
  }

  static get patterns() {
    return {
      dailyInterval: {
        test: (day, interval, di) => {
          const diff =
            di.diffInDays(di.start || new Date(), day.date) % interval;
          console.log(day.date.toISOString(), diff);
          return diff === 0;
        },
      },
      weeklyInterval: {
        test: (day, interval, di) =>
          di.diffInWeeks(di.start || new Date(), day.date) % interval === 0,
      },
      monthlyInterval: {
        test: (day, interval, di) =>
          di.diffInMonths(di.start || new Date(), day.date) % interval === 0,
      },
      yearlyInterval: {
        test: () => (day, interval, di) =>
          di.diffInYears(di.start || new Date(), day.date) % interval === 0,
      },
      days: {
        validate: days => (isArray(days) ? days : [parseInt(days, 10)]),
        test: (day, days) =>
          days.includes(day.day) || days.includes(-day.dayFromEnd),
      },
      weekdays: {
        validate: weekdays =>
          isArray(weekdays) ? weekdays : [parseInt(weekdays, 10)],
        test: (day, weekdays) => weekdays.includes(day.weekday),
      },<commit_msg>Update daily interval test<commit_after>import { addDays } from 'date-fns';
import { mixinOptionalProps } from './helpers';
import { isObject, isArray, isFunction, has } from './_';
import Locale from './locale';

const millisecondsPerDay = 24 * 60 * 60 * 1000;

export default class DateInfo {
  constructor(config, { order = 0, locale, timezone, isFullDay } = {}) {
    this.isDateInfo = true;
    this.order = order;
    this.locale = locale instanceof Locale ? locale : new Locale(locale);
    this.timezone = timezone;
    this.firstDayOfWeek = this.locale.firstDayOfWeek;

    // Adjust config for simple dates
    if (!isObject(config)) {
      const date = this.locale.normalizeDate(config);
      if (isFullDay) {
        config = {
          start: date,
          end: date,
        };
      } else {
        config = {
          startOn: date,
          endOn: date,
        };
      }
    }

    let start = null;
    let end = null;
    if (config.start) {
      start = this.locale.normalizeDate(config.start, {
        ...this.opts,
        time: '00:00:00',
      });
    } else if (config.startOn) {
      start = this.locale.normalizeDate(config.startOn, this.opts);
    }
    if (config.end) {
      end = this.locale.normalizeDate(config.end, {
        ...this.opts,
        time: '23:59:59',
      });
    } else if (config.endOn) {
      end = this.locale.normalizeDate(config.endOn, this.opts);
    }

    // Reconfigure start and end dates if needed
    if (start && end && start > end) {
      const temp = start;
      start = end;
      end = temp;
    } else if (start && config.span >= 1) {
      end = addDays(start, config.span - 1);
    }

    // Assign start and end dates
    this.start = start;
    this.startTime = start ? start.getTime() : NaN;
    this.end = end;
    this.endTime = end ? end.getTime() : NaN;

    // Assign spans
    if (start && end) {
      this.daySpan = this.diffInDays(start, end);
      this.weekSpan = this.diffInWeeks(start, end);
      this.monthSpan = this.diffInMonths(start, end);
      this.yearSpan = this.diffInYears(start, end);
    }

    // Assign 'and' condition
    const andOpt = mixinOptionalProps(config, {}, DateInfo.patternProps);
    if (andOpt.assigned) {
      this.on = { and: andOpt.target };
    }
    // Assign 'or' conditions
    if (config.on) {
      const or = (isArray(config.on) ? config.on : [config.on])
        .map(o => {
          if (isFunction(o)) return o;
          const opt = mixinOptionalProps(o, {}, DateInfo.patternProps);
          return opt.assigned ? opt.target : null;
        })
        .filter(o => o);
      if (or.length) this.on = { ...this.on, or };
    }
    // Assign flag if date is complex
    this.isComplex = !!this.on;
  }

  get isDate() {
    return this.startTime && this.startTime === this.endTime;
  }

  get isRange() {
    return !this.isDate;
  }

  get opts() {
    return {
      order: this.order,
      locale: this.locale,
      timezone: this.timezone,
    };
  }

  toDateInfo(date) {
    return date.isDateInfo ? date : new DateInfo(date, this.opts);
  }

  startOfWeek(date) {
    const day = date.getDay() + 1;
    const daysToAdd =
      day >= this.firstDayOfWeek
        ? this.firstDayOfWeek - day
        : -(7 - (this.firstDayOfWeek - day));
    return addDays(date, daysToAdd);
  }

  diffInDays(d1, d2) {
    return Math.round((d2 - d1) / millisecondsPerDay);
  }

  diffInWeeks(d1, d2) {
    return this.diffInDays(this.startOfWeek(d1), this.startOfWeek(d2));
  }

  diffInYears(d1, d2) {
    return d2.getUTCFullYear() - d1.getUTCFullYear();
  }

  diffInMonths(d1, d2) {
    return this.diffInYears(d1, d2) * 12 + (d2.getMonth() - d1.getMonth());
  }

  static get patterns() {
    return {
      dailyInterval: {
        test: (day, interval, di) =>
          di.diffInDays(di.start || new Date(), day.date) % interval === 0,
      },
      weeklyInterval: {
        test: (day, interval, di) =>
          di.diffInWeeks(di.start || new Date(), day.date) % interval === 0,
      },
      monthlyInterval: {
        test: (day, interval, di) =>
          di.diffInMonths(di.start || new Date(), day.date) % interval === 0,
      },
      yearlyInterval: {
        test: () => (day, interval, di) =>
          di.diffInYears(di.start || new Date(), day.date) % interval === 0,
      },
      days: {
        validate: days => (isArray(days) ? days : [parseInt(days, 10)]),
        test: (day, days) =>
          days.includes(day.day) || days.includes(-day.dayFromEnd),
      },
      weekdays: {
        validate: weekdays =>
          isArray(weekdays) ? weekdays : [parseInt(weekdays, 10)],
        test: (day, weekdays) => weekdays.includes(day.weekday),
      },
{'fraction_non_alphanumeric': 0.14897852278679938, 'fraction_numerical': 0.011524358302776323, 'mean_word_length': 2.1798800799467024, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, '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': '7621126', 'n_tokens_mistral': 3282, 'n_tokens_neox': 3175, 'n_words': 717}