Source

algo/getTableRowsSortStateReducer.js

"use strict";
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * Get next "TableCellSortState" to achieve the following behaviors:
 *  1. When the cell was not sorted
 *      --> sort it according to default ordering (ascending/descending)
 *  2. When the cell was sorted as default ordering
 *      --> set as the opposite ordering (ascending/descending)
 *  3. When the cell was sorted but NOT as default ordering
 *      --> clear sort state
 *
 * @returns a reducer function that accept a previous/current state and return next state.
 * @category algo
 * @module getTableRowsSortStateReducer
 */
var getTableRowsSortStateReducer = function (cellIndex, isDefaultToDescending) {
    if (isDefaultToDescending === void 0) { isDefaultToDescending = false; }
    return function (prev) {
        var prevCellState = prev.find(function (s) { return s.index === cellIndex; });
        // Case: previously not sorted --> append this cell
        if (!prevCellState) {
            return __spreadArray(__spreadArray([], prev, true), [
                { index: cellIndex, isDescending: isDefaultToDescending },
            ], false);
        }
        // Case: previously was not default ordering --> remove this cell
        if (prevCellState.isDescending !== isDefaultToDescending)
            return prev.filter(function (s) { return s.index !== cellIndex; });
        // Default case: previously was sorted --> change sort ordering
        return prev.map(function (s) {
            if (s.index === cellIndex) {
                return {
                    index: cellIndex,
                    isDescending: !s.isDescending,
                };
            }
            return s;
        });
    };
};
exports.default = getTableRowsSortStateReducer;