Sha256: 51e21d187ac225285a2673265826c4b4520a5bc3e445a1701e10023a388f4528

Contents?: true

Size: 1.53 KB

Versions: 46

Compression:

Stored size: 1.53 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

46 entries across 45 versions & 9 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/webpacker-5.4.4/package/config_types/config_list.js
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/webpacker-5.4.4/package/config_types/config_list.js
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/webpacker-5.4.4/package/config_types/config_list.js
optimacms-0.1.61 spec/dummy/node_modules/@rails/webpacker/package/config_types/config_list.js
jetpacker-0.7.0 package/config_types/config_list.js
webpacker-5.4.4 package/config_types/config_list.js
jetpacker-0.6.0 package/config_types/config_list.js
disco_app-0.16.1 test/dummy/node_modules/@rails/webpacker/package/config_types/config_list.js
disco_app-0.15.2 test/dummy/node_modules/@rails/webpacker/package/config_types/config_list.js
disco_app-0.18.4 test/dummy/node_modules/@rails/webpacker/package/config_types/config_list.js
disco_app-0.18.1 test/dummy/node_modules/@rails/webpacker/package/config_types/config_list.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/@rails/webpacker/package/config_types/config_list.js
disco_app-0.14.0 test/dummy/node_modules/@rails/webpacker/package/config_types/config_list.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/@rails/webpacker/package/config_types/config_list.js
webpacker-5.4.3 package/config_types/config_list.js
webpacker-5.4.2 package/config_types/config_list.js
webpacker-5.4.1 package/config_types/config_list.js
webpacker-5.4.0 package/config_types/config_list.js
webpacker-5.3.0 package/config_types/config_list.js
webpacker-5.2.2 package/config_types/config_list.js