Sha256: 7123bab379d3d02583bfb470b42085e78e281263da96b6e4a7edd74e6b796e33

Contents?: true

Size: 1.15 KB

Versions: 46

Compression:

Stored size: 1.15 KB

Contents

const objectify = require('../utils/objectify')
const deepAssign = require('../utils/deep_assign')
const deepMerge = require('../utils/deep_merge')
const { isStrPath, prettyPrint } = require('../utils/helpers')

/**
  * @class
  * @extends { Object }
*/
class ConfigObject extends Object {
  constructor(props = {}) {
    super()
    this.merge(props)
  }

  get(key) {
    return isStrPath(key) ? objectify(key, this) : this[key]
  }

  set(key, value) {
    Object.assign(this, deepAssign(this, key, value))
    return this
  }

  delete(key) {
    let obj = this
    let propKey = key

    if (isStrPath(key)) {
      const keys = key.split('.')
      propKey = keys.pop()
      const parentObjPath = keys.join('.')
      obj = objectify(parentObjPath, this)
    }

    if (!obj) throw new Error(`Prop not found: ${key} in ${prettyPrint(obj)}`)
    delete obj[propKey]

    return this
  }

  toObject() {
    const object = {}
    /* eslint no-return-assign: 0 */
    Object.keys(this).forEach((key) => (object[key] = this[key]))
    return object
  }

  merge(config) {
    Object.assign(this, deepMerge(this, config))
    return this
  }
}

module.exports = ConfigObject

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_object.js
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/webpacker-5.4.4/package/config_types/config_object.js
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/webpacker-5.4.4/package/config_types/config_object.js
optimacms-0.1.61 spec/dummy/node_modules/@rails/webpacker/package/config_types/config_object.js
jetpacker-0.7.0 package/config_types/config_object.js
webpacker-5.4.4 package/config_types/config_object.js
jetpacker-0.6.0 package/config_types/config_object.js
disco_app-0.16.1 test/dummy/node_modules/@rails/webpacker/package/config_types/config_object.js
disco_app-0.15.2 test/dummy/node_modules/@rails/webpacker/package/config_types/config_object.js
disco_app-0.18.4 test/dummy/node_modules/@rails/webpacker/package/config_types/config_object.js
disco_app-0.18.1 test/dummy/node_modules/@rails/webpacker/package/config_types/config_object.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/@rails/webpacker/package/config_types/config_object.js
disco_app-0.14.0 test/dummy/node_modules/@rails/webpacker/package/config_types/config_object.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/@rails/webpacker/package/config_types/config_object.js
webpacker-5.4.3 package/config_types/config_object.js
webpacker-5.4.2 package/config_types/config_object.js
webpacker-5.4.1 package/config_types/config_object.js
webpacker-5.4.0 package/config_types/config_object.js
webpacker-5.3.0 package/config_types/config_object.js
webpacker-5.2.2 package/config_types/config_object.js