From 7191a865b16246ce139c7422e5a20b174aec51bc Mon Sep 17 00:00:00 2001 From: Dan Tillberg Date: Wed, 9 Nov 2011 14:38:18 -0500 Subject: [PATCH] Adding stringify --- lib/node-iniparser.js | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) 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(""); +};