# all files / src/ sort.js

100% Statements 67/67
100% Branches 36/36
100% Functions 10/10
100% Lines 40/40
1 statement, 3 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
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    1077×

    7×

    7×

    8×
    8×

    8×

    8×
    4×

    4×
    1×

    7×

    7×

    8×
    8×
    5×

    8×

    8×
    2×

    6×

    4×

    4×

    4×
    3×

    1×

    4×

    2×

    9×
    9×
    2×

    7×
    7×

    7×
    8×
    22×

    22×
    16×

    6×

    8×

    7×

     import get from 'lodash/get';

    const defaultOrder = {
      FIRST: 'asc',
      '': 'asc',
      asc: 'desc',
      desc: ''
    };

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

      if (sortingColumn.property === selectedColumn) {
        sort = sortingOrder[sortingColumn.sort];

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

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

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

      if (!sortingColumns) {
        return [{
          property: selectedColumn,
          sort: sortingOrder.FIRST
        }];
      } else if (index >= 0) {
        // Clone to avoid mutating the original structure
        newSortingColumns = sortingColumns.map(col => ({ ...col }));

        const newSort = sortingOrder[newSortingColumns[index].sort];

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

        return newSortingColumns;
      }

      return [...sortingColumns, {
        property: selectedColumn,
        sort: sortingOrder.FIRST
      }];
    };

    // 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 && value.toLowerCase) {
            return value.toLowerCase();
          }

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

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

    export default {
      byColumn,
      byColumns,
      sorter
    };