All files / lib/list matrix.js

24.7% Statements 21/85
10.25% Branches 4/39
20% Functions 2/10
24.69% Lines 20/81

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142  1x 1x 1x 1x 1x 1x 1x   10x 10x 10x 20x 20x 12x     8x         3x 3x 1x   2x 2x 2x                                                                                                                                                                                                                                  
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMatrixCombinations = getMatrixCombinations;
exports.applyCombinationToValue = applyCombinationToValue;
exports.applyCombinationToFeatureAssertion = applyCombinationToFeatureAssertion;
exports.getFeatureAssertionsFromMatrix = getFeatureAssertionsFromMatrix;
exports.applyCombinationToSegmentAssertion = applyCombinationToSegmentAssertion;
exports.getSegmentAssertionsFromMatrix = getSegmentAssertionsFromMatrix;
function generateCombinations(keys, matrix, idx, prev, combinations) {
    const key = keys[idx];
    const values = matrix[key];
    for (let i = 0; i < values.length; i++) {
        const combination = { ...prev, [key]: values[i] };
        if (idx === keys.length - 1) {
            combinations.push(combination);
        }
        else {
            generateCombinations(keys, matrix, idx + 1, combination, combinations);
        }
    }
}
function getMatrixCombinations(matrix) {
    const keys = Object.keys(matrix);
    if (!keys.length) {
        return [];
    }
    const combinations = [];
    generateCombinations(keys, matrix, 0, {}, combinations);
    return combinations;
}
function applyCombinationToValue(value, combination) {
    if (typeof value === "string") {
        const variableKeysInValue = value.match(/\${{(.+?)}}/g);
        // no variables found
        if (!variableKeysInValue) {
            return value;
        }
        // only 1 variable found, so we can insert the value directly
        if (variableKeysInValue.length === 1 && value.startsWith("${{") && value.endsWith("}}")) {
            const key = value.replace("${{", "").replace("}}", "").trim();
            return combination[key];
        }
        // multiple variables found, so we can replace each as a whole string
        return value.replace(/\${{(.+?)}}/g, (_, key) => combination[key.trim()]);
    }
    return value;
}
/**
 * Features
 */
function applyCombinationToFeatureAssertion(combination, assertion) {
    const flattenedAssertion = { ...assertion };
    // environment
    flattenedAssertion.environment = applyCombinationToValue(flattenedAssertion.environment, combination);
    // context
    flattenedAssertion.context = Object.keys(flattenedAssertion.context || {}).reduce((acc, key) => {
        acc[key] = applyCombinationToValue(flattenedAssertion.context?.[key], combination);
        return acc;
    }, {});
    // at
    flattenedAssertion.at = applyCombinationToValue(flattenedAssertion.at, combination);
    if (typeof flattenedAssertion.at === "string") {
        flattenedAssertion.at =
            flattenedAssertion.at.indexOf(".") > -1
                ? parseFloat(flattenedAssertion.at)
                : parseInt(flattenedAssertion.at, 10);
    }
    // description
    if (flattenedAssertion.description) {
        flattenedAssertion.description = applyCombinationToValue(flattenedAssertion.description, combination);
    }
    return flattenedAssertion;
}
function getFeatureAssertionsFromMatrix(aIndex, assertionWithMatrix) {
    if (!assertionWithMatrix.matrix) {
        const assertion = { ...assertionWithMatrix };
        let suffix;
        if (assertion.environment) {
            suffix = ` (${assertion.environment})`;
        }
        if (assertion.description) {
            suffix = `: ${assertion.description}`;
        }
        else {
            suffix = `: at ${assertion.at}%`;
        }
        assertion.description = `Assertion #${aIndex + 1}${suffix}`;
        return [assertion];
    }
    const assertions = [];
    const combinations = getMatrixCombinations(assertionWithMatrix.matrix);
    for (let cIndex = 0; cIndex < combinations.length; cIndex++) {
        const combination = combinations[cIndex];
        const assertion = applyCombinationToFeatureAssertion(combination, assertionWithMatrix);
        let suffix;
        if (assertion.environment) {
            suffix = ` (${assertion.environment})`;
        }
        if (assertion.description) {
            suffix = `: ${assertion.description}`;
        }
        else {
            suffix = `: at ${assertion.at}%`;
        }
        assertion.description = `Assertion #${aIndex + 1}${suffix}`;
        assertions.push(assertion);
    }
    return assertions;
}
/**
 * Segments
 */
function applyCombinationToSegmentAssertion(combination, assertion) {
    const flattenedAssertion = { ...assertion };
    // context
    flattenedAssertion.context = Object.keys(flattenedAssertion.context).reduce((acc, key) => {
        acc[key] = applyCombinationToValue(flattenedAssertion.context[key], combination);
        return acc;
    }, {});
    // description
    if (flattenedAssertion.description) {
        flattenedAssertion.description = applyCombinationToValue(flattenedAssertion.description, combination);
    }
    return flattenedAssertion;
}
function getSegmentAssertionsFromMatrix(aIndex, assertionWithMatrix) {
    if (!assertionWithMatrix.matrix) {
        const assertion = { ...assertionWithMatrix };
        assertion.description = `Assertion #${aIndex + 1}${assertion.description ? `: ${assertion.description}` : ""}`;
        return [assertion];
    }
    const assertions = [];
    const combinations = getMatrixCombinations(assertionWithMatrix.matrix);
    for (let cIndex = 0; cIndex < combinations.length; cIndex++) {
        const combination = combinations[cIndex];
        const assertion = applyCombinationToSegmentAssertion(combination, assertionWithMatrix);
        assertion.description = `Assertion #${aIndex + 1}: ${assertion.description || `#${aIndex + 1}`}`;
        assertions.push(assertion);
    }
    return assertions;
}
//# sourceMappingURL=matrix.js.map