diff --git a/lib/node-iniparser.js b/lib/node-iniparser.js index 9ce6b3c..0c4734f 100644 --- a/lib/node-iniparser.js +++ b/lib/node-iniparser.js @@ -21,7 +21,7 @@ var regex = { * @param: {Function} callback, the function that will be called when parsing is done * @return: none */ -module.exports.parse = function(file, callback){ +exports.parse = function(file, callback){ if(!callback){ return; } @@ -34,7 +34,7 @@ module.exports.parse = function(file, callback){ }); }; -module.exports.parseSync = function(file){ +exports.parseSync = function(file){ return parse(fs.readFileSync(file, 'utf8')); }; @@ -63,4 +63,21 @@ function parse(data){ return value; } -module.exports.parseString = parse; +exports.parseString = parse; + + +function safe (val) { + return (val + "").replace(/[\n\r]+/g, " "); +} + +exports.stringify = function (obj) { + var ini = []; + for (var section in obj) if (section !== "-") { + ini.push("[" + safe(section) + "]\n"); + for (var key in obj[section]) { + ini.push("\t" + safe(key)); + ini.push((obj[section][key] === true) ? "\n" : " = " + safe(obj[section][key]) + "\n"); + } + } + return ini.join(""); +};