Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

class-config-base

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

class-config-base

The base class of a configuration class for a interfacial class.

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

class-config-base NPM MIT License Build Status Build Status Coverage Status

The base class of a configuration class for a interfacial class.

Install

npm i class-config-base --save

Usage

Load this module :

const ClassConfigBase = require('class-config-base')

Define a configration class :

const defaultConfig = {
  a: 0,
  b: { c: '', d: false },
}

class MyClassConfig extends ClassConfigBase {
  constructor (initConfig) {
    super(initConfig, defaultConfig)
  }
}

let myConfig = new MyClassConfig()
console.log(myConfig) // => MyClassConfig { a: 0, b: { c: '', d: false } }

myConfig.a = 123
myConfig.b.c = 'ABC'
myConfig.b.d = true
console.log(myConfig) // => MyClassConfig { a: 123, b: { c: 'ABC', d: true } }

myConfig = new MyClassConfig({ a: 9, b: { c: 'Z', d: true } })
console.log(myConfig) // => MyClassConfig { a: 9, b: { c: 'Z', d: true } }

Define accessors of the configuration class

class MyClassConfig extends ClassConfigBase {
  constructor (initConfig) {
    super(initConfig, defaultConfig)
  }

  getAccessorDescriptors () {
    return {
      'a': parent => ({
        enumerable: true,
        get () { return parent.a },
        set (v) {},
      })

      'b.c': parent => ({
        get () { return parent.c },
        set (v) {
          if (parent.d) {
            parent.c = v
          }
        },
      }),
    }
  }
}

let myConfig = new MyConfig()
console.log(myConfig) // => MyClassConfig { a: 0, b: { c: '', d: false } }

myConfig.a = 123
myConfig.b.d = true
myConfig.b.c = 'ABC'
console.log(myConfig) // => MyClassConfig { a: 0, b: { c: 'ABC', d: true } }

myConfig.$private.a = 123  // `$private` property holds the entity
console.log(myConfig) // => MyClassConfig { a: 123, b: { c: 'ABC', d: true } }

Define a interfacial class which doesn't have property entities :

class MyClassConfig extends ClassConfigBase {
  constructor (initConfig) {
    super(initConfig, defaultConfig)
  }

  getAccessorDescriptors () {
    return {
      'a' : parent => ({
        get () { return parent.a },
        set (v) { parent.a = Math.max(0, v) }
      }),

      'b.c': parent => ({
        get () { return parent.c },
        set (v) {
          if (parent.d) {
            parent.c = v
          }
        },
      }),
    }
  }

  getInterfaceDescriptors () {
    const self = this

    return {
      myA: {
        enumerable: true,
        set (v) { self.a = v },
        get () { return self.a },
      },

      myC: {
        enumerable: true,
        set (v) { self.b.c = v },
        get () { return self.b.c },
      },

      myD: {
        enumerable: true,
        set (v) { self.b.d = v },
        get () { return self.b.d },
      },

      toString: {
        set (v) { this.toString = v },
        get () { return () => stringer(this) },
      },
    }
  }
}

class MyClass {
  constructor(myConfig) {
    myConfig.configure(this)
  }
}

let myConfig = new MyConfig({ a: 999, b: { c: 'AAA', d: true } })
let myObj = new MyClass(myConfig)

myObj.myA // => 999
myObj.myC // => 'AAA'
myObj.myD // => true
myObj.toString() // => MyClass { myA: 999, myB: 'AAA', myC: true }

API

constructor(initConfig, defaultConfig)

Constructs a configuration class instance. initConfig and defaultConfig is plain objects and may be nested objects. defaultConfig is to specify the default values and the type of the properties. So if a property of initConfig is different from a corresponding property of defaultConfig, the property value of initConfig is ignored.

Parameter:

  • initConfig [object] : a configuration object which has initial property values.
  • defaultConfig [object] : a configuration object which has default property values.

getAccessorDescriptors() => object

Returns an object which maps between property key chains and functions which get property descriptors. A key chain concatenates all keys in a key path with dots. A descriptor is the one used by Object.defineProperty.

This method may override to configure accessors of the config class.

Return: [object]

An object which maps between property key chains and property descriptors of the config class instance.

The format of an entry in the returned object is as follows:

  getAccessorDescriptors () {
    return {
      /* An example of an entry in the returned object */
      'a.b.c' : function (parent, key, Info) {
        return {
          enumerable: true,
          configurable: false,
          get () { return parent[key] },
          set (v) { parent[key] = v },
        }
      },
      ...
    }
  }

The entry is a function of which the arguments are parent, key, info. parent is same with config.a.b, and key is same with 'c'. info is same with nodeInfo of each-props.

getInterfaceDescriptors() => object

Returns an object which maps between property name and property descriptors. A descriptor is the one used by Object.defineProperty.

This method may override to configure interfaces of the target class.

Returns: [object]

An object which maps between property name and property descriptors of the target class.

The format of an entry in the returned object is as follows:

  getInterfaceDescriptors () {
    const self = this
    
    return {
      /* An example of an entry in the returned object */
      'a' : {
        return {
          enumerable: true,
          configurable: false,
          get () { return self.a },
          set (v) { self.a = v },
        }
      },
      ...
    }
  }

configure(instance)

Configures the target class instance in its constructor.

Parameters:

  • instance [object] : A class instance to be configured.

License

Copyright (C) 2017 Takayuki Sato

This program is free software under MIT License. See the file LICENSE in this distribution for more details.

Keywords

FAQs

Package last updated on 15 Apr 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc