<commit_before>import SlimSelect from './index'

interface constructor { main: SlimSelect }

export type dataArray = dataObject[]
export type dataObject = optgroup | option
export interface optgroup {
  label: string
  options?: option[]
}

export interface option {
  id?: string
  value?: string
  text: string
  innerHTML?: string
  selected?: boolean
  display?: boolean
  disabled?: boolean
  placeholder?: boolean
  data?: object
}

// Class is responsible for managing the data
export default class Data {
  main: SlimSelect
  searchValue: string
  data: dataObject[]
  filtered: dataObject[] | null
  contentOpen: boolean = false
  contentPosition: string = 'below'
  isOnChangeEnabled: boolean = true
  constructor(info: constructor) {
    this.main = info.main
    this.searchValue = ''
    this.data = []
    this.filtered = null

    this.parseSelectData()
    this.setSelectedFromSelect()
  }

  newOption(info: any): option {
    return {
      id: (info.id ? info.id : String(Math.floor(Math.random() * 100000000))),
      value: (info.value ? info.value : ''),
      text: (info.text ? info.text : ''),
      innerHTML: (info.innerHTML ? info.innerHTML : ''),
      selected: (info.selected ? info.selected : false),
      display: (info.display ? info.display : true),
      disabled: (info.disabled ? info.disabled : false),
      placeholder: (info.placeholder ? info.placeholder : false),
      data: (info.data ? info.data : {}),
    }
  }

  // Add to the current data array
  add(data: option) {
    let dataObject: option = {
      id: String(Math.floor(Math.random() * 100000000)),
      value: data.value,
      text: data.text,
      innerHTML: '',
      selected: false,
      display: true,
      disabled: false,
      placeholder: false,
      data: {}
    }

    this.data.push(dataObject)
  }

  // From passed in select element pull optgroup and options into data
  parseSelectData() {
    this.data = []
    // Loop through nodes and create data
    var element: HTMLSelectElement = this.main.select.element
    var nodes = element.childNodes
    for (var i = 0; i < nodes.length; i++) {
      if (nodes[i].nodeName === 'OPTGROUP') {
        let node = nodes[i] as HTMLOptGroupElement
        let optgroup = {
          label: node.label,
          options: [] as option[]
        }
        let options = nodes[i].childNodes
        for (var ii = 0; ii < options.length; ii++) {
          if (options[ii].nodeName === 'OPTION') {
            let option = this.pullOptionData(options[ii] as HTMLOptionElement)
            optgroup.options.push(option)

            // If option has placeholder set to true set placeholder text
            if (option.placeholder) {
              this.main.config.placeholderText = option.text
            }
          }
        }
        this.data.push(optgroup)
      } else if (nodes[i].nodeName === 'OPTION') {
        let option = this.pullOptionData(nodes[i] as HTMLOptionElement)
        this.data.push(option)

        // If option has placeholder set to true set placeholder text
        if (option.placeholder) {
          this.main.config.placeholderText = option.text
        }
      }
    }
  }

  // From passed in option pull pieces of usable information
  pullOptionData(option: HTMLOptionElement): option {
    return {
      id: (option.dataset ? option.dataset.id : false) || String(Math.floor(Math.random() * 100000000)),
      value: option.value,
      text: option.text,
      innerHTML: option.innerHTML,
      selected: option.selected,
      disabled: option.disabled,
      placeholder: option.dataset.placeholder === 'true',
      data: option.dataset
    }
  }

  // From select element get current selected and set selected
  setSelectedFromSelect(): void {
    let options = this.main.select.element.options
    if (this.main.config.isMultiple) {
      let newSelected: string[] = []
      for (var i = 0; i < options.length; i++) {
        let option = <HTMLOptionElement>options[i]
        if (option.selected) {<commit_msg>data - only replace placeholder text if text is not empty<commit_after>import SlimSelect from './index'

interface constructor { main: SlimSelect }

export type dataArray = dataObject[]
export type dataObject = optgroup | option
export interface optgroup {
  label: string
  options?: option[]
}

export interface option {
  id?: string
  value?: string
  text: string
  innerHTML?: string
  selected?: boolean
  display?: boolean
  disabled?: boolean
  placeholder?: boolean
  data?: object
}

// Class is responsible for managing the data
export default class Data {
  main: SlimSelect
  searchValue: string
  data: dataObject[]
  filtered: dataObject[] | null
  contentOpen: boolean = false
  contentPosition: string = 'below'
  isOnChangeEnabled: boolean = true
  constructor(info: constructor) {
    this.main = info.main
    this.searchValue = ''
    this.data = []
    this.filtered = null

    this.parseSelectData()
    this.setSelectedFromSelect()
  }

  newOption(info: any): option {
    return {
      id: (info.id ? info.id : String(Math.floor(Math.random() * 100000000))),
      value: (info.value ? info.value : ''),
      text: (info.text ? info.text : ''),
      innerHTML: (info.innerHTML ? info.innerHTML : ''),
      selected: (info.selected ? info.selected : false),
      display: (info.display ? info.display : true),
      disabled: (info.disabled ? info.disabled : false),
      placeholder: (info.placeholder ? info.placeholder : false),
      data: (info.data ? info.data : {}),
    }
  }

  // Add to the current data array
  add(data: option) {
    let dataObject: option = {
      id: String(Math.floor(Math.random() * 100000000)),
      value: data.value,
      text: data.text,
      innerHTML: '',
      selected: false,
      display: true,
      disabled: false,
      placeholder: false,
      data: {}
    }

    this.data.push(dataObject)
  }

  // From passed in select element pull optgroup and options into data
  parseSelectData() {
    this.data = []
    // Loop through nodes and create data
    var element: HTMLSelectElement = this.main.select.element
    var nodes = element.childNodes
    for (var i = 0; i < nodes.length; i++) {
      if (nodes[i].nodeName === 'OPTGROUP') {
        let node = nodes[i] as HTMLOptGroupElement
        let optgroup = {
          label: node.label,
          options: [] as option[]
        }
        let options = nodes[i].childNodes
        for (var ii = 0; ii < options.length; ii++) {
          if (options[ii].nodeName === 'OPTION') {
            let option = this.pullOptionData(options[ii] as HTMLOptionElement)
            optgroup.options.push(option)

            // If option has placeholder set to true set placeholder text
            if (option.placeholder && option.text.trim() !== '') {
              this.main.config.placeholderText = option.text
            }
          }
        }
        this.data.push(optgroup)
      } else if (nodes[i].nodeName === 'OPTION') {
        let option = this.pullOptionData(nodes[i] as HTMLOptionElement)
        this.data.push(option)

        // If option has placeholder set to true set placeholder text
        if (option.placeholder && option.text.trim() !== '') {
          this.main.config.placeholderText = option.text
        }
      }
    }
  }

  // From passed in option pull pieces of usable information
  pullOptionData(option: HTMLOptionElement): option {
    return {
      id: (option.dataset ? option.dataset.id : false) || String(Math.floor(Math.random() * 100000000)),
      value: option.value,
      text: option.text,
      innerHTML: option.innerHTML,
      selected: option.selected,
      disabled: option.disabled,
      placeholder: option.dataset.placeholder === 'true',
      data: option.dataset
    }
  }

  // From select element get current selected and set selected
  setSelectedFromSelect(): void {
    let options = this.main.select.element.options
    if (this.main.config.isMultiple) {
      let newSelected: string[] = []
      for (var i = 0; i < options.length; i++) {
        let option = <HTMLOptionElement>options[i]
        if (option.selected) {
{'fraction_non_alphanumeric': 0.11821753007611098, 'fraction_numerical': 0.007365578197888534, 'mean_word_length': 2.57011393514461, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 6, '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': '1843085', 'n_tokens_mistral': 2383, 'n_tokens_neox': 2278, 'n_words': 653}