summaryrefslogtreecommitdiff
path: root/src/components/Mixins/TableFilterMixin.js
blob: 58e70c57aa91046f96fdb0a658d7e48a05afdbc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { includes } from 'lodash';

const TableFilterMixin = {
  methods: {
    getFilteredTableData(tableData = [], filters = []) {
      if (filters.length === 0) return tableData;
      // will return all items that match
      // any of the filter tags (not all)
      return tableData.filter(row => {
        let returnRow = false;
        for (const filter of filters) {
          if (includes(row, filter)) {
            returnRow = true;
            break;
          }
        }
        return returnRow;
      });
    },
    getFilteredTableDataByDate(
      tableData = [],
      startDate,
      endDate,
      propertyKey = 'date'
    ) {
      if (!startDate && !endDate) return tableData;
      const startDateInMs = startDate ? startDate.getTime() : 0;
      const endDateInMs = endDate
        ? endDate.getTime()
        : Number.POSITIVE_INFINITY;
      return tableData.filter(row => {
        const date = row[propertyKey];
        if (!(date instanceof Date)) return;

        const dateInMs = date.getTime();
        if (dateInMs >= startDateInMs && dateInMs <= endDateInMs) return row;
      });
    }
  }
};

export default TableFilterMixin;