# all files / src/ sort.js

100% Statements 56/56
100% Branches 28/28
100% Functions 9/9
100% Lines 39/39
1 statement, 2 branches 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
    957×

    7×
    5×

    5×

    5×
    2×

    2×
    1×

    4×

    7×
    5×
    3×

    5×

    5×
    1×

    4×
    2×

    2×

    2×
    1×

    1×

    2×

    2×

    1×
    4×

    7×
    7×
    2×

    5×
    5×

    5×
    5×
    13×

    13×
    10×

    3×

    5×

    5×

     import get from 'lodash/get';

    const byColumn = ({ sortingColumns, selectedColumn }) => {
      const sortingColumn = sortingColumns && sortingColumns.length ?
        sortingColumns[0] :
        {};
      let sort = 'asc';

      if (sortingColumn.property === selectedColumn) {
        sort = cycleSort(sortingColumn.sort);

        if (!sort) {
          return [];
        }
      }

      return [
        {
          property: selectedColumn,
          sort
        }
      ];
    };

    const byColumns = ({ sortingColumns, selectedColumn }) => {
      const index = sortingColumns && sortingColumns.map(
        c => c.property
      ).indexOf(selectedColumn);
      let newSortingColumns = [];

      if (!sortingColumns) {
        return [{
          property: selectedColumn,
          sort: 'asc'
        }];
      } else if (index >= 0) {
        newSortingColumns = sortingColumns;

        const newSort = cycleSort(newSortingColumns[index].sort);

        if (newSort) {
          newSortingColumns[index] = {
            property: selectedColumn,
            sort: newSort
          };
        } else {
          newSortingColumns.splice(index, 1);
        }

        return newSortingColumns;
      }

      return [...sortingColumns, {
        property: selectedColumn,
        sort: 'asc'
      }];
    };

    function cycleSort(sort) {
      return sort === 'asc' && 'desc';
    }

    // sorter === lodash orderBy
    // https://lodash.com/docs#orderBy
    const sorter = ({ data, sortingColumns, sort }) => {
      if (!sortingColumns) {
        return data;
      }

      const propertyList = [];
      const orderList = [];

      sortingColumns.forEach((column) => {
        propertyList.push(row => {
          const value = get(row, column.property);

          if (value.toLowerCase) {
            return value.toLowerCase();
          }

          return value;
        });
        orderList.push(column.sort);
      });

      return sort(data, propertyList, orderList);
    };

    export default {
      byColumn,
      byColumns,
      sorter
    };