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)
myConfig.a = 123
myConfig.b.c = 'ABC'
myConfig.b.d = true
console.log(myConfig)
myConfig = new MyClassConfig({ a: 9, b: { c: 'Z', d: true } })
console.log(myConfig)
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)
myConfig.a = 123
myConfig.b.d = true
myConfig.b.c = 'ABC'
console.log(myConfig)
myConfig.$private.a = 123
console.log(myConfig)
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
myObj.myC
myObj.myD
myObj.toString()
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.