Skip to content

Commit

Permalink
fix(util): update ini parsing (#4125)
Browse files Browse the repository at this point in the history
* fix(util): update ini parsing

* fix(util): use indexOf instead of split

* fix(util): require non-empty for ini parse

* fix(util): update section parse to match v3
  • Loading branch information
kuhe authored Jun 14, 2022
1 parent ef34124 commit 64eb16f
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,20 +215,28 @@ var util = {
parse: function string(ini) {
var currentSection, map = {};
util.arrayEach(ini.split(/\r?\n/), function(line) {
line = line.split(/(^|\s)[;#]/)[0]; // remove comments
var section = line.match(/^\s*\[([^\[\]]+)\]\s*$/);
if (section) {
currentSection = section[1];
line = line.split(/(^|\s)[;#]/)[0].trim(); // remove comments and trim
var isSection = line[0] === '[' && line[line.length - 1] === ']';
if (isSection) {
currentSection = line.substring(1, line.length - 1);
if (currentSection === '__proto__' || currentSection.split(/\s/)[1] === '__proto__') {
throw util.error(
new Error('Cannot load profile name \'' + currentSection + '\' from shared ini file.')
);
}
} else if (currentSection) {
var item = line.match(/^\s*(.+?)\s*=\s*(.+?)\s*$/);
if (item) {
var indexOfEqualsSign = line.indexOf('=');
var start = 0;
var end = line.length - 1;
var isAssignment =
indexOfEqualsSign !== -1 && indexOfEqualsSign !== start && indexOfEqualsSign !== end;

if (isAssignment) {
var name = line.substring(0, indexOfEqualsSign).trim();
var value = line.substring(indexOfEqualsSign + 1).trim();

map[currentSection] = map[currentSection] || {};
map[currentSection][item[1]] = item[2];
map[currentSection][name] = value;
}
}
});
Expand Down

0 comments on commit 64eb16f

Please sign in to comment.