<commit_before>import { default as $ } from './Tag';
import Any from './Any';
import Attribute from './Attribute';
import Map from './Map';
import Object from './Object';
import Scalar from './Scalar';
import Vector from './Vector';

function closestAncestorByTagName(elem, tagName) {
  let e = elem.parent;
  while (e) {
    if (e.tagName === tagName) {
      return e;
    }
    e = e.parent;
  }
  return null;
}

function closestAncestorByClassName(elem, className) {
  let e = elem.parent;
  while (e) {
    if (e.classList.contains(className)) {
      return e;
    }
    e = e.parent;
  }
  return null;
}

function closestDefinition(elem) {
  return closestAncestorByClassName(elem, 'endpoint-test-body').dataset.definition;
}

function onKeyPress(event) {
  if (event.which === 13) {
    event.preventDefault();
    closestDefinition(this).submit();
  }
}

function onSubmit() {
  closestDefinition(this).submit();
}

function onEdit() {
  closestDefinition(this).edit();
}

function onTabSwitch() {
  const index = $(this).parent().children().filter('li').index($(this));
  closestDefinition(this).switchTab(index);
}

function createType(def) {
  var func = require('squalus/' + def.class);
  var params = /\(([^\)]*)\)/.exec(func.prototype.constructor.toString());
  var args = [];
  if (params) {
    params = params[1].split(',').map(Function.prototype.call, String.prototype.trim);
    for (var i = 0; i < params.length; i++) {
      var value = def[params[i]];
      if (Array.isArray(value) && value[0].class) {
        value = value.map(createType);
      }
      else if (value && value.class) {
        value = createType(value);
      }
      args.push(value);
    }
  }
  var obj = func.construct(args);
  if (obj.replace) {
    obj = obj.replace();
  }
  return obj;
}

function convertValueToParam(val, key, query) {
  if (Array.isArray(val)) {
    val.map(function (item, i) {
      convertValueToParam(item, `${key}[${i}]`, query);
    });
  } else if (typeof val === 'object') {
    for (var name in val) {
      convertValueToParam(val[name], `${key}[${name}]`, query);
    }
  } else {
    if (typeof val === 'boolean') {
      val = (val ? 1 : 0);
    }
    query[key] = encodeURI(val);
  }
}

export default class Definition {

  constructor(guid, endpoint, type, params) {
    this._guid = guid;
    this._endpoint = endpoint;
    this._type = type ? createType(type) : null;
    this._params = params;
    this._test = document.getElementById(guid);
    this._body = this._test.querySelector('.endpoint-test-body');
    this._json = this._body.querySelector('.test-json');

    this._body.innerHTML = '';
    this._body.dataset.definition = this;
  }

  syncTabParams() {
    var container = this._body.querySelector('.tab-container');
    if (container) {
      var src = container.children().filter('.current').find('.test-param');
      var dst = container.children().not('.current').find('.test-param');
      src.each(function (i, node) {
        dst.eq(i).val($(node).val());
      });
    }
  }

  updateSingleParam(data) {
    if (this._endpoint.params) {
      if (data.Id) {
        this._body.querySelector('.test-param').dataset.type.populate(data.Id);<commit_msg>minor updates<commit_after>import { default as $ } from './Tag';
import Any from './Any';
import Attribute from './Attribute';
import Map from './Map';
import Object from './Object';
import Scalar from './Scalar';
import Vector from './Vector';

function closestAncestorByTagName(elem, tagName) {
  let e = elem.parent;
  while (e) {
    if (e.tagName === tagName) {
      return e;
    }
    e = e.parent;
  }
  return null;
}

function closestAncestorByClassName(elem, className) {
  let e = elem.parent;
  while (e) {
    if (e.classList.contains(className)) {
      return e;
    }
    e = e.parent;
  }
  return null;
}

function closestDefinition(elem) {
  return closestAncestorByClassName(elem, 'endpoint-test-body').dataset.definition;
}

function onKeyPress(event) {
  if (event.which === 13) {
    event.preventDefault();
    closestDefinition(this).submit();
  }
}

function onSubmit() {
  closestDefinition(this).submit();
}

function onEdit() {
  closestDefinition(this).edit();
}

function onTabSwitch() {
  const index = $(this).parent().children().filter('li').index($(this));
  closestDefinition(this).switchTab(index);
}

function createType(def) {
  const func = require('squalus/' + def.class);
  let params = /\(([^\)]*)\)/.exec(func.prototype.constructor.toString());
  let args = [];
  if (params) {
    params = params[1].split(',').map(Function.prototype.call, String.prototype.trim);
    args = params.map((param) => {
      let value = def[param];
      if (Array.isArray(value) && value[0].class) {
        value = value.map(createType);
      } else if (value && value.class) {
        value = createType(value);
      }
      return value;
    });
  }
  let obj = func.construct(args);
  if (obj.replace) {
    obj = obj.replace();
  }
  return obj;
}

function convertValueToParam(val, key, query) {
  if (Array.isArray(val)) {
    val.forEach((item, i) =>
      convertValueToParam(item, `${key}[${i}]`, query)
    );
  } else if (typeof val === 'object') {
    Object.keys(val).forEach((name) =>
      convertValueToParam(val[name], `${key}[${name}]`, query)
    );
  } else if (typeof val === 'boolean') {
    query[key] = encodeURI(val ? 1 : 0);
  } else {
    query[key] = encodeURI(val);
  }
}

export default class Definition {

  constructor(guid, endpoint, type, params) {
    this._guid = guid;
    this._endpoint = endpoint;
    this._type = type ? createType(type) : null;
    this._params = params;
    this._test = document.getElementById(guid);
    this._body = this._test.querySelector('.endpoint-test-body');
    this._json = this._body.querySelector('.test-json');

    this._body.innerHTML = '';
    this._body.dataset.definition = this;
  }

  syncTabParams() {
    const container = this._body.querySelector('.tab-container');
    if (container) {
      const src = container.children().filter('.current').find('.test-param');
      const dst = container.children().not('.current').find('.test-param');
      src.each(function (i, node) {
        dst.eq(i).val($(node).val());
      });
    }
  }

  updateSingleParam(data) {
    if (this._endpoint.params) {
      if (data.Id) {
        this._body.querySelector('.test-param').dataset.type.populate(data.Id);
{'fraction_non_alphanumeric': 0.17598504905777917, 'fraction_numerical': 0.002024606759071796, 'mean_word_length': 3.18371335504886, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, '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': '4965222', 'n_tokens_mistral': 2063, 'n_tokens_neox': 2069, 'n_words': 428}