# all files / src/ search.js

100% Statements 83/83
94.74% Branches 36/38
100% Functions 18/18
100% Lines 50/50
1 branch Ignored

    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
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    1277×

    7×

    5×
    5×
    1×

    4×
    4×

    7×

    10×
    10×
    1×

    9×

    9×
    6×

    14×

    7×

    36×
    22×
    22×
    22×
    22×
    22×

    22×
    1×

    22×

    22×

    7×
    6×
    5×
    5×
    1×

    4×

    27×

    24×

    6×
    6×
    6×

    6×
    11×

    11×

    6×

    6×

    7×

    3×

    3×

    3×
    2×

    1×

    7×

     import get from 'lodash/get';

    const multipleColumns = ({
      data, columns, query, strategy, transform
    }) => {
      if (!query) {
        return data;
      }

      return Object.keys(query).reduce(
        (filteredData, searchColumn) =>
          singleColumn({
            data: filteredData,
            columns,
            searchColumn,
            query: query[searchColumn],
            strategy,
            transform
          }),
        data
      );
    };

    const singleColumn = ({
      data, columns, searchColumn = 'all', query, strategy, transform
    }) => {
      if (!query) {
        return data;
      }

      let ret = columns;

      if (searchColumn !== 'all') {
        ret = columns.filter(col => col.cell && col.cell.property === searchColumn);
      }

      return data.filter(row => ret.filter(column => _columnMatches({
        query, column, strategy, transform, row
      })).length > 0);
    };

    const _columnMatches = ({ // eslint-disable-line no-underscore-dangle
      query, column = { cell: {} }, row,
      strategy = strategies.infix, transform = v => v.toLowerCase()
    }) => {
      const property = column.cell.property;
      const value = get(row, property);
      const formatter = column.cell.resolve || (a => a);
      let formattedValue = formatter(value);

      if (typeof formattedValue === 'undefined') {
        formattedValue = '';
      }

      formattedValue = formattedValue.toString ? formattedValue.toString() : '';

      return strategy(transform(query)).evaluate(transform(formattedValue));
    };

    const matches = ({
      value, query, strategy = strategies.infix, transform = v => v.toLowerCase()
    } = {}) => {
      if (!query) {
        return {};
      }

      return strategy(transform(query)).matches(transform(value));
    };

    const infix = queryTerm => ({
      evaluate(searchText) {
        return searchText.indexOf(queryTerm) !== -1;
      },
      matches(searchText) {
        const splitString = searchText.split(queryTerm);
        const result = [];
        let currentPosition = 0;

        for (let x = 0; x < splitString.length; x++) {
          result.push({
            startIndex: currentPosition + splitString[x].length,
            length: queryTerm.length
          });

          currentPosition += splitString[x].length + queryTerm.length;
        }

        result.pop();

        return result;
      }
    });

    const prefix = queryTerm => ({
      evaluate(searchText) {
        return searchText.indexOf(queryTerm) === 0;
      },
      matches(searchText) {
        const prefixIndex = searchText.indexOf(queryTerm);

        if (prefixIndex === 0) {
          return [
            {
              startIndex: 0,
              length: queryTerm.length
            }
          ];
        }

        return [];
      }
    });

    const strategies = {
      infix,
      prefix
    };

    export default {
      multipleColumns,
      singleColumn,
      _columnMatches,
      matches,
      strategies
    };