node/lib/ini.js

30 lines
644 B
JavaScript
Raw Normal View History

2010-03-06 08:12:31 +08:00
exports.parse = function(d) {
var ini = {'-':{}};
var section = '-';
var lines = d.split('\n');
for (var i=0; i<lines.length; i++) {
var line = lines[i].trim(),
rem = line.indexOf(";");
2010-03-06 08:12:31 +08:00
if (rem !== -1) line = line.substr(0, rem);
2010-03-06 08:12:31 +08:00
var re = /^\[([^\]]*)\]$|^([^=]+)(=(.*))?$/i;
var match = line.match(re);
2010-03-06 08:12:31 +08:00
if (match != null) {
if (match[1] != undefined) {
section = match[1].trim();
2010-03-06 08:12:31 +08:00
ini[section] = {};
} else {
var key = match[2].trim(),
value = (match[3]) ? (match[4] || "").trim() : true;
2010-03-06 08:12:31 +08:00
ini[section][key] = value;
}
}
}
return ini;
}