Sha256: 08eac6b447a20b890882b0f821e4cb08819ddc0a278d4c936631a02ac12faae9

Contents?: true

Size: 1.52 KB

Versions: 28

Compression:

Stored size: 1.52 KB

Contents

/**
  * @class
  * @extends { Array }
*/
class ConfigList extends Array {
  static get [Symbol.species]() { return Array }

  get(key) {
    const index = this.getIndex(key, true)
    return this[index].value
  }

  append(key, value) {
    return this.add({ key, value })
  }

  prepend(key, value) {
    return this.add({ key, value }, 'prepend')
  }

  insert(key, value, pos = {}) {
    if (!(pos.before || pos.after)) return this.append(key, value)

    const currentIndex = this.getIndex(key)
    if (currentIndex >= 0) this.splice(currentIndex, 1)

    let newIndex = this.getIndex(pos.before || pos.after)
    if (pos.after) newIndex += 1

    this.splice(newIndex, 0, { key, value })
    return this
  }

  delete(key) {
    const index = this.getIndex(key, true)
    this.splice(index, 1)
    return this
  }

  getIndex(key, shouldThrow = false) {
    const index = this.findIndex(entry => (
      entry === key
        || entry.key === key
        || (entry.constructor && entry.constructor.name === key)
    ))

    if (shouldThrow && index < 0) throw new Error(`Item ${key} not found`)
    return index
  }

  add({ key, value }, strategy = 'append') {
    const index = this.getIndex(key)
    if (index >= 0) this.delete(key)

    switch (strategy) {
      case 'prepend':
        this.unshift({ key, value })
        break
      default:
        this.push({ key, value })
    }

    return this
  }

  values() {
    return this.map(item => item.value)
  }

  keys() {
    return this.map(item => item.key)
  }
}

module.exports = ConfigList

Version data entries

28 entries across 28 versions & 5 rubygems

Version Path
disco_app-0.18.0 test/dummy/node_modules/@rails/webpacker/package/config_types/config_list.js
disco_app-0.18.2 test/dummy/node_modules/@rails/webpacker/package/config_types/config_list.js
chatops-rpc-0.0.2 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/webpacker-4.0.7/package/config_types/config_list.js
chatops-rpc-0.0.1 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/webpacker-4.0.7/package/config_types/config_list.js
jester-data-8.0.0 node_modules/@rails/webpacker/package/config_types/config_list.js
ezii-os-5.2.1 node_modules/@rails/webpacker/package/config_types/config_list.js
ezii-os-2.0.1 node_modules/@rails/webpacker/package/config_types/config_list.js
ezii-os-1.1.0 node_modules/@rails/webpacker/package/config_types/config_list.js
ezii-os-1.0.0 node_modules/@rails/webpacker/package/config_types/config_list.js
ezii-os-0.0.0.1.0 node_modules/@rails/webpacker/package/config_types/config_list.js
ezii-os-0.0.0.0.1 node_modules/@rails/webpacker/package/config_types/config_list.js
webpacker-4.0.7 package/config_types/config_list.js
webpacker-4.0.6 package/config_types/config_list.js
webpacker-4.0.5 package/config_types/config_list.js
webpacker-4.0.4 package/config_types/config_list.js
webpacker-4.0.3 package/config_types/config_list.js
webpacker-4.0.2 package/config_types/config_list.js
webpacker-4.0.1 package/config_types/config_list.js
webpacker-4.0.0 package/config_types/config_list.js
webpacker-4.0.0.rc.8 package/config_types/config_list.js