<commit_before>const { hasProperty } = require('../utils');

module.exports = function oneOf(schema, tpl) {
  if (!hasProperty(schema, 'oneOf')) {
    return;
  }

  const fns = schema.oneOf.map(reference => tpl.link(reference));
  const arr = tpl.cache(`[${fns}]`);
  const cachedArr = tpl.cache(`[${fns}]`);
  const iterator = tpl.cache(`${cachedArr}.length - 1`);
  const cachedIterator = tpl.cache(`${cachedArr}.length - 1`);
  const count = tpl.cache('0');
  const cachedCount = tpl.cache('0');

  tpl(
    'for ($1, $3, $5; $4 >= 0 && $4 < $2.length; $4--) {',
    arr,
    cachedArr,
    iterator,
    cachedIterator,
    count
  )('if(!%s[%s](%s))', cachedArr, cachedIterator, tpl.data)('%s++', cachedCount)('}')('if (%s !== 1)', cachedCount)(tpl.error('oneOf'));
};<commit_msg>refactor(template): Clean oneOf validator function<commit_after>const { hasProperty } = require('../utils');

module.exports = function oneOf(schema, tpl) {
  if (!hasProperty(schema, 'oneOf')) {
    return;
  }

  const fns = schema.oneOf.map(reference => tpl.link(reference));
  const arr = tpl.cache(`[${fns}]`);
  const cachedArr = tpl.cache(`[${fns}]`);
  const index = tpl.cache(`${cachedArr}.length - 1`);
  const cachedIndex = tpl.cache(`${cachedArr}.length - 1`);
  const count = tpl.cache('0');
  const cachedCount = tpl.cache('0');
  const error = tpl.error('oneOf');

  tpl(`for (
    ${arr}, ${index}, ${count};
    ${cachedIndex} >= 0 && ${cachedIndex} < ${cachedArr}.length;
    ${cachedIndex}--) {
      if(!${cachedArr}[${cachedIndex}](${tpl.data})) ${cachedCount}++;
    }
    if (${cachedCount} !== 1) ${error}
  `);
};
{'fraction_non_alphanumeric': 0.2333743842364532, 'fraction_numerical': 0.011699507389162561, 'mean_word_length': 4.110062893081761, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '776894', 'n_tokens_mistral': 588, 'n_tokens_neox': 587, 'n_words': 115}