This repository was archived by the owner on Oct 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathINI.js
More file actions
487 lines (431 loc) · 11.4 KB
/
INI.js
File metadata and controls
487 lines (431 loc) · 11.4 KB
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
(function () {
function isNodeJS () { //Allow for module.exports to be used if is being required with Node.js
try{
if (module) {
return true;
} else {
return false;
}
}catch(e){
return false;
}
}
var _isnodejs = isNodeJS();
if (!_isnodejs) {
var module = { exports: null }; //Exports is the only value that is required.
}
function removeInvisibleCharsFromString(oldString) {
/*var newString = oldString;
var invisibleChars = ["\n", "\t", " "];
var i = 0;
while (invisibleChars.indexOf(newString[0]) > -1) {
newString = newString.slice(1, newString.length);
i += 1;
}
var i = oldString.length - 1;
while (invisibleChars.indexOf(newString[newString.length - 1]) > -1) {
newString = newString.slice(0, newString.length - 1);
i += 1;
}
return newString;*/
return oldString.trim(); //the same as above, but smaller code length.
}
function convertStringToProperValue(str) {
if (!isNaN(Number(str))) {
return Number(str);
} else {
if (str.toLowerCase() == "false") {
return false;
} else {
if (str.toLowerCase() == "true") {
return false;
} else {
if (str.toLowerCase() == "nan") {
return NaN;
} else {
return str;
}
}
}
}
}
function from(inidata, providedOps) {
if (typeof inidata !== "string") {
throw new Error("Provided INI string value is not a string.");
return;
}
var ops = {
convertValues: true, //Convert values of variables to numbers if they are an type of number.
ignoreComments: false, //Don't include comments in parsed if true.
ignoreNewLines: true, //Dont include new line type in parsed array if true.
throwErrors: true, //Throw errors rather then just returning them if true.
};
if (providedOps) {
for (var key of Object.keys(providedOps)) {
ops[key] = providedOps[key];
}
}
try {
var inistuff = inidata.replaceAll("\r", "\n");
} catch (e) {
var inistuff = inidata;
}
var iniStuffArray = inistuff.split("\n");
var parts = [];
var index = 0;
var tmp2 = "";
function removeComments(str) {
var splitComment = str.split(";");
if (!ops.ignoreComments) {
if (splitComment.length > -1) {
var comment = splitComment.slice(1,splitComment.length).join(";");
parts.push({
type:"comment",
comment:comment
});
}
}
return splitComment[0];
}
while (index < iniStuffArray.length) {
var inist = removeComments(iniStuffArray[index]);
if (removeInvisibleCharsFromString(inist).startsWith("[")) {
var i = 0;
while (i < inist.length) {
var char = inist[i];
if (char == "[") {
var label = "";
i += 1;
while (char !== "]" && i < inist.length) {
if (!(i < inist.length)) {
var error = new Error(
`Unable to parse INI (Line ${index + 1} Character ${
i + 1
}): could not find ending character.`
);
if (ops.throwErrors) {
throw error;
}
return error;
}
var char = inist[i];
if (char !== "]") {
label += char;
}
i += 1;
}
parts.push({
type: "section",
label: removeInvisibleCharsFromString(label),
});
}
i += 1;
}
} else {
if (inist.indexOf("=") > -1) {
var i = 0;
var equalSigns = 0;
while (i < inist) {
if (inist[i] == "=") {
equalSigns += 1;
}
i += 1;
}
if (equalSigns > 1) {
var error = new Error(
`Unable to parse INI (Line ${
index + 1
} Character 1): multiple characters of "=" detected.`
);
if (ops.throwErrors) {
throw error;
}
return error;
}
var things = inist.split("=");
var name = removeInvisibleCharsFromString(things[0]);
var value = removeInvisibleCharsFromString(things[1]);
if (ops.convertValues) {
value = convertStringToProperValue(value);
}
parts.push({
type: "variable",
unparsedName:things[0],
unparsedValue:things[1],
name: name,
value: value,
});
} else {
if (iniStuffArray[index] == "\n") {
if (!ops.ignoreNewLines) {
parts.push({
type: "newline",
});
}
}
}
}
index += 1;
}
return parts;
}
function to(parsed, slightlyPrettier) {
if (typeof parsed !== "object") {
throw new Error("Provided parsed INI is not array.");
return;
}
var output = "";
var sect = null;
for (var obj of parsed) {
if (obj.type == "comment") {
output += ";";
output += obj.comment;
output += "\n";
}
if (obj.type == "section") {
if (slightlyPrettier && sect) {
output += "\n";
}
sect = obj.label;
output += "[";
output += obj.label;
output += "]";
output += "\n";
}
if (obj.type == "variable") {
output += obj.name;
output += "=";
output += obj.value;
output += "\n";
}
if (obj.type == "newline") {
output += "\n";
}
}
if (output.endsWith("\n")) {
output = output.slice(0,output.length-1);
}
return output;
}
function readValue(parsed, name) {
var sect = "";
for (var obj of parsed) {
if (obj.type == "section") {
sect = obj.label;
}
if (obj.type == "variable") {
if (obj.name == name) {
return obj.value;
}
}
}
}
function readValueFromSection(parsed, name, section) {
var sect = "";
for (var obj of parsed) {
if (obj.type == "section") {
sect = obj.label;
}
if (obj.type == "variable") {
if (sect == section && obj.name == name) {
return obj.value;
}
}
}
}
function setValue(parsed, name, value) {
var sect = "";
for (var obj of parsed) {
if (obj.type == "section") {
sect = obj.label;
}
if (obj.type == "variable") {
if (obj.name == name) {
obj.value = value;
return;
}
}
}
}
function setValueFromSection(parsed, name, section, value) {
var sect = "";
for (var obj of parsed) {
if (obj.type == "section") {
sect = obj.label;
}
if (obj.type == "variable") {
if (sect == section && obj.name == name) {
obj.value = value;
return;
}
}
}
}
function getValuesFromName(parsed, name) {
var values = [];
var sect = "";
for (var obj of parsed) {
if (obj.type == "section") {
sect = obj.label;
}
if (obj.type == "variable") {
if (obj.name == name) {
values.push(obj);
}
}
}
return values;
}
function getValuesFromSection(parsed, section) {
var values = [];
var sect = "";
for (var obj of parsed) {
if (obj.type == "section") {
sect = obj.label;
}
if (obj.type == "variable") {
if (sect == section) {
values.push(obj);
}
}
}
return values;
}
function createSection(name) {
return {
type: "section",
label: name,
};
}
function createVariable(name, value) {
return {
type: "variable",
name: name,
value: value,
};
}
function createComment(comment) {
return {
type: "comment",
comment: " " + comment,
};
}
function toBranchesJSON(parsed) {
var sect = null;
var output = {};
for (var part of parsed) {
if (part.type == "section") {
sect = part.label;
output[sect] = {};
}
if (part.type == "variable") {
if (sect) {
output[sect][part.name] = part.value;
} else {
output[part.name] = part.value;
}
}
}
return output;
}
function fromBranchesJSON(branchesJSON) {
var array = [];
for (var name of Object.keys(branchesJSON)) {
var value = branchesJSON[name];
if (typeof value == "object") {
array.push(createSection(name));
for (var name2 of Object.keys(value)) {
var value2 = value[name2];
array.push(createVariable(name2, value2));
}
} else {
if (typeof value !== "undefined") {
array.push(createVariable(name, value));
}
}
}
return array;
}
class INIFile {
constructor (text) {
if (typeof text == "string") {
//Read from INI text.
this._iniObject = toBranchesJSON(from(text));
} else {
//Don't read from text, start from scratch.
this._iniObject = {};
}
}
getValue (name,section) {
var sect = this._iniObject[section];
if (typeof sect == "object") {
if (typeof sect[name] !== "undefined") {
return sect[name];
}
}
return null;
}
valueExists (name,section) {
var sect = this._iniObject[section];
if (typeof sect == "object") {
if (typeof sect[name] !== "undefined") {
return true;
}
}
return false;
}
setValue (name,section,value) {
//This also returns true or false if the value has been stored
if (typeof sect == "object" || typeof sect == "undefined") {
return false;
}
var sect = this._iniObject[section];
if (typeof sect == "object") {
sect[name] = value;
return true;
}
return false;
}
createSection (name) {
this._iniObject[name] = {};
}
deleteSection (name) {
this._iniObject[name] = undefined;
}
listSectionValues (section) {
if (typeof this._iniObject[section] == "object") {
return Object.keys(this._iniObject[section]);
} else {
return [];
}
}
stringify () {
return to(fromBranchesJSON(this._iniObject));
}
}
module.exports = {
//Import and export the INI file.
from: from,
to: to,
//Read INI data.
readValue: readValue,
readValueFromSection: readValueFromSection,
getValuesFromName: getValuesFromName,
getValuesFromSection: getValuesFromSection,
//Write INI data.
setValue: readValue,
setValueFromSection: readValueFromSection,
//These are for adding things to the INI file.
createSection: createSection,
createVariable: createVariable,
createComment: createComment,
//Get INI as a JSON object.
toBranchesJSON: toBranchesJSON,
fromBranchesJSON: fromBranchesJSON,
//Basically does the same thing as above, but as a constructor. (Easier to script for some people)
INIFile: INIFile
};
if (!_isnodejs) {
window.INI = module.exports; //No Node.js, so just globalize it.
}
})();