<commit_before>import moment from 'moment-mini'
import { logger } from '../../logger'
import * as d3 from 'd3'

let dateFormat = ''
let axisFormat = ''
let excludes = []
let title = ''
let sections = []
let tasks = []
let currentSection = ''
const tags = ['active', 'done', 'crit', 'milestone']
let funs = []

export const clear = function () {
  sections = []
  tasks = []
  currentSection = ''
  funs = []
  title = ''
  taskCnt = 0
  lastTask = undefined
  lastTaskID = undefined
  rawTasks = []
  dateFormat = ''
  axisFormat = ''
  excludes = []
}

export const setAxisFormat = function (txt) {
  axisFormat = txt
}

export const getAxisFormat = function () {
  return axisFormat
}

export const setDateFormat = function (txt) {
  dateFormat = txt
}

export const getDateFormat = function () {
  return dateFormat
}

export const setExcludes = function (txt) {
  excludes = txt.toLowerCase().split(/[\s,]+/)
}

export const getExcludes = function () {
  return excludes
}

export const setTitle = function (txt) {
  title = txt
}

export const getTitle = function () {
  return title
}

export const addSection = function (txt) {
  currentSection = txt
  sections.push(txt)
}

export const getSections = function () {
  return sections
}

export const getTasks = function () {
  let allItemsPricessed = compileTasks()
  const maxDepth = 10
  let iterationCount = 0
  while (!allItemsPricessed && (iterationCount < maxDepth)) {
    allItemsPricessed = compileTasks()
    iterationCount++
  }

  tasks = rawTasks

  return tasks
}

const isInvalidDate = function (date, dateFormat, excludes) {
  if (date.isoWeekday() >= 6 && excludes.indexOf('weekends') >= 0) {
    return true
  }
  if (excludes.indexOf(date.format('dddd').toLowerCase()) >= 0) {
    return true
  }
  return excludes.indexOf(date.format(dateFormat.trim())) >= 0
}

const checkTaskDates = function (task, dateFormat, excludes) {
  if (!excludes.length || task.manualEndTime) return
  let startTime = moment(task.startTime, dateFormat, true)
  startTime.add(1, 'd')
  let endTime = moment(task.endTime, dateFormat, true)
  let renderEndTime = fixTaskDates(startTime, endTime, dateFormat, excludes)
  task.endTime = endTime.toDate()
  task.renderEndTime = renderEndTime
}

const fixTaskDates = function (startTime, endTime, dateFormat, excludes) {
  let invalid = false
  let renderEndTime = null
  while (startTime.date() <= endTime.date()) {
    if (!invalid) {
      renderEndTime = endTime.toDate()
    }
    invalid = isInvalidDate(startTime, dateFormat, excludes)
    if (invalid) {
      endTime.add(1, 'd')
    }
    startTime.add(1, 'd')
  }
  return renderEndTime
}

const getStartDate = function (prevTime, dateFormat, str) {
  str = str.trim()

  // Test for after
  const re = /^after\s+([\d\w-]+)/
  const afterStatement = re.exec(str.trim())

  if (afterStatement !== null) {
    const task = findTaskById(afterStatement[1])

    if (typeof task === 'undefined') {
      const dt = new Date()
      dt.setHours(0, 0, 0, 0)
      return dt
    }
    return task.endTime
  }

  // Check for actual date set
  let mDate = moment(str, dateFormat.trim(), true)
  if (mDate.isValid()) {
    return mDate.toDate()
  } else {
    logger.debug('Invalid date:' + str)
    logger.debug('With date format:' + dateFormat.trim())
  }

  // Default date - now
  return new Date()
}

const getEndDate = function (prevTime, dateFormat, str) {
  str = str.trim()

  // Check for actual date
  let mDate = moment(str, dateFormat.trim(), true)
  if (mDate.isValid()) {
    return mDate.toDate()
  }

  const d = moment(prevTime)
  // Check for length
  const re = /^([\d]+)([wdhms])/
  const durationStatement = re.exec(str.trim())

  if (durationStatement !== null) {
    switch (durationStatement[2]) {
      case 's':
        d.add(durationStatement[1], 'seconds')
        break
      case 'm':
        d.add(durationStatement[1], 'minutes')
        break
      case 'h':
        d.add(durationStatement[1], 'hours')
        break
      case 'd':
        d.add(durationStatement[1], 'days')<commit_msg>feat(ganttDb.js): add 1 day to end dates if inclusive is set to true<commit_after>import moment from 'moment-mini'
import { logger } from '../../logger'
import * as d3 from 'd3'

let dateFormat = ''
let axisFormat = ''
let excludes = []
let title = ''
let sections = []
let tasks = []
let currentSection = ''
const tags = ['active', 'done', 'crit', 'milestone']
let funs = []
let inclusiveEndDates = false

export const clear = function () {
  sections = []
  tasks = []
  currentSection = ''
  funs = []
  title = ''
  taskCnt = 0
  lastTask = undefined
  lastTaskID = undefined
  rawTasks = []
  dateFormat = ''
  axisFormat = ''
  excludes = []
  inclusiveEndDates = false
}

export const setAxisFormat = function (txt) {
  axisFormat = txt
}

export const getAxisFormat = function () {
  return axisFormat
}

export const setDateFormat = function (txt, inclusive) {
  dateFormat = txt
  inclusiveEndDates = inclusive || false // make sure it's not undefined
}

export const getDateFormat = function () {
  return dateFormat
}

export const setExcludes = function (txt) {
  excludes = txt.toLowerCase().split(/[\s,]+/)
}

export const getExcludes = function () {
  return excludes
}

export const setTitle = function (txt) {
  title = txt
}

export const getTitle = function () {
  return title
}

export const addSection = function (txt) {
  currentSection = txt
  sections.push(txt)
}

export const getSections = function () {
  return sections
}

export const getTasks = function () {
  let allItemsPricessed = compileTasks()
  const maxDepth = 10
  let iterationCount = 0
  while (!allItemsPricessed && (iterationCount < maxDepth)) {
    allItemsPricessed = compileTasks()
    iterationCount++
  }

  tasks = rawTasks

  return tasks
}

const isInvalidDate = function (date, dateFormat, excludes) {
  if (date.isoWeekday() >= 6 && excludes.indexOf('weekends') >= 0) {
    return true
  }
  if (excludes.indexOf(date.format('dddd').toLowerCase()) >= 0) {
    return true
  }
  return excludes.indexOf(date.format(dateFormat.trim())) >= 0
}

const checkTaskDates = function (task, dateFormat, excludes) {
  if (!excludes.length || task.manualEndTime) return
  let startTime = moment(task.startTime, dateFormat, true)
  startTime.add(1, 'd')
  let endTime = moment(task.endTime, dateFormat, true)
  let renderEndTime = fixTaskDates(startTime, endTime, dateFormat, excludes)
  task.endTime = endTime.toDate()
  task.renderEndTime = renderEndTime
}

const fixTaskDates = function (startTime, endTime, dateFormat, excludes) {
  let invalid = false
  let renderEndTime = null
  while (startTime.date() <= endTime.date()) {
    if (!invalid) {
      renderEndTime = endTime.toDate()
    }
    invalid = isInvalidDate(startTime, dateFormat, excludes)
    if (invalid) {
      endTime.add(1, 'd')
    }
    startTime.add(1, 'd')
  }
  return renderEndTime
}

const getStartDate = function (prevTime, dateFormat, str) {
  str = str.trim()

  // Test for after
  const re = /^after\s+([\d\w-]+)/
  const afterStatement = re.exec(str.trim())

  if (afterStatement !== null) {
    const task = findTaskById(afterStatement[1])

    if (typeof task === 'undefined') {
      const dt = new Date()
      dt.setHours(0, 0, 0, 0)
      return dt
    }
    return task.endTime
  }

  // Check for actual date set
  let mDate = moment(str, dateFormat.trim(), true)
  if (mDate.isValid()) {
    return mDate.toDate()
  } else {
    logger.debug('Invalid date:' + str)
    logger.debug('With date format:' + dateFormat.trim())
  }

  // Default date - now
  return new Date()
}

const getEndDate = function (prevTime, dateFormat, str, inclusive) {
  inclusive = inclusive || false
  str = str.trim()

  // Check for actual date
  let mDate = moment(str, dateFormat.trim(), true)
  if (mDate.isValid()) {
    if (inclusive) {
      mDate.add(1, 'd')
    }
    return mDate.toDate()
  }

  const d = moment(prevTime)
  // Check for length
  const re = /^([\d]+)([wdhms])/
  const durationStatement = re.exec(str.trim())

  if (durationStatement !== null) {
    switch (durationStatement[2]) {
      case 's':
        d.add(durationStatement[1], 'seconds')
        break
      case 'm':
        d.add(durationStatement[1], 'minutes')
        break
      case 'h':
        d.add(durationStatement[1], 'hours')
        break
      case 'd':
        d.add(durationStatement[1], 'days')
{'fraction_non_alphanumeric': 0.135792220113852, 'fraction_numerical': 0.0056925996204933585, 'mean_word_length': 3.2720364741641337, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '4014151', 'n_tokens_mistral': 2766, 'n_tokens_neox': 2702, 'n_words': 739}