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.

  • 1.0.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 which configures a interfacial class.

Install

npm install class-config-base

Load this module

For Node.js

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

For Web browser (only supporting es6)

<script src="class-config-base.min.js"></script>

Usage

  1. 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: 1 } }
    
  2. Define the class config class.

    • defineMorePrivates method is optional and provides a timing to define more private data than private data defined by defaultConfig.
    • 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 ClassConfig {
    
      constructor (initConfig) {
        super(initConfig, defaultConfig)
      }
    
      defineMorePrivates ($private) {
        $private.e = { f: [1, 2, 3] }
      }
    
      defineAccessors ($private, config) {
        return {
          'b.c': {
            enumerable: true,
            get () { return $private.b.c },
            set (v) { $private.b.c = Math.max(0, v) },
          }
        }
      }
    
      defineInterfaces (config, instance) {
        return {
          myA: { /* readonly property */
            enumerable: true,
            set () {},
            get () { return config.a },
          },
          myC: { /* writable property */
            enumerable: true,
            set (v) { config.b.c = v },
            get () { return config.b.c },
          },
          myF: { /* replaceable property */
            enumerable: true,
            configurable: true,
            set (value) { Object.defineProperty(instance, 'myF', {
              enumerable: true,
              configuable: true,
              writable: true,
              value,
            }) },
            get () { return config.e.f },
          },
          myG: { /* method property */
            enumerable: true,
            configurable: true,
            writable: true,
            value: (v) => { return config.b.d * v },
          },
        }
      }
    }
    

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

    const { readonly, writable, replaceable, method } = ClassConfig
    
    class MyClassConfig extends ClassConfig {
    
      constructor (initConfig) {
        super(initConfig, defaultConfig)
      }
    
      defineMorePrivates ($private) {
        $private.e = { f: [1, 2, 3] }
      }
    
      defineAccessors ($private, config) {
        return {
          'b.c': writable({
            get () { return $private.b.c },
            set (v) { $private.b.c = Math.max(0, v) },
          }),
        }
      }
    
      defineInterfaces (config, instance) {
        return {
          myA: readonly({ get: () => config.a }),
          myC: writable({
            set: v => { config.b.c = v },
            get: () => config.b.c,
          }),
          myF: replaceable({ get: () => config.e.f }),
          myG: method((v) => { return config.b.d * v }),
        }
      }
    }
    
  3. Define the interfacial class with the class config.

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

    The interfaces of interfacial class can be also defined by following way:

    class MyClassConfig extends ClassConfig {
      constructor (initConfig) { ... }
      defineMorePrivates ($private) { ... }
      defineAccessors ($private, config) { ... }
    }
    
    class MyClass {
      constructor (config) {
        config.configure(this, {
    
          myA: readonly({ get: () => config.a }),
    
          myC: writable({
            set: v => { config.b.c = v },
            get: () => config.b.c,
          }),
    
          myF: replaceable({ get: () => config.e.f }),
    
          myG: method((v) => { return config.b.d * v }),
        })
      }
    }
    
  4. 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.myF) // [1, 2, 3]
    console.log(myObj.myG(2)) // 2
    
    myObj.myA = 'Bar'
    console.log(myObj.myA) // 'Foo'
    
    myObj.myC = 999
    console.log(myObj.myC) // 999
    
    myObj.myF = 123
    console.log(myObj.myF) // 123
    
  5. A property value, even if it is read-only or hidden, can be updated with the class config object.

    myCfg.a = 'Buz'
    myCfg.b.c = 666
    myCfg.b.d = 888
    
    console.log(myObj.myA) // 'Buz'
    console.log(myObj.myC) // 666
    console.log(myObj.myG(2)) // 1776
    
  6. A mapping between a config class instance and a interfacial class instance can be managed by ClassConfig.Manager object.

    const { Manager } = ClassConfig
    
    const manager = new Manager()  // Create a manager
    
    manager.set(myCfg, myObj)  // Set a mapping
    
    const aCfg = manager.getConfig(myObj)  // Get the configure object
    const aObj = manager.getObject(myCfg)  // Get the interfacial object
    
    manager.delete(aObj)  // Delete a mapping
    

API

class ClassConfig

Is a class to configure the target class instance from hiding place.

.constructor (initConfig, defaultConfig) => ClassConfig

Is a constructor to creates an instance of this class. 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 type of a property in initConfig is different from a type of a corresponding property in defaultConfig, the property value in initConfig is ignored.

Parameters:

ParameterTypeDescription
initConfigobjectA configuration object which has initial property values.
defaultConfigobjectA configuration object which has default property values.

Returns:

A ClassConfig object.

.configure (instance, descriptors) => Void

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

Parameters:

ParameterTypeDescription
instanceobjectA class instance to be configured.
descriptorsobjectA plain object which has descriptors of interfaces of the target class instance.
.defineMorePrivates ($private) => Void

Defines more private data than private data defined in defaultConfig.

Parameters:

ParameterTypeDescription
$privateobjectThe root object to store private data of the config object.
.defineAccessors ($private, config) => object

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

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

Parameters:

ParameterTypeDescription
$privateobjectThe root object to store private data of the config object.
configClassConfigThis config object.

Returns:

A nested plain object which contains property descriptors of accessors of this config object.

.defineInterfaces (config, instance) => Void

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

This method defines the interfaces of the target class.

Parameters:

ParameterTypeDescription
configClassConfigThis config object.
instanceobjectThe instance of the interfacial class configured by this config object.
[static] .readonly ({ getter [, enumerable ] }) => object

Returns a readonly property descriptor.

Parameters:

ParameterTypeDescription
getterfunctionA getter for this property.
enumerablebooleanA flag to show this property during enumeration of the properties.

Return:

A property descriptor of the target readonly property.

[static] .writable ({ getter, setter, [, enumerable ] [, configurable ] }) => object

Returns a writable property descriptor.

ParameterTypeDescriptor
getterfunctionA getter for this property.
setterfunctionA setter for this property.
enumerablebooleanA flag to show this property during enumeration of the properties.
configurablebooleanA flag to change or delete this property.

Return:

A property descriptor of the target writable property.

[static] .replaceable ({ getter [, enumerable ] }) => object

Returns a replaceable property descriptor.

Parameters:

ParameterTypeDescription
getfunctionA getter for this property.
enumerablebooleanA flag to show this property during enumeration of the properties.

Return:

A property descriptor of the target replaceable property.

[static] .method (fn) : object

Returns a property descriptor for a method.

Parameters:

ParameterTypeDescription
fnfunctionA method function for this property.

Return:

A property descriptor of the target method property.

class ClassConfig.Manager

Is a manager class which has mappings of a config object and an object configured by it.

.constructor () => ClassConfig.Manager

Creates an instance of this class.

Returns:

A ClassConfig.Manager object.

.set (object, config) => Void

Sets a mapping of a config object and an object configured by it.

Parameters:

ParameterTypeDescription
objectobjectThe object configured by the config object.
configClassConfigThe config object.
.delete (objectOrConfig) => Void

Deletes a mapping of a config object and an object configured by it.

Parameters:

ParameterTypeDescription
objectOrConfigobject |ClassConfigThe object or config object to be deleted its mapping from this manager object.
.getConfig (object) => ClassConfig

Gets a config object corresponding to the specified object.

Parameters:

ParameterTypeDescription
objectobjectThe object registered with this manager object.

Returns:

The config object corresponding to the specified object.

.getObject (config) => object

Get an object corresponding to the specified config object.

Parameters:

ParameterTypeDescription
configClassConfigThe config object registered with this manager object.

Returns:

The object corresponding to the specified config object.

License

Copyright (C) 2017-2018 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 08 Jul 2018

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