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.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
620
increased by26.79%
Maintainers
1
Weekly downloads
 
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

  1. Load this module.

    const ClassConfigBase = require('class-config-base')
    
  2. Define default config object. This object determines the property default values, the property structure and the property data types of the target class config object.

    const defaultConfig = { a: '', b: { c: 0, d: false } }
    
  3. Define the class config class. getAccessorDescriptors method is optional and creates descriptors to override property accessors. getInterfaceDescriptors method creates descriptors to define properties and methods of the target interfacial class.

    class MyClassConfig extends ClassConfigBase {
      constructor (initConfig) {
        super(initConfig, defaultConfig)
      }
    
      getAccessorDescriptors () {
        return {
          'b.c': parent => ({
            get () { return parent.a },
            set (v) { parent.a = Math.max(0, v) },
          }),
        }
      }
    
      getInterfaceDescriptors () {
        const self = this
        return {
          myA: {  /* to make a read-only property */
            enumerable: true,
            set () {},
            get () { return self.a },
          },
          
          myC: {  /* to make a restricted property */
            enumerable: true,
            set (v) { if (self.b.d) { self.b.c = v } },
            get () { return self.b.c },
          },
          
          myD: {
            enumerable: true,
            set (v) { self.b.d = v },
            get () { return self.b.d },
          },
        }
      }
    }
    
  4. Define the interfacial class with the class config.

    class MyClass {
      constructor (myClassConfig) {
        myClassConfig.configure(this)
      }
    }
    
  5. Instantiate and use the interfacial class.

    const myClassConfig = new MyClassConfig({ a: 'Foo', b: { c: 123, d: true } })
    const myObj = new MyClass(myClassConfig)
    
    console.log(myObj.toString())  // [object MyClass]
    console.log(Object.prototype.toString.call(myObj)) // [object MyClass]
    console.log(myObj.myA) // 'Foo'
    console.log(myObj.myC) // 123
    console.log(myObj.myD) // true
    
    myObj.myA = 'Bar'
    console.log(myObj.myA) // 'Foo'
    
    myObj.myC = 999
    console.log(myObj.myC) // 999
    
    myObj.myC = -888
    console.log(myObj.myC) // 0
    
    myObj.myD = false
    console.log(myObj.myD) // false
    
    myObj.myC = 777
    console.log(myObj.myC) // 0
    
  6. A property value, even if it is read-only, can be updated with the class config object.

    myClassConfig.a = 'Buz'
    myClassConfig.b.c = 666
    
    console.log(myObj.myA) // 'Buz'
    console.log(myObj.myC) // 666
    console.log(myObj.myD) // false
    

API

constructor(initConfig, defaultConfig)

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

Parameters:

  • 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 is to override configure accessors of the config class.

Returns:

  • (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. In the above example, parent equals to config.a.b, and key equals to '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 defines the 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 */
      'c' : {
        return {
          enumerable: true,
          configurable: false,
          get () { return self.a.b.c },
          set (v) { self.a.b.c = v },
        }
      },
      ...
    }
  }

configure(instance)

Configures the interfaces of 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 14 May 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