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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | 10x 10x 10x 20x 20x 12x 8x 1x 3x 3x 1x 2x 2x 2x 1x 1x 1x 1x 1x | import type { AssertionMatrix, FeatureAssertion, SegmentAssertion } from "@featurevisor/types";
function generateCombinations(
keys: string[],
matrix: AssertionMatrix,
idx: number,
prev: any,
combinations: any[],
) {
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);
}
}
}
export function getMatrixCombinations(matrix: AssertionMatrix) {
const keys = Object.keys(matrix);
if (!keys.length) {
return [];
}
const combinations: any[] = [];
generateCombinations(keys, matrix, 0, {}, combinations);
return combinations;
}
export function applyCombinationToValue(value: any, combination: any) {
Iif (typeof value === "string") {
const variableKeysInValue = value.match(/\${{(.+?)}}/g);
// no variables found
Iif (!variableKeysInValue) {
return value;
}
// only 1 variable found, so we can insert the value directly
Iif (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
*/
export function applyCombinationToFeatureAssertion(
combination: any,
assertion: FeatureAssertion,
): FeatureAssertion {
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);
Iif (typeof flattenedAssertion.at === "string") {
flattenedAssertion.at =
(flattenedAssertion.at as string).indexOf(".") > -1
? parseFloat(flattenedAssertion.at)
: parseInt(flattenedAssertion.at, 10);
}
// description
Iif (flattenedAssertion.description) {
flattenedAssertion.description = applyCombinationToValue(
flattenedAssertion.description,
combination,
);
}
return flattenedAssertion;
}
export function getFeatureAssertionsFromMatrix(
aIndex,
assertionWithMatrix: FeatureAssertion,
): FeatureAssertion[] {
Iif (!assertionWithMatrix.matrix) {
const assertion = { ...assertionWithMatrix };
let suffix;
Iif (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: FeatureAssertion[] = [];
const combinations = getMatrixCombinations(assertionWithMatrix.matrix);
for (let cIndex = 0; cIndex < combinations.length; cIndex++) {
const combination = combinations[cIndex];
const assertion = applyCombinationToFeatureAssertion(combination, assertionWithMatrix);
let suffix;
Iif (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
*/
export function applyCombinationToSegmentAssertion(
combination: any,
assertion: SegmentAssertion,
): SegmentAssertion {
const flattenedAssertion = { ...assertion };
// context
flattenedAssertion.context = Object.keys(flattenedAssertion.context).reduce((acc, key) => {
acc[key] = applyCombinationToValue(flattenedAssertion.context[key], combination);
return acc;
}, {});
// description
Iif (flattenedAssertion.description) {
flattenedAssertion.description = applyCombinationToValue(
flattenedAssertion.description,
combination,
);
}
return flattenedAssertion;
}
export function getSegmentAssertionsFromMatrix(
aIndex,
assertionWithMatrix: SegmentAssertion,
): SegmentAssertion[] {
Iif (!assertionWithMatrix.matrix) {
const assertion = { ...assertionWithMatrix };
assertion.description = `Assertion #${aIndex + 1}${
assertion.description ? `: ${assertion.description}` : ""
}`;
return [assertion];
}
const assertions: SegmentAssertion[] = [];
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;
}
|