<commit_before>import chrono from 'chrono-node'
import moment from 'moment'
import _ from 'lodash'
import every from 'lodash/collection/every'
import includes from 'lodash/collection/includes'
import Entry from './entry'

export { filterTags }
export { hashTags }
export { parseDateRange }
export { groupEntries }

export default class Query {
  constructor (db) {
    if (!db) throw new Error('Please provide a couchdb instance')

    this.db = db
    this.executed = false
  }

  findEntries (start = null, end = null, tags = []) {
    return this 
  }

  groupByDate () {
    return this 
  }

  exec() {
    return this.db.query()
  }
}

function filterTags (tags = [], doc) {
  if (!tags) // no tags provided, filter nothing
    return true

  const docTags = doc.tags || [] 

  const found = every(tags, t => includes(docTags, t))
  return found
}

function hashTags (tags = []) {
  // add a # before the tag if it doesn't already exist
  return tags.map(tag => tag.startsWith('#') ? tag : '#' + tag)
}

function parseDateRange (range) {
  let days = parseInt(range)
  days = days >= 0 ? days : 6 // default 6 days

  let dates = chrono.parse(range)[0] || [{}]
  // by default, set the date range then check if chrono parsed anything good
  let start = moment().subtract(days, 'days').startOf('day').toDate()
  let end   = moment().endOf('day').toDate();
  if (dates.start && dates.end) {
    start = moment(dates.start.date()).startOf('day').toDate()
    end   = moment(dates.end.date()).endOf('day').toDate()
  }

  return { start, end }
}

function groupEntries (docs) {
  return _.chain(docs) 
  .map(doc => Entry.fromJSON(doc))
  .groupBy(e => { return moment(e.from).startOf('day').format('YYYY-MM-DD') })
  .map((group, d) => { 
    return { 
      ticks: group, 
      date: moment(d, 'YYYY-MM-DD').toDate(),
      minutes: _.reduce(group, (sum, e) => { return sum + e.duration.minutes }, 0)
    }
  })
  .value()
}
<commit_msg>findEntries can accept opts style arg<commit_after>import chrono from 'chrono-node'
import moment from 'moment'
import _ from 'lodash'
import every from 'lodash/collection/every'
import includes from 'lodash/collection/includes'
import Entry from './entry'

export { filterTags }
export { hashTags }
export { parseDateRange }
export { groupEntries }

export default class Query {
  constructor (db) {
    if (!db) throw new Error('Please provide a couchdb instance')

    this.db = db
    this.executed = false
  }

  findEntries ({start = null, end = null, tags = []} = {}) {
    return this 
  }

  groupByDate () {
    return this 
  }

  exec() {
    return this.db.query()
  }
}

function filterTags (tags = [], doc) {
  if (!tags) // no tags provided, filter nothing
    return true

  const docTags = doc.tags || [] 

  const found = every(tags, t => includes(docTags, t))
  return found
}

function hashTags (tags = []) {
  // add a # before the tag if it doesn't already exist
  return tags.map(tag => tag.startsWith('#') ? tag : '#' + tag)
}

function parseDateRange (range) {
  let days = parseInt(range)
  days = days >= 0 ? days : 6 // default 6 days

  let dates = chrono.parse(range)[0] || [{}]
  // by default, set the date range then check if chrono parsed anything good
  let start = moment().subtract(days, 'days').startOf('day').toDate()
  let end   = moment().endOf('day').toDate();
  if (dates.start && dates.end) {
    start = moment(dates.start.date()).startOf('day').toDate()
    end   = moment(dates.end.date()).endOf('day').toDate()
  }

  return { start, end }
}

function groupEntries (docs) {
  return _.chain(docs) 
  .map(doc => Entry.fromJSON(doc))
  .groupBy(e => { return moment(e.from).startOf('day').format('YYYY-MM-DD') })
  .map((group, d) => { 
    return { 
      ticks: group, 
      date: moment(d, 'YYYY-MM-DD').toDate(),
      minutes: _.reduce(group, (sum, e) => { return sum + e.duration.minutes }, 0)
    }
  })
  .value()
}

{'fraction_non_alphanumeric': 0.15862068965517243, 'fraction_numerical': 0.002554278416347382, 'mean_word_length': 3.1927194860813706, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '1399527', 'n_tokens_mistral': 1321, 'n_tokens_neox': 1275, 'n_words': 365}