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 | 3x 3x 414x 414x 359x 55x 2x 53x 8x 8x 8x 8x 45x 8x 8x 4x 4x 4x 37x 25x 25x 3x 22x 3x 19x 3x 16x 3x 13x 2x 11x 2x 9x 2x 7x 3x 4x 2x 2x 2x 12x 12x 12x 2x 10x 4x 6x 2x 4x 4x 3x 842x 414x 414x 428x 37x 405x 38x 383x 6x 9x 377x 380x | import { compareVersions } from "compare-versions";
import { Context, Condition, PlainCondition } from "@featurevisor/types";
import { Logger } from "./logger";
export function conditionIsMatched(condition: PlainCondition, context: Context): boolean {
const { attribute, operator, value } = condition;
if (operator === "equals") {
return context[attribute] === value;
} else if (operator === "notEquals") {
return context[attribute] !== value;
} else if (operator === "before" || operator === "after") {
// date comparisons
const valueInContext = context[attribute] as string | Date;
const dateInContext =
valueInContext instanceof Date ? valueInContext : new Date(valueInContext);
const dateInCondition = value instanceof Date ? value : new Date(value as string);
return operator === "before"
? dateInContext < dateInCondition
: dateInContext > dateInCondition;
} else if (
Array.isArray(value) &&
(["string", "number"].indexOf(typeof context[attribute]) !== -1 || context[attribute] === null)
) {
// array
const valueInContext = context[attribute] as string;
if (operator === "in") {
return value.indexOf(valueInContext) !== -1;
} else if (operator === "notIn") {
return value.indexOf(valueInContext) === -1;
}
} else if (typeof context[attribute] === "string" && typeof value === "string") {
// string
const valueInContext = context[attribute] as string;
if (operator === "contains") {
return valueInContext.indexOf(value) !== -1;
} else if (operator === "notContains") {
return valueInContext.indexOf(value) === -1;
} else if (operator === "startsWith") {
return valueInContext.startsWith(value);
} else if (operator === "endsWith") {
return valueInContext.endsWith(value);
} else if (operator === "semverEquals") {
return compareVersions(valueInContext, value) === 0;
} else if (operator === "semverNotEquals") {
return compareVersions(valueInContext, value) !== 0;
} else if (operator === "semverGreaterThan") {
return compareVersions(valueInContext, value) === 1;
} else if (operator === "semverGreaterThanOrEquals") {
return compareVersions(valueInContext, value) >= 0;
} else if (operator === "semverLessThan") {
return compareVersions(valueInContext, value) === -1;
} else if (operator === "semverLessThanOrEquals") {
return compareVersions(valueInContext, value) <= 0;
}
} else if (typeof context[attribute] === "number" && typeof value === "number") {
// numeric
const valueInContext = context[attribute] as number;
if (operator === "greaterThan") {
return valueInContext > value;
} else if (operator === "greaterThanOrEquals") {
return valueInContext >= value;
} else if (operator === "lessThan") {
return valueInContext < value;
} else if (operator === "lessThanOrEquals") {
return valueInContext <= value;
}
}
return false;
}
export function allConditionsAreMatched(
conditions: Condition[] | Condition,
context: Context,
logger: Logger,
): boolean {
if ("attribute" in conditions) {
try {
return conditionIsMatched(conditions, context);
} catch (e) {
logger.warn(e.message, {
error: e,
details: {
condition: conditions,
context,
},
});
return false;
}
}
if ("and" in conditions && Array.isArray(conditions.and)) {
return conditions.and.every((c) => allConditionsAreMatched(c, context, logger));
}
if ("or" in conditions && Array.isArray(conditions.or)) {
return conditions.or.some((c) => allConditionsAreMatched(c, context, logger));
}
if ("not" in conditions && Array.isArray(conditions.not)) {
return conditions.not.every(
() =>
allConditionsAreMatched(
{
and: conditions.not,
},
context,
logger,
) === false,
);
}
if (Array.isArray(conditions)) {
return conditions.every((c) => allConditionsAreMatched(c, context, logger));
}
return false;
}
|