diff --git a/README.md b/README.md index dba5aba..28504e7 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,42 @@ To parse a string in .ini format: var iniparser = require('iniparser'); var config = iniparser.parseString('foo=bar'); + +## Options +The following options can be passed in as the second parameter to all 3 parse methods: + +`treatEmptyStringsAsNull: boolean - Stores a setting as a null value if an empty string is detected as the value.` + + +To parse a .ini file async with options: +
+var iniparser = require('iniparser');
+var options = {
+    treatEmptyStringsAsNull: true
+}
+iniparser.parse('./config.ini', options, function(err,data){
+	var version = data.version;
+});
+
+ +To parse a .ini file sync with options: +
+var iniparser = require('iniparser');
+var options = {
+    treatEmptyStringsAsNull: true
+}
+var config = iniparser.parseSync('./config.ini', options);
+
+ +To parse a string in .ini format with options: +
+var iniparser = require('iniparser');
+var options = {
+    treatEmptyStringsAsNull: true
+}
+var config = iniparser.parseString('foo=bar', options);
+
+ ## Installation npm: diff --git a/lib/node-iniparser.js b/lib/node-iniparser.js index 93397a3..55b54b6 100644 --- a/lib/node-iniparser.js +++ b/lib/node-iniparser.js @@ -10,57 +10,68 @@ var fs = require('fs'); * comment: ;this is a comment */ var regex = { - section: /^\s*\[\s*([^\]]*)\s*\]\s*$/, - param: /^\s*([\w\.\-\_]+)\s*=\s*(.*?)\s*$/, - comment: /^\s*;.*$/ + section: /^\s*\[\s*([^\]]*)\s*\]\s*$/, + param: /^\s*([\w\.\-\_]+)\s*=\s*(.*?)\s*$/, + comment: /^\s*;.*$/ }; /* * parses a .ini file * @param: {String} file, the location of the .ini file + * @param: {Object} options, the options to run the parse with * @param: {Function} callback, the function that will be called when parsing is done * @return: none */ -module.exports.parse = function(file, callback){ - if(!callback){ - return; - } - fs.readFile(file, 'utf8', function(err, data){ - if(err){ - callback(err); - }else{ - callback(null, parse(data)); - } - }); +module.exports.parse = function(file, options, callback){ + if(typeof(options) == 'function' && !callback) { + //backwards compatibility with non-optioned method + callback = options; + options = null; + } + if(!callback){ + return; + } + fs.readFile(file, 'utf8', function(err, data){ + if(err){ + callback(err); + }else{ + callback(null, parse(data, options)); + } + }); }; -module.exports.parseSync = function(file){ - return parse(fs.readFileSync(file, 'utf8')); +module.exports.parseSync = function(file, options){ + return parse(fs.readFileSync(file, 'utf8'), options); }; -function parse(data){ - var value = {}; - var lines = data.split(/\r\n|\r|\n/); - var section = null; - lines.forEach(function(line){ - if(regex.comment.test(line)){ - return; - }else if(regex.param.test(line)){ - var match = line.match(regex.param); - if(section){ - value[section][match[1]] = match[2]; - }else{ - value[match[1]] = match[2]; - } - }else if(regex.section.test(line)){ - var match = line.match(regex.section); - value[match[1]] = {}; - section = match[1]; - }else if(line.length == 0 && section){ - section = null; - }; - }); - return value; +function parse(data, options){ + var parseOptions = options ? options : {}; + var value = {}; + var lines = data.split(/\r\n|\r|\n/); + var section = null; + lines.forEach(function(line){ + if(regex.comment.test(line)){ + return; + }else if(regex.param.test(line)){ + var match = line.match(regex.param); + var lineValue = match[2]; + if(lineValue != null && parseOptions.treatEmptyStringsAsNull === true && !lineValue.trim()) { + lineValue = null; + } + if(section){ + value[section][match[1]] = lineValue; + }else{ + value[match[1]] = lineValue; + } + }else if(regex.section.test(line)){ + var match = line.match(regex.section); + value[match[1]] = {}; + section = match[1]; + }else if(line.length == 0 && section){ + section = null; + }; + }); + return value; } module.exports.parseString = parse; diff --git a/test/files/test.ini b/test/files/test.ini index a8a1e95..b660b93 100644 --- a/test/files/test.ini +++ b/test/files/test.ini @@ -1,5 +1,6 @@ foo=bar var_with_space_at_end = bar +var_with_empty_value = [worlds] earth=awesome a.b=c @@ -7,4 +8,4 @@ a.b=c [section2] ;test=worth there_is=a space in here with = and trailing tab -bar=foo +bar=foo \ No newline at end of file diff --git a/test/test.js b/test/test.js index 0e56f88..2a349f8 100644 --- a/test/test.js +++ b/test/test.js @@ -60,5 +60,11 @@ module.exports = { 'variable with space in value': function(){ var config = iniparser.parseSync('./files/test.ini'); assert.equal(config.section2.there_is, "a space in here with = and trailing tab"); - } + }, + 'empty string as null option': function() { + var config = iniparser.parse('./files/test.ini', {treatEmptyStringsAsNull: true}, function(err, config) { + assert.equal(err, null); + assert.equal(config.var_with_empty_value, null); + }); + } };