<commit_before>/**
 * @flow
 * Area.
 * Area is a helper class built on top of the native ClientRect with a few useful methods.
 */
import type { TOptions, TContainOptions, TContainsEdgeAxis } from '../types/Area';

const defaultOptions = {
  absolute: false
};

const defaultContainOptions = {
  weak: false
};

export default class Area {
  /* Public properties */
  top: number;
  right: number;
  bottom: number;
  left: number;
  height: number;
  width: number;

  /* Prototype methods */
  toAbsolute: Function;
  contains: Function;
  containsEdge: Function;
  intersect: Function;

  constructor(pointer: window | HTMLElement | Object, options?: TOptions): Area {
    const { absolute } = { ...defaultOptions, ...options };
    let rect;

    if (pointer === window) {
      rect = {
        top: window.scrollY,
        left: window.scrollX,
        height: window.innerHeight || window.documentElement.clientHeight,
        width: window.innerWidth || window.documentElement.clientWidth
      };
    } else if (pointer instanceof HTMLElement) {
      rect = pointer.getBoundingClientRect();
    } else {
      rect = pointer;
    }

    /**
     * Propagete the properties into the class context.
     * Ensure potentially missing properties are composed from the
     * existing onces.
     */
    this.top = rect.top;
    this.right = rect.right || rect.left + rect.width;
    this.bottom = rect.bottom || rect.top + rect.height;
    this.left = rect.left;

    /* Ensure dimensions */
    this.height = rect.height || this.bottom - this.top;
    this.width = rect.width || this.right - this.left;

    return absolute ? this.toAbsolute() : this;
  }

  /**
   * Compose a new Area based on the current one, which takes scroll position into account
   * when calculating its coordinates. Explicitly returns a new instance of Area to prevent
   * unexpected mutations of the values of the original (source) Area.
   */
  toAbsolute(): Area {
    return new Area({
      top: this.top + window.scrollY,
      right: this.right + window.scrollX,
      bottom: this.bottom + window.scrollY,
      left: this.left + window.scrollX
    });
  }

  /**
   * Check if current area contains the given area or coordinates object.
   */
  contains(area: Area | Object, options?: TContainOptions): boolean {
    const { weak } = { ...defaultContainOptions, ...options };
    const parentArea: Area = this;<commit_msg>Area: Floor the coordinates to prevent uneven matches<commit_after>/**
 * @flow
 * Area.
 * Area is a helper class built on top of the native ClientRect with a few useful methods.
 */
import type { TOptions, TContainOptions, TContainsEdgeAxis } from '../types/Area';

const defaultOptions = {
  absolute: false
};

const defaultContainOptions = {
  weak: false
};

export default class Area {
  /* Public properties */
  top: number;
  right: number;
  bottom: number;
  left: number;
  height: number;
  width: number;

  /* Prototype methods */
  toAbsolute: Function;
  contains: Function;
  containsEdge: Function;
  intersect: Function;

  constructor(pointer: window | HTMLElement | Object, options?: TOptions): Area {
    const { absolute } = { ...defaultOptions, ...options };
    let rect;

    if (pointer === window) {
      rect = {
        top: window.scrollY,
        left: window.scrollX,
        height: window.innerHeight || window.documentElement.clientHeight,
        width: window.innerWidth || window.documentElement.clientWidth
      };
    } else if (pointer instanceof HTMLElement) {
      rect = pointer.getBoundingClientRect();
    } else {
      rect = pointer;
    }

    /**
     * Propagete the properties into the class context.
     * Ensure potentially missing properties are composed from the
     * existing onces.
     */
    this.top = rect.top;
    this.right = rect.right || Math.floor(rect.left + rect.width);
    this.bottom = rect.bottom || Math.floor(rect.top + rect.height);
    this.left = rect.left;

    /* Ensure dimensions */
    this.height = rect.height || Math.floor(this.bottom - this.top);
    this.width = rect.width || Math.floor(this.right - this.left);

    return absolute ? this.toAbsolute() : this;
  }

  /**
   * Compose a new Area based on the current one, which takes scroll position into account
   * when calculating its coordinates. Explicitly returns a new instance of Area to prevent
   * unexpected mutations of the values of the original (source) Area.
   */
  toAbsolute(): Area {
    return new Area({
      top: Math.floor(this.top + window.scrollY),
      right: Math.floor(this.right + window.scrollX),
      bottom: Math.floor(this.bottom + window.scrollY),
      left: Math.floor(this.left + window.scrollX)
    });
  }

  /**
   * Check if current area contains the given area or coordinates object.
   */
  contains(area: Area | Object, options?: TContainOptions): boolean {
    const { weak } = { ...defaultContainOptions, ...options };
    const parentArea: Area = this;
{'fraction_non_alphanumeric': 0.11368209255533199, 'fraction_numerical': 0.0, 'mean_word_length': 2.859472049689441, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6145080', 'n_tokens_mistral': 1409, 'n_tokens_neox': 1394, 'n_words': 452}