const Base = require('./Base.js')

class FilterBase extends Base {
  constructor (data, _saved) {
    super(data, _saved)

    this.filters = this.getField('filters', {})
  }

  pruneFilters () {
    const filters = this.filters
    for (const key in filters) {
      const value = filters[key]
      if (!Array.isArray(value) || filters[key].length === 0) {
        delete filters[key]
      }
    }
  }

  validate () {
    this.pruneFilters()
  }

  toObject () {
    if (Base.isMongoDatabase) {
      const map = new Map()
      const filters = this.filters
      for (const key in filters) {
        map.set(key, filters[key])
      }
      return {
        filters: map
      }
    }
    return {
      filters: this.filters
    }
  }
}

module.exports = FilterBase

@@ -733,16 +733,2046 @@
    %7D%0A  %7D
+%0A%0A  /**%0A   * Get the array index of the filter in a category%0A   * @param %7Bstring%7D category%0A   * @param %7Bstring%7D value%0A   * @returns %7Bnumber%7D%0A   */%0A  getFilterIndex (category, value) %7B%0A    value = value.toLowerCase()%0A    const filters = this.filters%5Bcategory%5D%0A    const index = !filters ? -1 : filters.indexOf(value)%0A    return index !== -1%0A  %7D%0A%0A  /**%0A   * Remove a filter from a category%0A   * @param %7Bstring%7D category%0A   * @param %7Bstring%7D value%0A   */%0A  async removeFilter (category, value) %7B%0A    value = value.toLowerCase()%0A    const index = this.getFilterIndex(category, value)%0A    if (index === -1) %7B%0A      throw new Error(%60%22$%7Bvalue%7D%22 does not exist%60)%0A    %7D%0A    this.filters%5Bcategory%5D.splice(index, 1)%0A    return this.save()%0A  %7D%0A%0A  /**%0A   * Add a filter to a category%0A   * @param %7Bstring%7D category%0A   * @param %7Bstring%7D value%0A   */%0A  async addFilter (category, value) %7B%0A    value = value.toLowerCase()%0A    const index = this.getFilterIndex(category, value)%0A    if (index === -1) %7B%0A      throw new Error(%60%22$%7Bvalue%7D already exists%22%60)%0A    %7D%0A    const filters = this.filters%0A    if (!filters%5Bcategory%5D) %7B%0A      filters%5Bcategory%5D = %5B%5D%0A    %7D%0A    filters%5Bcategory%5D.push(value.toLowerCase())%0A    return this.save()%0A  %7D%0A%0A  /**%0A   * Add multiple filters to a category%0A   * @param %7Bstring%7D category%0A   * @param %7Bstring%5B%5D%7D values%0A   */%0A  async addFilters (category, values) %7B%0A    values = values.map(value =%3E value.toLowerCase())%0A    const filters = this.filters%0A    for (const value of values) %7B%0A      const index = this.getFilterIndex(category, value)%0A      if (index === -1) %7B%0A        throw new Error(%60%22$%7Bvalue%7D%22 already exists%60)%0A      %7D%0A    %7D%0A    if (!filters%5Bcategory%5D) %7B%0A      filters%5Bcategory%5D = %5B%5D%0A    %7D%0A    filters%5Bcategory%5D = filters%5Bcategory%5D.concat(values)%0A    return this.save()%0A  %7D%0A%0A  /**%0A   * Remove all filters%0A   */%0A  async removeFilters () %7B%0A    this.filters = %7B%7D%0A    return this.save()%0A  %7D%0A%0A  /**%0A   * If any filters exist%0A   * @returns %7Bboolean%7D%0A   */%0A  hasFilters () %7B%0A    return Object.keys(this.filters).length %3E 0%0A  %7D
 %0A%7D%0A%0Amodu

Add FilterBase methods