-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.js
325 lines (288 loc) · 9.57 KB
/
settings.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
config parameters manager
*/
function Settings(logger) {
if (!(this instanceof Settings)) {
return new Settings();
}
if (!logger) {
if (console == undefined) {
logger = {};
} else {
logger = console;
}
}
if (!logger.log || typeof logger.log !== "function") {
logger.log = function () {};
}
["debug", "info", "warn"].forEach(function (method) {
if (!logger[method] || typeof logger[method] !== "function") {
logger[method] = logger.log;
}
});
if (!logger.error || typeof logger.error !== "function") {
logger.error = function (message) {
throw message;
};
}
// settings parameters
var parameters = {};
// add a new config parameter (or multiple at once)
this.add = function (params) {
if (!params) {
logger.warn("invaid argument");
return;
}
if(Array.isArray(params)) {
var i;
for(i=0; i< params.length; i++) {
this.add(params[i]);
}
return;
} else if (typeof params !== "object") {
logger.warn("invaid argument");
return;
}
var name = params.name,
defaultValue = params.defaultValue,
validator = params.validator,
validationMoment = params.validationMoment,
changeCallback = params.onChange,
locked = params.locked,
help = params.help;
if (!name || typeof name !== "string") {
logger.warn("a config parameter name is required and should be a string");
return;
}
if (parameters.hasOwnProperty(name)) {
logger.warn(name + " config parameter already exists, maybe you want to call Set function instead");
return;
}
if (help === undefined || help === null) {
help = "";
} else if (typeof help !== "string") {
logger.warn(name + " help property is set to empty string because invalid argument is specified");
help = "";
}
var validateFun;
if (validator == undefined) {
validateFun = function () {
return true;
};
} else if (typeof validator === "function") {
validateFun = validator;
} else if (typeof validator === "string" || Array.isArray(validator)) {
var arr = [];
if (typeof validator === "string") {
arr.push(validator);
} else {
arr = validator;
}
validateFun = function (value) {
var i;
for (i = 0; i < arr.length; i++) {
if (typeof value === arr[i]) {
return true;
}
}
return false;
};
} else if (typeof validator !== "function") {
logger.warn(name + " validator property is set to null because invalid argument is specified");
validator = function () {
return true;
};
}
if (changeCallback == undefined) {
changeCallback = null;
} else if (typeof changeCallback !== "function") {
logger.warn(name + " changeCallback property is set to null because invalid argument is specified");
changeCallback = null;
}
if (validationMoment === undefined) {
validationMoment = Settings.validationMoment.DEFERRED;
} else if (validationMoment !== Settings.validationMoment.DEFERRED && validationMoment !== Settings.validationMoment.IMMEDIATE && validationMoment !== Settings.validationMoment.ONDEMAND) {
logger.warn(name + " validationMoment property is set to DEFERRED because invalid argument is specified");
validationMoment = Settings.validationMoment.DEFERRED;
}
if (locked === undefined) {
locked = false;
} else if (typeof locked !== "boolean") {
logger.warn(name + " locked property is set to false because invalid argument is specified");
locked = false;
}
// undefined is not allowed as a config parameter value (gonna explain why in docs)
if (defaultValue === undefined) {
defaultValue = null;
}
parameters[name] = {
"defaultValue": defaultValue,
"pendingValue": undefined, // used in deferred validation to hold the value waiting for check.
"validValue": defaultValue,
"validateFun": validateFun,
"changeCallback": changeCallback,
"validationMoment": validationMoment,
"locked": locked,
"help": help
};
return this;
};
// set a value to a config parameter
this.set = function (name, value) {
if (name === undefined || value === undefined) {
logger.log("parameter name and value are required");
return;
}
if (!parameters.hasOwnProperty(name)) {
logger.warn(name + " config parameter is undefined");
return;
}
var sett = parameters[name];
// if the same value is pending validation or already set, this call will affect nothing
if (sett.pendingValue === value || (sett.validValue === value && sett.pendingValue === undefined)) {
return;
} else if (value === sett.validValue && sett.pendingValue !== undefined && !sett.locked) { //cancel the pending operation
sett.pendingValue = undefined;
return;
}
if (sett.locked) {
logger.warn(name + " config parameter is locked, you cannot modify it");
return;
}
if (sett.validationMoment === Settings.validationMoment.IMMEDIATE || (sett.validationMoment === Settings.validationMoment.DEFERRED && sett.changeCallback != null)) {
if (sett.validateFun(value) || value === sett.defaultValue) {
sett.validValue = value;
if (sett.changeCallback) {
sett.changeCallback(value);
}
} else {
logger.warn("invalid value for " + name + " config parameter specified");
return;
}
} else {
sett.pendingValue = value;
}
};
// get value of a config parameter
this.get = function (name) {
if (!parameters.hasOwnProperty(name)) {
logger.warn(name + " config parameter is undefined");
return;
}
var sett = parameters[name];
if (sett.validationMoment === Settings.validationMoment.ONDEMAND || (sett.validationMoment === Settings.validationMoment.DEFERRED && sett.changeCallback == null)) {
if (sett.pendingValue !== undefined) {
if (sett.validateFun(sett.pendingValue) || sett.pendingValue === sett.defaultValue) {
sett.validValue = sett.pendingValue;
if (sett.changeCallback) {
sett.changeCallback(sett.validValue);
}
}
sett.pendingValue = undefined;
}
}
// return a clone to the object so we can control object modification
return __clone(sett.validValue);
};
// locking a config parameter
this.lock = function (name) {
if (!parameters.hasOwnProperty(name)) {
logger.warn(name + " config parameter is undefined");
return;
}
var sett = parameters[name];
if (!sett.locked) {
sett.locked = true;
} else {
logger.warn(name + " config parameter is already locked");
return;
}
};
//reset a config parameter value to default one
this.reset = function (name) {
if (!parameters.hasOwnProperty(name)) {
logger.warn(name + " config parameter is undefined");
return;
}
this.set(name, parameters[name].defaultValue);
};
this.getHelp = function (name) {
if (!parameters.hasOwnProperty(name)) {
logger.warn(name + " config parameter is undefined");
return;
}
return parameters[name].help;
};
// faciliate pushing a item to a config parameter value that is instanceOf array
this.pushToArray = function (name, item) {
var currentValue = this.get(name);
if (currentValue === undefined) {
return;
}
if (Array.isArray(currentValue)) {
currentValue.push(item);
this.set(name, currentValue);
} else {
logger.warn(name + " config parameter value is not an array, you can't push items to");
}
};
// faciliate adding a property to a config parameter value that is instanceOf object
this.addToObject = function (name, key, value) {
var currentValue = this.get(name);
if (currentValue === undefined) {
return;
}
if (key != undefined) {
logger.warn("invalid property name : " + key);
return;
}
if (!(currentValue instanceof object)) {
logger.warn(name + " config parameter value is not an instance of object, you can't add properties to");
return;
}
currentValue[key] = value;
this.set(name, currentValue);
};
// support primitive types, object and array. User custom types are not sypported
function __clone(obj) {
// Handle the 3 simple types, and null or undefined
if (obj == null || "object" != typeof obj) {
return obj;
}
if (obj instanceof Array) {
var copy = [];
for (var i = 0, len = obj.length; i < len; i++) {
copy[i] = __clone(obj[i]);
}
return copy;
}
if (obj instanceof Object) {
var copy = {};
for (var attr in obj) {
if (obj.hasOwnProperty(attr)) copy[attr] = __clone(obj[attr]);
}
return copy;
}
throw new Error("Unable to copy obj! Its type isn't supported.");
}
function getParameter(name) {
if (!parameters.hasOwnProperty(name)) {
throw "Settings do not contain " + name;
}
return parameters[name];
}
//for testing and debugging purposes
this.getParameter = getParameter;
return this;
}
// when validation is done
Settings.validationMoment = {
// value is validated immediately when set.
IMMEDIATE: 0,
// deafault one, if an onChange function is present: DEFERRED = IMMEDIATE , else DEFERRED = ONDEMAND
DEFERRED: 1,
// validation is done only when config variable requested (be careful, onChange call will be deferred also)
ONDEMAND: 2
}
// for testing with node:
// module.exports.Settings = Settings;