Source

algo/getTableRowsSortStateReducer.js

  1. "use strict";
  2. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  3. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  4. if (ar || !(i in from)) {
  5. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  6. ar[i] = from[i];
  7. }
  8. }
  9. return to.concat(ar || Array.prototype.slice.call(from));
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. /**
  13. * Get next "TableCellSortState" to achieve the following behaviors:
  14. * 1. When the cell was not sorted
  15. * --> sort it according to default ordering (ascending/descending)
  16. * 2. When the cell was sorted as default ordering
  17. * --> set as the opposite ordering (ascending/descending)
  18. * 3. When the cell was sorted but NOT as default ordering
  19. * --> clear sort state
  20. *
  21. * @returns a reducer function that accept a previous/current state and return next state.
  22. * @category algo
  23. * @module getTableRowsSortStateReducer
  24. */
  25. var getTableRowsSortStateReducer = function (cellIndex, isDefaultToDescending) {
  26. if (isDefaultToDescending === void 0) { isDefaultToDescending = false; }
  27. return function (prev) {
  28. var prevCellState = prev.find(function (s) { return s.index === cellIndex; });
  29. // Case: previously not sorted --> append this cell
  30. if (!prevCellState) {
  31. return __spreadArray(__spreadArray([], prev, true), [
  32. { index: cellIndex, isDescending: isDefaultToDescending },
  33. ], false);
  34. }
  35. // Case: previously was not default ordering --> remove this cell
  36. if (prevCellState.isDescending !== isDefaultToDescending)
  37. return prev.filter(function (s) { return s.index !== cellIndex; });
  38. // Default case: previously was sorted --> change sort ordering
  39. return prev.map(function (s) {
  40. if (s.index === cellIndex) {
  41. return {
  42. index: cellIndex,
  43. isDescending: !s.isDescending,
  44. };
  45. }
  46. return s;
  47. });
  48. };
  49. };
  50. exports.default = getTableRowsSortStateReducer;