Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Open
Show file tree
Hide file tree
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
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,42 @@ To parse a string in .ini format:
var iniparser = require('iniparser');
var config = iniparser.parseString('foo=bar');
</pre>

## 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:
<pre>
var iniparser = require('iniparser');
var options = {
treatEmptyStringsAsNull: true
}
iniparser.parse('./config.ini', options, function(err,data){
var version = data.version;
});
</pre>

To parse a .ini file sync with options:
<pre>
var iniparser = require('iniparser');
var options = {
treatEmptyStringsAsNull: true
}
var config = iniparser.parseSync('./config.ini', options);
</pre>

To parse a string in .ini format with options:
<pre>
var iniparser = require('iniparser');
var options = {
treatEmptyStringsAsNull: true
}
var config = iniparser.parseString('foo=bar', options);
</pre>

## Installation
npm:

Expand Down
89 changes: 50 additions & 39 deletions lib/node-iniparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
3 changes: 2 additions & 1 deletion test/files/test.ini
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
foo=bar
var_with_space_at_end = bar
var_with_empty_value =
[worlds]
earth=awesome
a.b=c

[section2]
;test=worth
there_is=a space in here with = and trailing tab
bar=foo
bar=foo
8 changes: 7 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
};