-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.js
168 lines (150 loc) · 4.17 KB
/
Config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* Requires
*/
import { Exception } from '../Error/Exception.js';
import { isPojo } from '../Object/isPojo.js';
import { mergeObject } from '../Object/mergeObject.js';
import { cloneObject } from '../Object/cloneObject.js';
import { strCreate } from '../Object/strCreate.js';
import { strAccess } from '../Object/strAccess.js';
/**
* Config exception
* @class
* @extends Exception
*/
class ConfigException extends Exception {}
/**
* Config
* @class
*/
export class Config {
/**
* Clone object recursive
* @public
* @param {Object} data - Data to clone
* @return {Object|Array} - Cloned object
*/
static clone( data ) {
return cloneObject( data, true );
}
/**
* Merge object recursive
* @public
* @param {Object} data - Data to merge
* @param {Object} target - Target to merge to
* @param {boolean} extend - Set true to add undefined|null properties
* @return {void}
*/
static merge( data, target, extend = false ) {
mergeObject( target, data, extend, true, true );
}
/**
* Build defaults from inheritance
* @public
* @param {Array} extended - Inheritance array
* @return {null|Object} - Merged configs
*/
static extendInheritance( extended ) {
if ( !extended.length ) return null;
const config = extended.shift();
for ( let i = 0; i < extended.length; i++ ) {
if ( !isPojo( extended[ i ] ) ) {
throw new ConfigException( 'Argument extended must contain only plain Objects' );
}
this.merge( extended[ i ], config, true );
}
return config;
}
/**
* Config defaults
* @private
* @property
* @type {Object}
*/
#defaults = {};
/**
* Config data
* @private
* @property
* @type {Object}
*/
#data = {};
/**
* Constructor
* @constructor
* @param {Object} defaults - Default config values object
* @param {Array<Object>} extended - Default config extension array for inheritance
*/
constructor( defaults = {}, extended = [] ) {
// Check defaults
if ( !isPojo( defaults ) ) {
throw new ConfigException( 'Argument defaults must be a plain Object' );
}
this.#defaults = defaults;
// Check extended
if ( !( extended instanceof Array ) ) {
throw new ConfigException( 'Argument extended must be an Array' );
}
// Update default config with extensions
const inherited_defaults = this.constructor.extendInheritance( extended );
if ( inherited_defaults ) this.constructor.merge( inherited_defaults, this.#defaults, true );
// Setup active config data
this.#data = this.constructor.clone( this.#defaults );
}
/**
* Defaults getter
* @public
* @return {Object} - Defaults data object
*/
get defaults() {
return this.constructor.clone( this.#defaults );
}
/**
* Data getter
* @public
* @return {Object} - Config data object
*/
get data() {
return this.constructor.clone( this.#data );
}
/**
* Direct config access
* @public
* @return {Object} - Config data object
*/
get exposed() {
return this.#data;
}
/**
* Get config value
* @public
* @param {string} name - Value name
* @return {*|null} - Config value
*/
get( name ) {
return strAccess( name, this.#data );
}
/**
* Set config value
* @public
* @param {string} name - Value name
* @param {*} value - Value to set
* @return {void}
*/
set( name, value ) {
strCreate( name, value, this.#data, true );
}
/**
* Merge data into config
* @public
* @param {Object} data - Data object to merge
* @param {boolean} extend - Set true to add undefined|null properties
* @return {void}
*/
merge( data, extend = false ) {
if ( !isPojo( data ) ) {
throw new ConfigException( 'Argument data must be a plain Object' );
}
this.constructor.merge( data, this.#data, extend );
}
}