Source

math/calculateCenteredRange.js

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * Calculate a range of number to be displayed,
 * with the active number to be centered in the range if possible.
 *
 * @author Sandy Lau https://github.com/sandylau333
 *
 * @param {number} active The active number to be centered
 * @param {number} minPossibleIndex Minimum possible number
 * @param {number} maxPossibleIndex Maximum possible number
 * @param {number} maxRange Maximum range of number to be displayed each time
 *
 * @returns {Array<number>} Return a tuple
 * including minimum and maximum number of the range [Min, Max]
 * @category math
 * @module calculateCenteredRange
 */
var calculateCenteredRange = function (active, minPossibleIndex, maxPossibleIndex, maxRange) {
    if (maxRange === void 0) { maxRange = 10; }
    var numBeforeMiddle = Math.floor((maxRange - 1) / 2);
    var start = Math.max(minPossibleIndex, (active - numBeforeMiddle));
    var end = Math.min(maxPossibleIndex, (start + maxRange - 1));
    start = (end - start + 1) < maxRange
        ? Math.max(minPossibleIndex, (end - maxRange + 1))
        : start;
    return [start, end];
};
exports.default = calculateCenteredRange;