<commit_before>import { $ } from 'luett'

export function inputAdapter (el) {
  return {
    get () {
      return {
        value: el.value,
        name: null
      }
    },
    set (valueObj, propagate = false) {
      el.value = valueObj.value

      if (propagate) {
        el.dispatchEvent(new window.Event('change'))
      }
    }
  }
}

export function selectAdapter (el) {
  return {
    emptyOption: '',
    get () {
      return {
        value: el.options[el.selectedIndex].value,
        name: el.options[el.selectedIndex].textContent
      }
    },
    set (valueObj, propagate = false) {
      const value = valueObj.value

      if (value && !$(`[value="${valueObj.value}"]`, el)) {
        const option = document.createElement('option')
        option.value = valueObj.value
        option.textContent = valueObj.name
        el.appendChild(option)
      }

      el.value = value === null ? this.emptyOption : value

      if (propagate) {
        el.dispatchEvent(new window.Event('change'))
      }
    }
  }
}

export default el => {
  const tagName = (el.tagName || el).toLowerCase()

  switch (tagName) {
    case 'select':
      return selectAdapter(el)
    case 'input':
      return inputAdapter(el)
  }
}
<commit_msg>add new value-submission dom adapter<commit_after>import { $ } from 'luett'

export function inputAdapter (el) {
  return {
    get () {
      return {
        value: el.value,
        name: null
      }
    },
    set (valueObj, propagate = false) {
      el.value = valueObj.value

      if (propagate) {
        el.dispatchEvent(new window.Event('change'))
      }
    }
  }
}

export function selectAdapter (el) {
  return {
    emptyOption: '',
    get () {
      return {
        value: el.options[el.selectedIndex].value,
        name: el.options[el.selectedIndex].textContent
      }
    },
    set (valueObj, propagate = false) {
      const value = valueObj.value

      if (value && !$(`[value="${valueObj.value}"]`, el)) {
        const option = document.createElement('option')
        option.value = valueObj.value
        option.textContent = valueObj.name
        el.appendChild(option)
      }

      el.value = value === null ? this.emptyOption : value

      if (propagate) {
        el.dispatchEvent(new window.Event('change'))
      }
    }
  }
}

export function valueSubmissionAdapter (inputAdapter, submit) {
  const iface = {
    get: (...args) => inputAdapter.get(...args),
    set: (valueObj, ...args) => {
      const currentValue = iface.get().value
      inputAdapter.set(valueObj, ...args)
      if (currentValue !== valueObj.value) {
        if (!submit) {
          console.error('tried to auto-submit value but submit is missing')
        } else {
          submit.click()
        }
      }
    }
  }
  return iface
}

export default el => {
  const tagName = (el.tagName || el).toLowerCase()

  switch (tagName) {
    case 'select':
      return selectAdapter(el)
    case 'input':
      return inputAdapter(el)
  }
}

{'fraction_non_alphanumeric': 0.13571190396798932, 'fraction_numerical': 0.0, 'mean_word_length': 2.1545741324921135, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4012181', 'n_tokens_mistral': 929, 'n_tokens_neox': 914, 'n_words': 201}