<commit_before>import TinyExperimentCookieInterface from './tinyExperimentCookieInterface';

export default class TinyExperiment {
  constructor(args = {}) {
    this._numVariantPossibilities = args.variantNames.length;

    this.experimentKey = args.experimentKey;
    this.experimentName = args.experimentName;
    this.variantNames = args.variantNames;
    this.variantWeights = undefined;

    this._cookies = new TinyExperimentCookieInterface();
    this._cached = args.cached || false;
    this._cachePeriod = args.cachePeriod || 7;
    this._variantHandlers = {};
    this._tracked = false;
    this._completionHandler = args.completionHandler;
    this._completion = new Promise((_resolve, _reject) => {
      this._executeExperiment = _resolve;
      this._cancelExperiment = _reject;
    });

    this.init();
  }

  getRandomVariant(possibilities) {
    return Math.floor(Math.random() * possibilities);
  }

  allocVariantId() {
    if (this.variantWeights) {
      let rand = Math.random();
      let i = 0;

      let cumulatedWeight = this.variantWeights.reduce(a, b => {
        if (a + b > rand) this.variantId = i;

        i++
        return a + b;
      }, 0);

      if (cumulatedWeight != 1.0) throw new Error('Variant weights must add up to 1.0');
    } else {
      this.variantId = Math.floor(Math.random() * this._numVariantPossibilities);
    }
  }

  allocVariantName() {
    if (!Array.isArray(this.variantNames) || this.variantNames.length == 0) {
      throw new TypeError("Variant names must be an array of strings");
    }
    
    this.variantNames[this.variantId];
  }

  init() {

    this.allocVariantId();
    this.allocVariantName();

    if (this._cached) {
      if (this._cookies.getVariant(this.experimentKey)) {
        this.variantName = this._cookies.getVariant(this.experimentKey);
        this.variantId = this.variantNames.indexOf(this.variantName);
      } else {
        this._cookies.setVariant(this.experimentKey, this.variantName, this._cachePeriod);
      }
    }

    this._completion.then(() => {
      if (typeof this._variantHandlers[this.variantName] == 'function') {
        this._variantHandlers[this.variantName]();<commit_msg>Fixes overwriting variantId in weighted calc<commit_after>import TinyExperimentCookieInterface from './tinyExperimentCookieInterface';

export default class TinyExperiment {
  constructor(args = {}) {
    this._numVariantPossibilities = args.variantNames.length;

    this.experimentKey = args.experimentKey;
    this.experimentName = args.experimentName;
    this.variantNames = args.variantNames;
    this.variantWeights = undefined;

    this._cookies = new TinyExperimentCookieInterface();
    this._cached = args.cached || false;
    this._cachePeriod = args.cachePeriod || 7;
    this._variantHandlers = {};
    this._tracked = false;
    this._completionHandler = args.completionHandler;
    this._completion = new Promise((_resolve, _reject) => {
      this._executeExperiment = _resolve;
      this._cancelExperiment = _reject;
    });

    this.init();
  }

  getRandomVariant(possibilities) {
    return Math.floor(Math.random() * possibilities);
  }

  allocVariantId() {
    if (this.variantWeights) {
      let rand = Math.random();
      let i = 0;

      let cumulatedWeight = this.variantWeights.reduce(a, b => {
        if (!this.variantId && a + b > rand) this.variantId = i;

        i++
        return a + b;
      }, 0);

      if (cumulatedWeight != 1.0) throw new Error('Variant weights must add up to 1.0');
    } else {
      this.variantId = Math.floor(Math.random() * this._numVariantPossibilities);
    }
  }

  allocVariantName() {
    if (!Array.isArray(this.variantNames) || this.variantNames.length == 0) {
      throw new TypeError("Variant names must be an array of strings");
    }

    this.variantNames[this.variantId];
  }

  init() {

    this.allocVariantId();
    this.allocVariantName();

    if (this._cached) {
      if (this._cookies.getVariant(this.experimentKey)) {
        this.variantName = this._cookies.getVariant(this.experimentKey);
        this.variantId = this.variantNames.indexOf(this.variantName);
      } else {
        this._cookies.setVariant(this.experimentKey, this.variantName, this._cachePeriod);
      }
    }

    this._completion.then(() => {
      if (typeof this._variantHandlers[this.variantName] == 'function') {
        this._variantHandlers[this.variantName]();
{'fraction_non_alphanumeric': 0.12291007681879801, 'fraction_numerical': 0.0036150022593764122, 'mean_word_length': 3.1412535079513564, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6056309', 'n_tokens_mistral': 1348, 'n_tokens_neox': 1291, 'n_words': 251}