All files evaluate.ts

91.13% Statements 216/237
88.23% Branches 135/153
100% Functions 10/10
90.98% Lines 212/233

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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871                                        2x   2x   2x 2x 2x 2x     2x 2x     2x 2x 2x 2x     2x 2x 2x 2x 2x   2x                                                                                                             2x 108x 108x 108x     108x 108x 12x 1x         108x     108x                 108x                 108x 12x 1x       108x                                   2x   199x   199x     199x         199x   85x         85x 4x           4x     4x 1x           1x   1x                     1x                               4x                   4x   4x             195x   7x 4x               4x   4x       3x 2x   2x 2x             2x   2x         1x 1x   1x 1x   1x 1x               1x   1x                   188x     188x 7x           7x   7x       181x 1x           181x 113x 111x       113x 2x             2x   2x     111x                 179x 1x           1x   1x           178x   178x   14x 9x                 9x   9x       5x 1x   1x 1x                 1x   1x         4x 1x                     1x   1x             167x 4x       4x 2x   2x 2x     4x         4x   4x 1x     3x 2x               2x   2x 2x     2x     1x     4x 2x               2x   2x               165x             165x 16x 10x                   165x   165x 16x 2x                       165x 72x   72x 72x     93x     165x   161x 1x                     1x   1x       160x   88x 2x 2x       2x 1x                       1x   1x       1x                 1x   1x       86x 1x                     1x   1x       85x 85x                     85x   85x         72x   18x 3x   3x 3x                     3x   3x         15x 22x   15x 15x                     15x   15x             58x   54x         5x                         5x   5x           49x 3x 46x 21x 25x 24x     49x 75x   48x 9x   9x 16x 7x               9x 9x                 9x 4x                         4x   4x       44x         5x                         5x   5x               44x                           44x 40x 40x                     40x   40x                                 4x                 4x   4x                              
import type {
  FeatureKey,
  Context,
  RuleKey,
  Traffic,
  Force,
  Required,
  Variation,
  VariationValue,
  VariableKey,
  VariableValue,
  VariableSchema,
  EvaluatedFeature,
  StickyFeatures,
  Allocation,
} from "@featurevisor/types";
 
import { Logger } from "./logger";
import { HooksManager } from "./hooks";
import { DatafileReader } from "./datafileReader";
import { BucketKey, BucketValue, getBucketKey, getBucketedNumber } from "./bucketer";
 
export enum EvaluationReason {
  // feature specific
  FEATURE_NOT_FOUND = "feature_not_found", // feature is not found in datafile
  DISABLED = "disabled", // feature is disabled
  REQUIRED = "required", // required features are not enabled
  OUT_OF_RANGE = "out_of_range", // out of range when mutually exclusive experiments are involved via Groups
 
  // variations specific
  NO_VARIATIONS = "no_variations", // feature has no variations
  VARIATION_DISABLED = "variation_disabled", // feature is disabled, and variation's disabledVariationValue is used
 
  // variable specific
  VARIABLE_NOT_FOUND = "variable_not_found", // variable's schema is not defined in the feature
  VARIABLE_DEFAULT = "variable_default", // default variable value used
  VARIABLE_DISABLED = "variable_disabled", // feature is disabled, and variable's disabledValue is used
  VARIABLE_OVERRIDE = "variable_override", // variable overridden from inside a variation
 
  // common
  NO_MATCH = "no_match", // no rules matched
  FORCED = "forced", // against a forced rule
  STICKY = "sticky", // against a sticky feature
  RULE = "rule", // against a regular rule
  ALLOCATED = "allocated", // regular allocation based on bucketing
 
  ERROR = "error", // error
}
 
type EvaluationType = "flag" | "variation" | "variable";
 
export interface Evaluation {
  // required
  type: EvaluationType;
  featureKey: FeatureKey;
  reason: EvaluationReason;
 
  // common
  bucketKey?: BucketKey;
  bucketValue?: BucketValue;
  ruleKey?: RuleKey;
  error?: Error;
  enabled?: boolean;
  traffic?: Traffic;
  forceIndex?: number;
  force?: Force;
  required?: Required[];
  sticky?: EvaluatedFeature;
 
  // variation
  variation?: Variation;
  variationValue?: VariationValue;
 
  // variable
  variableKey?: VariableKey;
  variableValue?: VariableValue;
  variableSchema?: VariableSchema;
}
 
export interface EvaluateDependencies {
  context: Context;
 
  logger: Logger;
  hooksManager: HooksManager;
  datafileReader: DatafileReader;
 
  // OverrideOptions
  sticky?: StickyFeatures;
 
  defaultVariationValue?: VariationValue;
  defaultVariableValue?: VariableValue;
}
 
export interface EvaluateParams {
  type: EvaluationType;
  featureKey: FeatureKey;
  variableKey?: VariableKey;
}
 
export type EvaluateOptions = EvaluateParams & EvaluateDependencies;
 
export function evaluateWithHooks(opts: EvaluateOptions): Evaluation {
  try {
    const { hooksManager } = opts;
    const hooks = hooksManager.getAll();
 
    // run before hooks
    let options = opts;
    for (const hook of hooksManager.getAll()) {
      if (hook.before) {
        options = hook.before(options);
      }
    }
 
    // evaluate
    let evaluation = evaluate(options);
 
    // default: variation
    Iif (
      typeof options.defaultVariationValue !== "undefined" &&
      evaluation.type === "variation" &&
      typeof evaluation.variationValue === "undefined"
    ) {
      evaluation.variationValue = options.defaultVariationValue;
    }
 
    // default: variable
    Iif (
      typeof options.defaultVariableValue !== "undefined" &&
      evaluation.type === "variable" &&
      typeof evaluation.variableValue === "undefined"
    ) {
      evaluation.variableValue = options.defaultVariableValue;
    }
 
    // run after hooks
    for (const hook of hooks) {
      if (hook.after) {
        evaluation = hook.after(evaluation, options);
      }
    }
 
    return evaluation;
  } catch (e) {
    const { type, featureKey, variableKey, logger } = opts;
 
    const evaluation: Evaluation = {
      type,
      featureKey,
      variableKey,
      reason: EvaluationReason.ERROR,
      error: e,
    };
 
    logger.error("error during evaluation", evaluation);
 
    return evaluation;
  }
}
 
export function evaluate(options: EvaluateOptions): Evaluation {
  const { type, featureKey, variableKey, context, logger, datafileReader, sticky, hooksManager } =
    options;
 
  const hooks = hooksManager.getAll();
  let evaluation: Evaluation;
 
  try {
    /**
     * Root flag evaluation
     */
    let flag: Evaluation;
    if (type !== "flag") {
      // needed by variation and variable evaluations
      flag = evaluate({
        ...options,
        type: "flag",
      });
 
      if (flag.enabled === false) {
        evaluation = {
          type,
          featureKey,
          reason: EvaluationReason.DISABLED,
        };
 
        const feature = datafileReader.getFeature(featureKey);
 
        // serve variable default value if feature is disabled (if explicitly specified)
        if (type === "variable") {
          if (
            feature &&
            variableKey &&
            feature.variablesSchema &&
            feature.variablesSchema[variableKey]
          ) {
            const variableSchema = feature.variablesSchema[variableKey];
 
            Iif (typeof variableSchema.disabledValue !== "undefined") {
              // disabledValue: <value>
              evaluation = {
                type,
                featureKey,
                reason: EvaluationReason.VARIABLE_DISABLED,
                variableKey,
                variableValue: variableSchema.disabledValue,
                variableSchema,
                enabled: false,
              };
            } else Iif (variableSchema.useDefaultWhenDisabled) {
              // useDefaultWhenDisabled: true
              evaluation = {
                type,
                featureKey,
                reason: EvaluationReason.VARIABLE_DEFAULT,
                variableKey,
                variableValue: variableSchema.defaultValue,
                variableSchema,
                enabled: false,
              };
            }
          }
        }
 
        // serve disabled variation value if feature is disabled (if explicitly specified)
        Iif (type === "variation" && feature && feature.disabledVariationValue) {
          evaluation = {
            type,
            featureKey,
            reason: EvaluationReason.VARIATION_DISABLED,
            variationValue: feature.disabledVariationValue,
            enabled: false,
          };
        }
 
        logger.debug("feature is disabled", evaluation);
 
        return evaluation;
      }
    }
 
    /**
     * Sticky
     */
    if (sticky && sticky[featureKey]) {
      // flag
      if (type === "flag" && typeof sticky[featureKey].enabled !== "undefined") {
        evaluation = {
          type,
          featureKey,
          reason: EvaluationReason.STICKY,
          sticky: sticky[featureKey],
          enabled: sticky[featureKey].enabled,
        };
 
        logger.debug("using sticky enabled", evaluation);
 
        return evaluation;
      }
 
      // variation
      if (type === "variation") {
        const variationValue = sticky[featureKey].variation;
 
        if (typeof variationValue !== "undefined") {
          evaluation = {
            type,
            featureKey,
            reason: EvaluationReason.STICKY,
            variationValue,
          };
 
          logger.debug("using sticky variation", evaluation);
 
          return evaluation;
        }
      }
 
      // variable
      if (variableKey) {
        const variables = sticky[featureKey].variables;
 
        if (variables) {
          const result = variables[variableKey];
 
          if (typeof result !== "undefined") {
            evaluation = {
              type,
              featureKey,
              reason: EvaluationReason.STICKY,
              variableKey,
              variableValue: result,
            };
 
            logger.debug("using sticky variable", evaluation);
 
            return evaluation;
          }
        }
      }
    }
 
    /**
     * Feature
     */
    const feature =
      typeof featureKey === "string" ? datafileReader.getFeature(featureKey) : featureKey;
 
    // feature: not found
    if (!feature) {
      evaluation = {
        type,
        featureKey,
        reason: EvaluationReason.FEATURE_NOT_FOUND,
      };
 
      logger.warn("feature not found", evaluation);
 
      return evaluation;
    }
 
    // feature: deprecated
    if (type === "flag" && feature.deprecated) {
      logger.warn("feature is deprecated", { featureKey });
    }
 
    // variableSchema
    let variableSchema: VariableSchema | undefined;
 
    if (variableKey) {
      if (feature.variablesSchema && feature.variablesSchema[variableKey]) {
        variableSchema = feature.variablesSchema[variableKey];
      }
 
      // variable schema not found
      if (!variableSchema) {
        evaluation = {
          type,
          featureKey,
          reason: EvaluationReason.VARIABLE_NOT_FOUND,
          variableKey,
        };
 
        logger.warn("variable schema not found", evaluation);
 
        return evaluation;
      }
 
      Iif (variableSchema.deprecated) {
        logger.warn("variable is deprecated", {
          featureKey,
          variableKey,
        });
      }
    }
 
    // variation: no variations
    if (type === "variation" && (!feature.variations || feature.variations.length === 0)) {
      evaluation = {
        type,
        featureKey,
        reason: EvaluationReason.NO_VARIATIONS,
      };
 
      logger.warn("no variations", evaluation);
 
      return evaluation;
    }
 
    /**
     * Forced
     */
    const { force, forceIndex } = datafileReader.getMatchedForce(feature, context);
 
    if (force) {
      // flag
      if (type === "flag" && typeof force.enabled !== "undefined") {
        evaluation = {
          type,
          featureKey,
          reason: EvaluationReason.FORCED,
          forceIndex,
          force,
          enabled: force.enabled,
        };
 
        logger.debug("forced enabled found", evaluation);
 
        return evaluation;
      }
 
      // variation
      if (type === "variation" && force.variation && feature.variations) {
        const variation = feature.variations.find((v) => v.value === force.variation);
 
        if (variation) {
          evaluation = {
            type,
            featureKey,
            reason: EvaluationReason.FORCED,
            forceIndex,
            force,
            variation,
          };
 
          logger.debug("forced variation found", evaluation);
 
          return evaluation;
        }
      }
 
      // variable
      if (variableKey && force.variables && typeof force.variables[variableKey] !== "undefined") {
        evaluation = {
          type,
          featureKey,
          reason: EvaluationReason.FORCED,
          forceIndex,
          force,
          variableKey,
          variableSchema,
          variableValue: force.variables[variableKey],
        };
 
        logger.debug("forced variable", evaluation);
 
        return evaluation;
      }
    }
 
    /**
     * Required
     */
    if (type === "flag" && feature.required && feature.required.length > 0) {
      const requiredFeaturesAreEnabled = feature.required.every((required) => {
        let requiredKey;
        let requiredVariation;
 
        if (typeof required === "string") {
          requiredKey = required;
        } else {
          requiredKey = required.key;
          requiredVariation = required.variation;
        }
 
        const requiredEvaluation = evaluate({
          ...options,
          type: "flag",
          featureKey: requiredKey,
        });
        const requiredIsEnabled = requiredEvaluation.enabled;
 
        if (!requiredIsEnabled) {
          return false;
        }
 
        if (typeof requiredVariation !== "undefined") {
          const requiredVariationEvaluation = evaluate({
            ...options,
            type: "variation",
            featureKey: requiredKey,
          });
 
          let requiredVariationValue;
 
          Iif (requiredVariationEvaluation.variationValue) {
            requiredVariationValue = requiredVariationEvaluation.variationValue;
          } else if (requiredVariationEvaluation.variation) {
            requiredVariationValue = requiredVariationEvaluation.variation.value;
          }
 
          return requiredVariationValue === requiredVariation;
        }
 
        return true;
      });
 
      if (!requiredFeaturesAreEnabled) {
        evaluation = {
          type,
          featureKey,
          reason: EvaluationReason.REQUIRED,
          required: feature.required,
          enabled: requiredFeaturesAreEnabled,
        };
 
        logger.debug("required features not enabled", evaluation);
 
        return evaluation;
      }
    }
 
    /**
     * Bucketing
     */
    // bucketKey
    let bucketKey = getBucketKey({
      featureKey,
      bucketBy: feature.bucketBy,
      context,
 
      logger,
    });
    for (const hook of hooks) {
      if (hook.bucketKey) {
        bucketKey = hook.bucketKey({
          featureKey,
          context,
          bucketBy: feature.bucketBy,
          bucketKey,
        });
      }
    }
 
    // bucketValue
    let bucketValue = getBucketedNumber(bucketKey);
 
    for (const hook of hooks) {
      if (hook.bucketValue) {
        bucketValue = hook.bucketValue({
          featureKey,
          bucketKey,
          context,
          bucketValue,
        });
      }
    }
 
    let matchedTraffic: Traffic | undefined;
    let matchedAllocation: Allocation | undefined;
 
    if (type !== "flag") {
      matchedTraffic = datafileReader.getMatchedTraffic(feature.traffic, context);
 
      if (matchedTraffic) {
        matchedAllocation = datafileReader.getMatchedAllocation(matchedTraffic, bucketValue);
      }
    } else {
      matchedTraffic = datafileReader.getMatchedTraffic(feature.traffic, context);
    }
 
    if (matchedTraffic) {
      // percentage: 0
      if (matchedTraffic.percentage === 0) {
        evaluation = {
          type,
          featureKey,
          reason: EvaluationReason.RULE,
          bucketKey,
          bucketValue,
          ruleKey: matchedTraffic.key,
          traffic: matchedTraffic,
          enabled: false,
        };
 
        logger.debug("matched rule with 0 percentage", evaluation);
 
        return evaluation;
      }
 
      // flag
      if (type === "flag") {
        // flag: check if mutually exclusive
        if (feature.ranges && feature.ranges.length > 0) {
          const matchedRange = feature.ranges.find((range) => {
            return bucketValue >= range[0] && bucketValue < range[1];
          });
 
          // matched
          if (matchedRange) {
            evaluation = {
              type,
              featureKey,
              reason: EvaluationReason.ALLOCATED,
              bucketKey,
              bucketValue,
              ruleKey: matchedTraffic.key,
              traffic: matchedTraffic,
              enabled:
                typeof matchedTraffic.enabled === "undefined" ? true : matchedTraffic.enabled,
            };
 
            logger.debug("matched", evaluation);
 
            return evaluation;
          }
 
          // no match
          evaluation = {
            type,
            featureKey,
            reason: EvaluationReason.OUT_OF_RANGE,
            bucketKey,
            bucketValue,
            enabled: false,
          };
 
          logger.debug("not matched", evaluation);
 
          return evaluation;
        }
 
        // flag: override from rule
        if (typeof matchedTraffic.enabled !== "undefined") {
          evaluation = {
            type,
            featureKey,
            reason: EvaluationReason.RULE,
            bucketKey,
            bucketValue,
            ruleKey: matchedTraffic.key,
            traffic: matchedTraffic,
            enabled: matchedTraffic.enabled,
          };
 
          logger.debug("override from rule", evaluation);
 
          return evaluation;
        }
 
        // treated as enabled because of matched traffic
        if (bucketValue <= matchedTraffic.percentage) {
          evaluation = {
            type,
            featureKey,
            reason: EvaluationReason.RULE,
            bucketKey,
            bucketValue,
            ruleKey: matchedTraffic.key,
            traffic: matchedTraffic,
            enabled: true,
          };
 
          logger.debug("matched traffic", evaluation);
 
          return evaluation;
        }
      }
 
      // variation
      if (type === "variation" && feature.variations) {
        // override from rule
        if (matchedTraffic.variation) {
          const variation = feature.variations.find((v) => v.value === matchedTraffic.variation);
 
          if (variation) {
            evaluation = {
              type,
              featureKey,
              reason: EvaluationReason.RULE,
              bucketKey,
              bucketValue,
              ruleKey: matchedTraffic.key,
              traffic: matchedTraffic,
              variation,
            };
 
            logger.debug("override from rule", evaluation);
 
            return evaluation;
          }
        }
 
        // regular allocation
        if (matchedAllocation && matchedAllocation.variation) {
          const variation = feature.variations.find((v) => v.value === matchedAllocation.variation);
 
          if (variation) {
            evaluation = {
              type,
              featureKey,
              reason: EvaluationReason.ALLOCATED,
              bucketKey,
              bucketValue,
              ruleKey: matchedTraffic.key,
              traffic: matchedTraffic,
              variation,
            };
 
            logger.debug("allocated variation", evaluation);
 
            return evaluation;
          }
        }
      }
    }
 
    // variable
    if (type === "variable" && variableKey) {
      // override from rule
      if (
        matchedTraffic &&
        matchedTraffic.variables &&
        typeof matchedTraffic.variables[variableKey] !== "undefined"
      ) {
        evaluation = {
          type,
          featureKey,
          reason: EvaluationReason.RULE,
          bucketKey,
          bucketValue,
          ruleKey: matchedTraffic.key,
          traffic: matchedTraffic,
          variableKey,
          variableSchema,
          variableValue: matchedTraffic.variables[variableKey],
        };
 
        logger.debug("override from rule", evaluation);
 
        return evaluation;
      }
 
      // check variations
      let variationValue;
 
      if (force && force.variation) {
        variationValue = force.variation;
      } else if (matchedTraffic && matchedTraffic.variation) {
        variationValue = matchedTraffic.variation;
      } else if (matchedAllocation && matchedAllocation.variation) {
        variationValue = matchedAllocation.variation;
      }
 
      if (variationValue && Array.isArray(feature.variations)) {
        const variation = feature.variations.find((v) => v.value === variationValue);
 
        if (variation && variation.variableOverrides && variation.variableOverrides[variableKey]) {
          const overrides = variation.variableOverrides[variableKey];
 
          const override = overrides.find((o) => {
            if (o.conditions) {
              return datafileReader.allConditionsAreMatched(
                typeof o.conditions === "string" && o.conditions !== "*"
                  ? JSON.parse(o.conditions)
                  : o.conditions,
                context,
              );
            }
 
            if (o.segments) {
              return datafileReader.allSegmentsAreMatched(
                datafileReader.parseSegmentsIfStringified(o.segments),
                context,
              );
            }
 
            return false;
          });
 
          if (override) {
            evaluation = {
              type,
              featureKey,
              reason: EvaluationReason.VARIABLE_OVERRIDE,
              bucketKey,
              bucketValue,
              ruleKey: matchedTraffic?.key,
              traffic: matchedTraffic,
              variableKey,
              variableSchema,
              variableValue: override.value,
            };
 
            logger.debug("variable override", evaluation);
 
            return evaluation;
          }
        }
 
        if (
          variation &&
          variation.variables &&
          typeof variation.variables[variableKey] !== "undefined"
        ) {
          evaluation = {
            type,
            featureKey,
            reason: EvaluationReason.ALLOCATED,
            bucketKey,
            bucketValue,
            ruleKey: matchedTraffic?.key,
            traffic: matchedTraffic,
            variableKey,
            variableSchema,
            variableValue: variation.variables[variableKey],
          };
 
          logger.debug("allocated variable", evaluation);
 
          return evaluation;
        }
      }
    }
 
    /**
     * Nothing matched
     */
    Iif (type === "variation") {
      evaluation = {
        type,
        featureKey,
        reason: EvaluationReason.NO_MATCH,
        bucketKey,
        bucketValue,
      };
 
      logger.debug("no matched variation", evaluation);
 
      return evaluation;
    }
 
    if (type === "variable") {
      if (variableSchema) {
        evaluation = {
          type,
          featureKey,
          reason: EvaluationReason.VARIABLE_DEFAULT,
          bucketKey,
          bucketValue,
          variableKey,
          variableSchema,
          variableValue: variableSchema.defaultValue,
        };
 
        logger.debug("using default value", evaluation);
 
        return evaluation;
      }
 
      evaluation = {
        type,
        featureKey,
        reason: EvaluationReason.VARIABLE_NOT_FOUND,
        variableKey,
        bucketKey,
        bucketValue,
      };
 
      logger.debug("variable not found", evaluation);
 
      return evaluation;
    }
 
    evaluation = {
      type,
      featureKey,
      reason: EvaluationReason.NO_MATCH,
      bucketKey,
      bucketValue,
      enabled: false,
    };
 
    logger.debug("nothing matched", evaluation);
 
    return evaluation;
  } catch (e) {
    evaluation = {
      type,
      featureKey,
      variableKey,
      reason: EvaluationReason.ERROR,
      error: e,
    };
 
    logger.error("error", evaluation);
 
    return evaluation;
  }
}