New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.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.3.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
736
increased by48.69%
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 install 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 class config class.

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

    class MyClassConfig extends ClassConfigBase {
    
      constructor (initConfig) {
        super(initConfig, defaultConfig)
      }
    
      defineAccessors () {
        return {
          'b.c': (parent, key) => ({
            enumerable: true,
            get () { return parent[key] },
            set (v) { parent[key] = Math.max(0, v) },
          })
        }
      }
      
      defineInterfaces () {
        return {
          myA: (config, myA) => ({ /* readonly property */
            enumerable: true,
            set () {},
            get () { return config.a },
          }),
          myC: (config, myC) => ({ /* writable property */
            enumerable: true,
            set (v) { config.b.c = Math.max(v, 0) },
            get () { return config.b.c },
          }),
          myD: (config, myD) => ({ /* replaceable property */
            enumerable: true,
            configurable: true,
            set (value) { Object.defineProperty(this, myD, {
              enumerable: true,
              configuable: true,
              writable: true,
              value,
            }) },
            get () { return config.b.d },
          }),
        }
      }     
    }
    

    This module prepares some useful functions to define accessors/interfaces simply. By using these functions, the above example can be rewritten as follows:

    const { readonly, writable, replaceable } = ClassConfigBase
    
    class MyClassConfig extends ClassConfigBase {
    
      constructor (initConfig) {
        super(initConfig, defaultConfig)
      }
    
      defineAccessors () {
        return {
          'b.c': (parent, key) => writable({
            get: () => parent[key],
            set: v => { parent[key] = Math.max(0, v) },
          })
        }
      }
      
      defineInterfaces () {
        return {
          myA: config => readonly({
            get: () => config.a,
          },
          myC: config => writable({
            set (v) { config.b.c = Math.max(v, 0) },
            get () { return config.b.c },
          },
          myD: config => replaceable({
            get: () => config.b.d,
          }
        }
      }     
    }    
    
  4. Define the interfacial class with the class config.

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

    const myCfg = new MyClassConfig({ a: 'Foo', b: { c: 123, d: true } })
    const myObj = new MyClass(myCfg)
    
    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.myD = 123
    console.log(myObj.myD) // 123
    
  6. A property value, even if it is read-only, can be updated with the class config object.

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

API

constructor(initConfig, defaultConfig)

Constructs a configuration class instance. initConfig and defaultConfig are 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.

defineAccessors() => object

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

This method is used to override configure accessors of the config class.

Returns:

  • (object) An object which maps between property key chains and functions to get property descriptors of the config class.

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

  defineAccessors () {
    return {
      'a.b.c' : function (parent, key) {
        return {
          enumerable: true,
          get () { return parent[key] },
          set (v) { parent[key] = v },
        }
      },
      ...
    }
  }

This entry is a function of which the arguments are parent and key. In the above example, parent equals to .a.b, and key equals to 'c'.

defineInterfaces() => object

Returns an object which maps between property names and functions which return property descriptors. A descriptor is a thing used by Object.defineProperty.

This method defines the interfaces of the target class.

Returns:

  • (object) An object which maps between property name and functions to get property descriptors of the target class.

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

  defineInterfaces () {
    return {
      'ccc' : function (config, interfaceName) {
        return {
          enumerable: true,
          get () { return config.a.b.c },
          set (v) { config.a.b.c = v },
        }
      },
      ...
    }
  }

This entry is a function of which the arguments are config and interfaceName. In the above example, interfaceName equals to 'ccc'.

configure(instance)

Configures the interfaces of the target class instance in its constructor.

Parameters:

  • instance (object) : A class instance to be configured.

static readonly({ get, enumerable = true }) => object

Returns a readonly property descriptors.

Parameters:

  • get : A getter of this property.
  • enumerable: A flag to show this property during enumeration of the properties.

static writable({ get, set, enumerable = true, configurable = false }) => object

Returns a writable property descriptors.

  • get : A getter of this property.
  • set : A setter of this property.
  • enumerable: A flag to show this property during enumeration of the properties.
  • configurable: A flag to change or delete this property.

static replaceable({ get, enumerable = true }) => object

Returns a replaceable property descriptors.

Parameters:

  • get : A getter of this property.
  • enumerable: A flag to show this property during enumeration of the properties.

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 03 Jun 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