{"size": 1214, "ext": "js", "max_stars_repo_path": "src/citizenapp/src/Utils/findStartAndEndIndex.js", "max_stars_repo_name": "helsenorgelab/pasientdata", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/citizenapp/src/Utils/findStartAndEndIndex.js", "max_issues_repo_name": "helsenorgelab/pasientdata", "max_issues_repo_issues_event_min_datetime": "2019-08-08T14:32:06.000Z", "max_issues_repo_issues_event_max_datetime": "2019-08-08T20:27:02.000Z", "max_forks_repo_path": "src/citizenapp/src/Utils/findStartAndEndIndex.js", "max_forks_repo_name": "helsenorge/pasientdata2019", "max_forks_repo_forks_event_min_datetime": "2019-08-07T10:09:25.000Z", "max_forks_repo_forks_event_max_datetime": "2019-08-07T10:09:25.000Z"}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": 1, "avg_line_length": 25.829787234, "max_line_length": 86, "alphanum_fraction": 0.5560131796}
import moment from "moment";

export default function findStartAndEndIndex(data, start, end) {
  let startIndex = 0;
  let endIndex = data.length - 1;
  let endIndexFound = false;

  if (moment(data[0].start).isBefore(end)) {
    if (
      moment(end, "YYYY-MM-DDTHH:mm:ss").isAfter(
        moment(data[data.length - 1].start, "YYYY-MM-DDTHH:mm:ss")
      )
    ) {
      endIndexFound = true;
    }

    for (let i = 0; i < data.length; i++) {
      if (
        moment(start, "YYYY-MM-DDTHH:mm:ss").isAfter(
          moment(data[i].start, "YYYY-MM-DDTHH:mm:ss")
        )
      ) {
        startIndex = i; // Index right before the first data point we want to include.
      }
      if (
        !endIndexFound &&
        moment(end, "YYYY-MM-DDTHH:mm:ss").isBefore(
          moment(data[i].start, "YYYY-MM-DDTHH:mm:ss")
        )
      ) {
        endIndex = i; // Index right after the first data point we want to include.
        break;
      }
    }
    if (!endIndexFound) {
      endIndex--;
    }
    if (startIndex !== data.length - 1 && startIndex > 0) {
      startIndex++;
    }
  } else {
    return { startIndex: 0, endIndex: 0 };
  }

  return { startIndex: startIndex, endIndex: endIndex };
}
