Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

.stringify(object) #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions lib/node-iniparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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'));
};

Expand Down Expand Up @@ -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("");
};