class-config-base
Advanced tools
Comparing version 0.3.1 to 1.0.0
'use strict' | ||
const ClassConfigBase = require('./lib/class-config-base') | ||
const Config = require('./lib/config') | ||
const Manager = require('./lib/manager') | ||
Object.defineProperty(ClassConfigBase, 'ClassConfigManager', { | ||
Object.defineProperty(Config, 'Manager', { | ||
enumerable: true, | ||
value: require('./lib/class-config-manager') | ||
value: Manager, | ||
}) | ||
module.exports = ClassConfigBase | ||
module.exports = Config |
{ | ||
"name": "class-config-base", | ||
"version": "0.3.1", | ||
"version": "1.0.0", | ||
"description": "The base class of a configuration class for a interfacial class.", | ||
@@ -8,3 +8,4 @@ "main": "index.js", | ||
"index.js", | ||
"lib" | ||
"lib", | ||
"web" | ||
], | ||
@@ -16,6 +17,6 @@ "scripts": { | ||
"coveralls": "nyc --reporter=text-lcov npm test | coveralls", | ||
"web:build": "browserify index.js --standalone ClassConfigBase -o web/class-config-base.js && cd web && uglifyjs class-config-base.js --compress --mangle -o class-config-base.min.js --source-map url=i\"'class-config-base.min.js.map'\"", | ||
"web:build": "browserify index.js --standalone ClassConfig -o web/class-config-base.js && cd web && uglifyjs class-config-base.js --compress --mangle -o class-config-base.min.js --source-map url=i\"'class-config-base.min.js.map'\"", | ||
"chrome:install": "npm i --no-save mocha-chrome", | ||
"chrome:test": "mocha-chrome test/web/browser-test.html", | ||
"build": "npm run lint && npm run coverage && npm run web:build && node build/webify.js" | ||
"build": "npm run lint && npm run coverage && npm run web:build && node tool/mktest.js" | ||
}, | ||
@@ -41,14 +42,14 @@ "repository": { | ||
"browserify": "^16.2.2", | ||
"chai": "^3.5.0", | ||
"coveralls": "^3.0.1", | ||
"eslint": "^4.19.1", | ||
"mocha": "^3.2.0", | ||
"nyc": "^11.7.2", | ||
"chai": "^4.1.2", | ||
"coveralls": "^3.0.2", | ||
"eslint": "^5.0.1", | ||
"mocha": "^5.2.0", | ||
"nyc": "^12.0.2", | ||
"uglify-es": "^3.3.9" | ||
}, | ||
"dependencies": { | ||
"copy-props": "^2.0.0", | ||
"copy-props": "^2.0.4", | ||
"default-val": "^0.1.5", | ||
"instance-stringer": "^0.1.0" | ||
"instance-stringer": "^1.0.0" | ||
} | ||
} |
398
README.md
@@ -8,3 +8,3 @@ # [class-config-base][repo-url] [![NPM][npm-img]][npm-url] [![MIT License][mit-img]][mit-url] [![Build Status][travis-img]][travis-url] [![Build Status][appveyor-img]][appveyor-url] [![Coverage Status][coverage-img]][coverage-url] | ||
``` | ||
npm install class-config-base --save | ||
npm install class-config-base | ||
``` | ||
@@ -17,3 +17,3 @@ | ||
```js | ||
const ClassConfigBase = require('class-config-base') | ||
const ClassConfig = require('class-config-base') | ||
``` | ||
@@ -32,40 +32,47 @@ | ||
```js | ||
const defaultConfig = { a: '', b: { c: 0, d: false } } | ||
const defaultConfig = { a: '', b: { c: 0, d: 1 } } | ||
``` | ||
2. 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. | ||
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. | ||
```js | ||
class MyClassConfig extends ClassConfigBase { | ||
class MyClassConfig extends ClassConfig { | ||
constructor (initConfig) { | ||
super(initConfig, defaultConfig) | ||
} | ||
defineAccessors () { | ||
defineMorePrivates ($private) { | ||
$private.e = { f: [1, 2, 3] } | ||
} | ||
defineAccessors ($private, config) { | ||
return { | ||
'b.c': (parent, key) => ({ | ||
'b.c': { | ||
enumerable: true, | ||
get () { return parent[key] }, | ||
set (v) { parent[key] = Math.max(0, v) }, | ||
}) | ||
get () { return $private.b.c }, | ||
set (v) { $private.b.c = Math.max(0, v) }, | ||
} | ||
} | ||
} | ||
defineInterfaces () { | ||
defineInterfaces (config, instance) { | ||
return { | ||
myA: (config, myA) => ({ /* readonly property */ | ||
myA: { /* readonly property */ | ||
enumerable: true, | ||
set () {}, | ||
get () { return config.a }, | ||
}), | ||
myC: (config, myC) => ({ /* writable property */ | ||
}, | ||
myC: { /* writable property */ | ||
enumerable: true, | ||
set (v) { config.b.c = Math.max(v, 0) }, | ||
set (v) { config.b.c = v }, | ||
get () { return config.b.c }, | ||
}), | ||
myD: (config, myD) => ({ /* replaceable property */ | ||
}, | ||
myF: { /* replaceable property */ | ||
enumerable: true, | ||
configurable: true, | ||
set (value) { Object.defineProperty(this, myD, { | ||
set (value) { Object.defineProperty(instance, 'myF', { | ||
enumerable: true, | ||
@@ -76,57 +83,56 @@ configuable: true, | ||
}) }, | ||
get () { return config.b.d }, | ||
}), | ||
myE: (config, myE) => ({ /* method property */ | ||
get () { return config.e.f }, | ||
}, | ||
myG: { /* method property */ | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: (v) => { return config.b.c * v }, | ||
}), | ||
value: (v) => { return config.b.d * v }, | ||
}, | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
This module prepares some useful functions to define accessors/interfaces simply. | ||
This module provides some useful functions to define accessors/interfaces simply. | ||
By using these functions, the above example can be rewritten as follows: | ||
```js | ||
const { readonly, writable, replaceable, method } = ClassConfigBase | ||
class MyClassConfig extends ClassConfigBase { | ||
const { readonly, writable, replaceable, method } = ClassConfig | ||
class MyClassConfig extends ClassConfig { | ||
constructor (initConfig) { | ||
super(initConfig, defaultConfig) | ||
} | ||
defineAccessors () { | ||
defineMorePrivates ($private) { | ||
$private.e = { f: [1, 2, 3] } | ||
} | ||
defineAccessors ($private, config) { | ||
return { | ||
'b.c': (parent, key) => writable({ | ||
get: () => parent[key], | ||
set: v => { parent[key] = Math.max(0, v) }, | ||
}) | ||
'b.c': writable({ | ||
get () { return $private.b.c }, | ||
set (v) { $private.b.c = Math.max(0, v) }, | ||
}), | ||
} | ||
} | ||
defineInterfaces () { | ||
defineInterfaces (config, instance) { | ||
return { | ||
myA: config => readonly({ | ||
get: () => config.a, | ||
myA: readonly({ get: () => config.a }), | ||
myC: writable({ | ||
set: v => { config.b.c = v }, | ||
get: () => config.b.c, | ||
}), | ||
myC: config => writable({ | ||
set (v) { config.b.c = Math.max(v, 0) }, | ||
get () { return config.b.c }, | ||
}), | ||
myD: config => replaceable({ | ||
get: () => config.b.d, | ||
}), | ||
myE: config => method(v => config.b.c * v), | ||
myF: replaceable({ get: () => config.e.f }), | ||
myG: method((v) => { return config.b.d * v }), | ||
} | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
3. Define the interfacial class with the class config. | ||
```js | ||
@@ -139,9 +145,37 @@ class MyClass { | ||
``` | ||
The interfaces of interfacial class can be also defined by following way: | ||
```js | ||
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. | ||
```js | ||
const myCfg = new MyClassConfig({ a: 'Foo', b: { c: 123, d: true } }) | ||
const myObj = new MyClass(myCfg) | ||
console.log(myObj.toString()) // [object MyClass] | ||
@@ -151,22 +185,16 @@ console.log(Object.prototype.toString.call(myObj)) // [object MyClass] | ||
console.log(myObj.myC) // 123 | ||
console.log(myObj.myD) // true | ||
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 | ||
console.log(myObj.myE(2)) // 1998 | ||
myObj.myC = -888 | ||
console.log(myObj.myC) // 0 | ||
myObj.myD = false | ||
console.log(myObj.myD) // false | ||
myObj.myD = 123 | ||
console.log(myObj.myD) // 123 | ||
myObj.myF = 123 | ||
console.log(myObj.myF) // 123 | ||
``` | ||
5. A property value, even if it is read-only, can be updated with the class config object. | ||
5. A property value, even if it is read-only or hidden, can be updated with the class config object. | ||
@@ -176,21 +204,21 @@ ```js | ||
myCfg.b.c = 666 | ||
myCfg.b.d = 888 | ||
console.log(myObj.myA) // 'Buz' | ||
console.log(myObj.myC) // 666 | ||
console.log(myObj.myE(-4)) // -2664 | ||
console.log(myObj.myG(2)) // 1776 | ||
``` | ||
6. A mapping between a configuration class instance and a interfacial class instance can be managed by `ClassConfigManager` object. | ||
6. A mapping between a config class instance and a interfacial class instance can be managed by `ClassConfig.Manager` object. | ||
```js | ||
const { ClassConfigManager } = ClassConfigBase | ||
const manager = new ClassConfigManager() // Create a manager | ||
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 | ||
@@ -201,8 +229,12 @@ ``` | ||
### <u>ClassConfigBase(initConfig, defaultConfig)</u> | ||
### <u>class ClassConfig</u> | ||
Is a constructor to constructs a configuration class instance. | ||
Is a class to configure the target class instance from hiding place. | ||
#### <u>.constructor (initConfig, defaultConfig) => ClassConfig</u> | ||
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 property in *initConfig* is different from a corresponding property in *defaultConfig*, the property value in *initConfig* is ignored. | ||
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. | ||
@@ -216,76 +248,60 @@ **Parameters:** | ||
### <u>defineAccessors() : object</u> | ||
**Returns:** | ||
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`. | ||
A `ClassConfig` object. | ||
This method is used to override accessors of the config class. | ||
#### <u>.configure (instance, descriptors) => Void</u> | ||
**Returns:** | ||
Configures the interfaces of the target class instance in its constructor. | ||
An object which maps between property key chains and functions to get property descriptors of the config class. | ||
**Parameters:** | ||
The format of an entry in the returned object is as follows: | ||
| Parameter | Type | Description | | ||
|:--------------|:------:|:-----------------------------------| | ||
| *instance* | object | A class instance to be configured. | | ||
| *descriptors* | object | A plain object which has descriptors of interfaces of the target class instance. | | ||
This entry is a function of which the arguments are *parent* and *key*. | ||
In the following example, *parent* equals to `.a.b`, and *key* equals to `'c'`. | ||
#### <u>.defineMorePrivates ($private) => Void</u> | ||
```js | ||
defineAccessors () { | ||
return { | ||
'a.b.c' : function (parent, key) { | ||
return { | ||
enumerable: true, | ||
get () { return parent[key] }, | ||
set (v) { parent[key] = v }, | ||
} | ||
}, | ||
... | ||
} | ||
} | ||
``` | ||
Defines more private data than private data defined in `defaultConfig`. | ||
### <u>defineInterfaces() : object</u> | ||
**Parameters:** | ||
Returns an object which maps between property names and functions which return property descriptors. A descriptor is a thing used by `Object.defineProperty`. | ||
| Parameter | Type | Description | | ||
|:--------------|:------:|:-----------------------------------| | ||
| *$private* | object | The root object to store private data of the config object. | | ||
This method defines the interfaces of the target class. | ||
#### <u>.defineAccessors ($private, config) => object</u> | ||
**Returns:** | ||
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`. | ||
An object which maps between property name and functions to get property descriptors of the target class. | ||
This method is used to override accessors of the config class. | ||
The format of an entry in the returned object is as follows: | ||
**Parameters:** | ||
``` | ||
defineInterfaces () { | ||
return { | ||
'ccc' : function (config, interfaceName) { | ||
return { | ||
enumerable: true, | ||
get () { return config.a.b.c }, | ||
set (v) { config.a.b.c = v }, | ||
} | ||
}, | ||
... | ||
} | ||
} | ||
``` | ||
| Parameter | Type | Description | | ||
|:--------------|:------:|:------------------------------------------| | ||
| *$private* | object | The root object to store private data of the config object. | | ||
| *config* | `ClassConfig` | This config object. | | ||
This entry is a function of which the arguments are *config* and *interfaceName*. | ||
In the above example, *interfaceName* equals to `'ccc'`. | ||
**Returns:** | ||
### <u>configure(instance) : void</u> | ||
A nested plain object which contains property descriptors of accessors of this config object. | ||
Configures the interfaces of the target class instance in its constructor. | ||
#### <u>.defineInterfaces (config, instance) => Void</u> | ||
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:** | ||
| Parameter | Type | Description | | ||
|:------------|:------:|:-----------------------------------| | ||
| *instance* | object | A class instance to be configured. | | ||
| Parameter | Type | Description | | ||
|:--------------|:------:|:-----------------------------------| | ||
| *config* | `ClassConfig` | This config object. | | ||
| *instance* | object | The instance of the interfacial class configured by this config object. | | ||
#### <u>[static] .readonly ({ getter [, enumerable ] }) => object</u> | ||
### <u>*static* readonly({ get, enumerable = true }) : object</u> | ||
Returns a readonly property descriptor. | ||
@@ -295,6 +311,6 @@ | ||
| Parameter | Type | Description | | ||
|:-----------|:------:|:-----------------------------------------| | ||
| *get* |function| A getter for this property. | | ||
|*enumerable*|boolean | A flag to show this property during enumeration of the properties. | | ||
| Parameter | Type | Description | | ||
|:-------------|:--------:|:-----------------------------------------| | ||
| *getter* | function | A getter for this property. | | ||
| *enumerable* | boolean | A flag to show this property during enumeration of the properties. | | ||
@@ -305,13 +321,12 @@ **Return:** | ||
#### <u>[static] .writable ({ getter, setter, [, enumerable ] [, configurable ] }) => object</u> | ||
### <u>*static* writable({ get, set, enumerable = true, configurable = false }) => object</u> | ||
Returns a writable property descriptor. | ||
| Parameter | Type | Descriptor | | ||
|:-------------|:------:|:------------------------------------------| | ||
| *get* |function| A getter for this property. | | ||
| *set* |function| A setter for this property. | | ||
| *enumerable* |boolean | A flag to show this property during enumeration of the properties. | | ||
|*configurable*|boolean | A flag to change or delete this property. | | ||
| Parameter | Type | Descriptor | | ||
|:-------------|:--------:|:------------------------------------------| | ||
| *getter* | function | A getter for this property. | | ||
| *setter* | function | A setter for this property. | | ||
| *enumerable* | boolean | A flag to show this property during enumeration of the properties. | | ||
|*configurable*| boolean | A flag to change or delete this property. | | ||
@@ -322,5 +337,4 @@ **Return:** | ||
#### <u>[static] .replaceable ({ getter [, enumerable ] }) => object</u> | ||
### <u>*static* replaceable({ get, enumerable = true }) : object</u> | ||
Returns a replaceable property descriptor. | ||
@@ -330,6 +344,6 @@ | ||
| Parameter | Type | Description | | ||
|:-------------|:------:|:-----------------------------------------| | ||
| *get* |function| A getter for this property. | | ||
| *enumerable* |boolean | A flag to show this property during enumeration of the properties. | | ||
| Parameter | Type | Description | | ||
|:-------------|:--------:|:-----------------------------------------| | ||
| *get* | function | A getter for this property. | | ||
| *enumerable* | boolean | A flag to show this property during enumeration of the properties. | | ||
@@ -340,5 +354,4 @@ **Return:** | ||
#### <u>[static] .method (fn) : object</u> | ||
### <u>*static* method(fn) : object</u> | ||
Returns a property descriptor for a method. | ||
@@ -348,5 +361,5 @@ | ||
| Parameter | Type | Description | | ||
|:-------------|:------:|:-----------------------------------------| | ||
| *fn* |function| A method function for this property. | | ||
| Parameter | Type | Description | | ||
|:-------------|:--------:|:-----------------------------------------| | ||
| *fn* | function | A method function for this property. | | ||
@@ -357,5 +370,66 @@ **Return:** | ||
### <u>class ClassConfig.Manager</u> | ||
Is a manager class which has mappings of a config object and an object configured by it. | ||
#### <u>.constructor () => ClassConfig.Manager</u> | ||
Creates an instance of this class. | ||
**Returns:** | ||
A `ClassConfig.Manager` object. | ||
#### <u>.set (object, config) => Void</u> | ||
Sets a mapping of a config object and an object configured by it. | ||
**Parameters:** | ||
| Parameter | Type | Description | | ||
|:--------------|:----------:|:--------------------------------------------| | ||
| *object* | object | The object configured by the config object. | | ||
| *config* | `ClassConfig` | The config object. | | ||
#### <u>.delete (objectOrConfig) => Void</u> | ||
Deletes a mapping of a config object and an object configured by it. | ||
**Parameters:** | ||
| Parameter | Type | Description | | ||
|:-----------------|:-------------------------:|:---------------------------| | ||
| *objectOrConfig* | object |`ClassConfig`| The object or config object to be deleted its mapping from this manager object. | | ||
#### <u>.getConfig (object) => ClassConfig</u> | ||
Gets a config object corresponding to the specified object. | ||
**Parameters:** | ||
| Parameter | Type | Description | | ||
|:--------------|:------:|:------------------------------------------------| | ||
| *object* | object | The object registered with this manager object. | | ||
**Returns:** | ||
The config object corresponding to the specified object. | ||
#### <u>.getObject (config) => object</u> | ||
Get an object corresponding to the specified config object. | ||
**Parameters:** | ||
| Parameter | Type | Description | | ||
|:--------------|:-------------:|:------------------------------------------| | ||
| *config* | `ClassConfig` | The config object registered with this manager object. | | ||
**Returns:** | ||
The object corresponding to the specified config object. | ||
## License | ||
Copyright (C) 2017 Takayuki Sato | ||
Copyright (C) 2017-2018 Takayuki Sato | ||
@@ -366,3 +440,3 @@ This program is free software under [MIT][mit-url] License. | ||
[repo-url]: https://github.com/sttk/class-config-base/ | ||
[npm-img]: https://img.shields.io/badge/npm-v0.3.1-blue.svg | ||
[npm-img]: https://img.shields.io/badge/npm-v1.0.0-blue.svg | ||
[npm-url]: https://www.npmjs.org/package/class-config-base/ | ||
@@ -369,0 +443,0 @@ [mit-img]: https://img.shields.io/badge/license-MIT-green.svg |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
59477
9
844
434
4
+ Addedinstance-stringer@1.0.0(transitive)
- Removedinstance-stringer@0.1.2(transitive)
Updatedcopy-props@^2.0.4
Updatedinstance-stringer@^1.0.0