Source

math/getMaxLengthCombos.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 });
/**
 * Generate array combinations,
 * each of which is of the length of the number of arrays passed (groups)
 *
 * @param {Array<Array<T>>}
 * arrays An array of arrays containing each options (e.g. option groups)
 * e.g. [['a','b'], ['1', '2'], ['@', '#']]
 * @returns {Array<Array<T>>}
 * An array of arrays containing each combinations generated by the passed arrays.
 * e.g. [['a', '1', '@'], ['a', '1', '#'], ['a', '2', '@']...]
 * @category math
 * @module getMaxLengthCombos
 */
function getMaxLengthCombos(arrays) {
    if (arrays === void 0) { arrays = []; }
    if (arrays.length === 0)
        return [];
    var results = [];
    var arraysSorted = arrays.sort(function (a, b) {
        if (a.length < b.length)
            return 1;
        if (a.length > b.length)
            return -1;
        return 0;
    });
    var recur = function (bufferArr, currentArr) {
        if (bufferArr === void 0) { bufferArr = []; }
        if (currentArr === void 0) { currentArr = []; }
        var nextDepth = bufferArr.length + 1;
        currentArr.forEach(function (v) {
            if (nextDepth === arrays.length)
                results.push(__spreadArray(__spreadArray([], bufferArr, true), [v], false));
            else
                recur(__spreadArray(__spreadArray([], bufferArr, true), [v], false), arrays[nextDepth]);
        });
    };
    recur([], arraysSorted[0]);
    return results;
}
exports.default = getMaxLengthCombos;