-
Notifications
You must be signed in to change notification settings - Fork 6
/
config-ini.js
577 lines (527 loc) · 18.8 KB
/
config-ini.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/**
* Created by Erxin, Shang(Edwin) on 6/21/2016.
* JavaScript Configuration file(.ini) content parser, similar to python ConfigParser without I/O operations
* The license is under GPL-3.0
* Git repo:https://github.com/shangerxin/config-ini
* Author homepage: http://www.shangerxin.com
* Version, 1.6.1
*/
(function (exports) {
var error = Object.assign(new Error(), {
name: "ConfigIniParser Error",
message: "Parse config ini file error",
});
var errorNoSection = Object.assign(new Error(), {
name: "ConfigIniParser Error",
message: "The specify section not found",
});
var errorNoOption = Object.assign(new Error(), {
name: "ConfigIniParser Error",
message: "The specify option not found",
});
var errorDuplicateSectionError = Object.assign(new Error(), {
name: "ConfigIniParser Error",
message: "Found duplicated section in the given ini file",
});
var errorCallParseMultipleTimes = Object.assign(new Error(), {
name: "ConfigIniParser Error",
message: "Multiple call parse on the same parser instance",
});
var errorIncorrectArgumentType = Object.assign(new TypeError(), {
name: "ConfigIniParser Error",
message: "The argument type is not correct",
});
var DEFAULT_SECTION = "__DEFAULT_SECTION__";
var _sectionRegex = /^\s*\[\s*([^\]]+?)\s*\]\s*$/;
var _optionRegex = /\s*([^=:\s]+)\s*[=:]\s*(.*)\s*/;
var _commentRegex = /^\s*[#;].*/;
var _emptyRegex = /^\s*$/;
var SECTION_NAME_INDEX = 1;
var OPTION_NAME_INDEX = 1;
var OPTION_VALUE_INDEX = 2;
var NOT_FOUND = -1;
var DEFAULT_DELIMITER = "\n";
function _findSection(iniStructure, sectionName) {
var sections = iniStructure.sections;
for (var i = 0; i < sections.length; i++) {
var section = sections[i];
if (section.name == sectionName) {
return section;
}
}
}
function _findSectionIndex(iniStructure, sectionName) {
var sections = iniStructure.sections;
for (var i = 0; i < sections.length; i++) {
var section = sections[i];
if (section.name == sectionName) {
return i;
}
}
return NOT_FOUND;
}
function _findOption(section, optionName) {
var options = section.options;
for (var i = 0; i < options.length; i++) {
var option = options[i];
if (option.name == optionName) {
return option;
}
}
}
function _findOptionIndex(section, optionName) {
var options = section.options;
for (var i = 0; i < options.length; i++) {
var option = options[i];
if (option.name == optionName) {
return i;
}
}
return NOT_FOUND;
}
function _createSection(name) {
return {
name: name,
options: [],
};
}
function _createOption(name, value) {
return {
name: name,
value: value,
};
}
/*
* Create a ini file parser, the format of ini file could be found at
* https://en.wikipedia.org/wiki/INI_file
*
* Duplicated sections will cause exception, duplicated options will be
* ignored and only the first one will take effect.
*
* @constructor
* @param {string} delimiter, the line delimiter which is used to separate the lines
* @return {ConfigIniParser} a ConfigIniParser object
*/
var ConfigIniParser = function (delimiter) {
this.delimiter = delimiter ? delimiter : DEFAULT_DELIMITER;
this._calledParse = false;
/*
_init object structure
{
sections:[
{
name:string,
options:[
{
name:string,
value:value
},
]
},
]
}
*/
this._ini = {
sections: [],
};
this._ini.sections.push(_createSection(DEFAULT_SECTION));
};
/*
* Create a new section, if the section is already contain in the structure then
* a duplicated section exception will be thrown
* @param {string} sectionName a section name defined in ini [section name]
* @return {object} the created section object
*/
ConfigIniParser.prototype.addSection = function (sectionName) {
if (_findSection(this._ini, sectionName)) {
throw errorDuplicateSectionError;
} else {
var section = _createSection(sectionName);
this._ini.sections.push(section);
return this;
}
};
/*
* Get a specific option value
* @param {string} sectionName the name defined in ini `[section name]`. If null the default
* section will be used.
* @param {string} optionName the name defined in ini `option-name = option-value`
* @param {object} defaultValue optional default value to be used when the option does not exist. If it is not provided and the option value does not exist, then an exception is thrown.
* @return {string/object} the string value of the option or defaultValue
*/
ConfigIniParser.prototype.get = function (
sectionName,
optionName,
defaultValue
) {
var section = _findSection(
this._ini,
sectionName ? sectionName : DEFAULT_SECTION
);
if (section) {
var option = _findOption(section, optionName);
if (option) {
return option.value;
}
}
if (defaultValue === undefined) {
throw errorNoOption;
} else {
return defaultValue;
}
};
/*
* Get a option from the default section. This is a convenient method when get the
* option from the default section.
* @param {string} optionName the name defined in ini `option-name = option-value`
* @param {object} defaultValue optional default value to be used when the option does not exist. If it is not provided and the value does not exist, then an exception is thrown.
* @return {string/object} the string value of the option or defaultValue
*/
ConfigIniParser.prototype.getOptionFromDefaultSection = function (
optionName,
defaultValue
) {
return this.get(null, optionName, defaultValue);
};
/*
* Get the option from the section and convert the value to boolean, if the value
* is a number then return true if it is
* not equal to 0; if the value is string and which value is "true"/"false" then
* will be converted to bool, return true if it is "true";
* @param {string} sectionName the name defined in ini `[section name]`
* @param {string} optionName the name defined in ini `option-name = option-value`
* @return {boolean} indicate the value is true or false
*/
ConfigIniParser.prototype.getBoolean = function (sectionName, optionName) {
var value = this.get(
sectionName ? sectionName : DEFAULT_SECTION,
optionName
);
if (isNaN(value)) {
return String(value).toLowerCase() == "true";
} else {
return value != 0;
}
};
/*
* Get the option from the default section and convert the value to boolean, if the
* value is a number then return true if it is
* not equal to 0; if the value is string and which value is "true"/"false" then
* will be converted to bool, return true if it is "true";
* @param {string} optionName the name defined in ini `option-name = option-value`
* @return {boolean} indicate the value is true or false
*/
ConfigIniParser.prototype.getBooleanFromDefaultSection = function (
optionName
) {
return this.getBoolean(null, optionName);
};
/*
* Get the option from the section and convert the value to number
* @param {string} sectionName the name defined in ini `[section name]`
* @param {string} optionName the name defined in ini `option-name = option-value`
* @return {number/NaN} number or NaN
*/
ConfigIniParser.prototype.getNumber = function (sectionName, optionName) {
return +this.get(
sectionName ? sectionName : DEFAULT_SECTION,
optionName
);
};
/*
* Get the option from the default section and convert the value to number
* @param {string} optionName the name defined in ini `option-name = option-value`
* @return {number/NaN} number or NaN
*/
ConfigIniParser.prototype.getNumberFromDefaultSection = function (
optionName
) {
return this.getNumber(null, optionName);
};
/*
* Check a specify section is exist or not
* @return {boolean} to indicate the result
*/
ConfigIniParser.prototype.isHaveSection = function (sectionName) {
if (!sectionName) {
throw errorIncorrectArgumentType;
}
return !!_findSection(this._ini, sectionName);
};
/*
* Check an option is exist in a section or not.
* @param {string} sectionName the name of the section. If null a default section will
* be used
* @return {boolean} boolean
*/
ConfigIniParser.prototype.isHaveOption = function (
sectionName,
optionName
) {
var section = _findSection(
this._ini,
sectionName ? sectionName : DEFAULT_SECTION
);
if (section) {
var option = _findOption(section, optionName);
if (option) {
return true;
}
}
return false;
};
/*
* Check an option is exist in the default section or not.
* @return {boolean} boolean
*/
ConfigIniParser.prototype.isHaveOptionInDefaultSection = function (
optionName
) {
return this.isHaveOption(null, optionName);
};
/*
* Get key/value pair of the options in the specify section
* @param {string} sectionName a section name defined in ini [section name]. If sectionName
* is null then the default section will be used.
* @return {Array.} an array contain several sub arrays which are composed by optionName,
* optionValue. The returned array looks like [[optionName0, optionValue0], ...]
*/
ConfigIniParser.prototype.items = function (sectionName) {
var section = _findSection(
this._ini,
sectionName ? sectionName : DEFAULT_SECTION
);
var items = [];
for (var i = 0; i < section.options.length; i++) {
var option = section.options[i];
items.push([option.name, option.value]);
}
return items;
};
/*
* Get all the option names from the specify section. If the sectionName is null
* the default section will be used
* @param {string} sectionName a section name defined in ini [section name]
* @return {Array.} an string array contain all the option names
*/
ConfigIniParser.prototype.options = function (sectionName) {
var section = _findSection(
this._ini,
sectionName ? sectionName : DEFAULT_SECTION
);
if (section) {
var optionNames = [];
var options = section.options;
var option;
for (var i = 0; i < options.length; i++) {
option = options[i];
optionNames.push(option.name);
}
return optionNames;
} else {
throw errorNoSection;
}
};
/*
* Remove the specify option from the section if the option exist then remove it
* and return true else return false. If the sectionName null then the default
* section will be used.
*
* @param {string} sectionName a section name defined in ini [section name]
* @param {string} optionName
* @return, boolean
*/
ConfigIniParser.prototype.removeOption = function (
sectionName,
optionName
) {
var section = _findSection(
this._ini,
sectionName ? sectionName : DEFAULT_SECTION
);
if (section) {
var optionIndex = _findOptionIndex(section, optionName);
if (optionIndex != NOT_FOUND) {
section.options.splice(optionIndex, 1);
return true;
}
}
return false;
};
/*
* Remove the specify option from the default section if the option exist then remove it
* and return true else return false.
*
* @param {string} optionName
* @return, boolean
*/
ConfigIniParser.prototype.removeOptionFromDefaultSection = function (
optionName
) {
return this.removeOption(null, optionName);
};
/*
* Remove the specify section if the section exist then remove it
* and return true else return false. If null is passed. Then the
* default section will be removed
*
* @param {string} sectionName
* @return {boolean}
*/
ConfigIniParser.prototype.removeSection = function (sectionName) {
var sectionIndex = _findSectionIndex(
this._ini,
sectionName ? sectionName : DEFAULT_SECTION
);
if (sectionIndex != NOT_FOUND) {
this._ini.sections.splice(sectionIndex, 1);
if (sectionName === DEFAULT_SECTION) {
this._ini.sections.push(_createSection(DEFAULT_SECTION));
}
return true;
} else {
return false;
}
};
/*
* Get all the section names from the ini content
* @return {Array.} an string array
*/
ConfigIniParser.prototype.sections = function () {
var sectionNames = [];
var sections = this._ini.sections;
var section;
for (var i = 0; i < sections.length; i++) {
section = sections[i];
if (section.name != DEFAULT_SECTION) {
sectionNames.push(section.name);
}
}
return sectionNames;
};
/*
* Set a option value, if the option is not exist then it will be created and add
* to the section. If the section is not exist an errorNoSection will be thrown.
* If the section name is null then the option will be added into the default section
* @param {string} sectionName, string
* @param {string} optionName, string
* @param {string} value, a value should be able to converted to string
* @return {object} parser object itself
*/
ConfigIniParser.prototype.set = function (sectionName, optionName, value) {
var section = _findSection(
this._ini,
sectionName ? sectionName : DEFAULT_SECTION
);
var option;
if (section) {
option = _findOption(section, optionName);
if (option) {
option.value = value;
return this;
} else {
option = _createOption(optionName, value);
section.options.push(option);
return this;
}
} else {
throw errorNoSection;
}
};
/*
* Set a option value to the default section. if the option is not exist then
* it will be created and added to the default section.
* @param {string} optionName, string
* @param {string} value, a value should be able to converted to string
* @return {object} parser object itself
*/
ConfigIniParser.prototype.setOptionInDefaultSection = function (
optionName,
value
) {
return this.set(null, optionName, value);
};
/*
* Convert the configuration content to strings the line will the separate with the
* given line delimiter. A empty line will be added between each section
*
* @return {string} the content of configuration
*/
ConfigIniParser.prototype.stringify = function (delimiter) {
var lines = [];
var sections = this._ini.sections;
var currentSection;
var options;
var currentOption;
for (var i = 0; i < sections.length; i++) {
currentSection = sections[i];
if (currentSection.name != DEFAULT_SECTION) {
lines.push("[" + currentSection.name + "]");
}
options = currentSection.options;
for (var j = 0; j < options.length; j++) {
currentOption = options[j];
lines.push(currentOption.name + "=" + currentOption.value);
}
if (lines.length > 0) {
lines.push("");
}
}
return lines.join(delimiter ? delimiter : this.delimiter);
};
/*
* Parse a given ini content
* @param {string} iniContent, a string normally separated with \r\n or \n
* @return {ConfigIniParser} the parser instance itself
*/
ConfigIniParser.prototype.parse = function (iniContent) {
if (this._calledParse) {
throw errorCallParseMultipleTimes;
}
this._calledParse = true;
var lines = iniContent.split(this.delimiter);
var currentSection = _findSection(this._ini, DEFAULT_SECTION);
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
if (line.match(_commentRegex) || line.match(_emptyRegex)) {
continue;
}
var sectionInfo = line.match(_sectionRegex);
if (sectionInfo) {
var sectionName = sectionInfo[SECTION_NAME_INDEX];
if (_findSection(this._ini, sectionName)) {
throw errorDuplicateSectionError;
} else {
currentSection = _createSection(sectionName);
this._ini.sections.push(currentSection);
}
continue;
}
var optionInfo = line.match(_optionRegex);
if (optionInfo) {
var optionName = optionInfo[OPTION_NAME_INDEX];
var optionValue = optionInfo[OPTION_VALUE_INDEX];
var option = _createOption(optionName, optionValue);
currentSection.options.push(option);
continue;
}
throw error;
}
return this;
};
ConfigIniParser.Errors = {
Error: error,
ErrorNoSection: errorNoSection,
ErrorNoOption: errorNoOption,
ErrorDuplicateSectionError: errorDuplicateSectionError,
ErrorCallParseMultipleTimes: errorCallParseMultipleTimes,
ErrorIncorrectArgumentType: errorIncorrectArgumentType,
};
exports.ConfigIniParser = ConfigIniParser;
})(
typeof exports != "undefined"
? exports
: typeof window != "undefined"
? window
: {}
);