Sha256: 5ccdcc4ebcb10d192ec9c1a4d53d90b86622da029f19f4646681d40dac249000

Contents?: true

Size: 1.53 KB

Versions: 15

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

15 entries across 15 versions & 2 rubygems

Version Path
webpacker-3.6.0 package/config_types/config_list.js
optimacms-0.4.3 spec/dummy/node_modules/@rails/webpacker/package/config_types/config_list.js
optimacms-0.4.2 spec/dummy/node_modules/@rails/webpacker/package/config_types/config_list.js
webpacker-3.5.5 package/config_types/config_list.js
webpacker-3.5.3 package/config_types/config_list.js
webpacker-3.5.2 package/config_types/config_list.js
webpacker-3.5.1 package/config_types/config_list.js
webpacker-3.5.0 package/config_types/config_list.js
webpacker-3.4.3 package/config_types/config_list.js
webpacker-4.0.0.pre.pre.2 package/config_types/config_list.js
webpacker-4.0.0.pre.pre.1 package/config_types/config_list.js
webpacker-3.4.1 package/config_types/config_list.js
webpacker-3.4.0 package/config_types/config_list.js
webpacker-3.3.1 package/config_types/config_list.js
webpacker-3.3.0 package/config_types/config_list.js