├── test ├── files │ └── test.ini └── test.js ├── package.json ├── lib └── node-iniparser.js └── README.md /test/files/test.ini: -------------------------------------------------------------------------------- 1 | foo=bar 2 | var_with_space_at_end = bar 3 | [worlds] 4 | earth=awesome 5 | a.b=c 6 | 7 | [section2] 8 | ;test=worth 9 | there_is=a space in here with = and trailing tab 10 | bar=foo 11 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "iniparser", 3 | "version": "1.0.4", 4 | "description": "a simple .ini parser", 5 | "author": "Jordy van Gelder ", 6 | "main": "./lib/node-iniparser", 7 | "directories": { 8 | "lib": "./lib" 9 | }, 10 | "repository" : { 11 | "type" : "git", 12 | "url" : "https://github.com/shockie/node-iniparser.git" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /lib/node-iniparser.js: -------------------------------------------------------------------------------- 1 | /* 2 | * get the file handler 3 | */ 4 | var fs = require('fs'); 5 | 6 | /* 7 | * define the possible values: 8 | * section: [section] 9 | * param: key=value 10 | * comment: ;this is a comment 11 | */ 12 | var regex = { 13 | section: /^\s*\[\s*([^\]]*)\s*\]\s*$/, 14 | param: /^\s*([\w\.\-\_]+)\s*=\s*(.*?)\s*$/, 15 | comment: /^\s*;.*$/ 16 | }; 17 | 18 | /* 19 | * parses a .ini file 20 | * @param: {String} file, the location of the .ini file 21 | * @param: {Function} callback, the function that will be called when parsing is done 22 | * @return: none 23 | */ 24 | module.exports.parse = function(file, callback){ 25 | if(!callback){ 26 | return; 27 | } 28 | fs.readFile(file, 'utf8', function(err, data){ 29 | if(err){ 30 | callback(err); 31 | }else{ 32 | callback(null, parse(data)); 33 | } 34 | }); 35 | }; 36 | 37 | module.exports.parseSync = function(file){ 38 | return parse(fs.readFileSync(file, 'utf8')); 39 | }; 40 | 41 | function parse(data){ 42 | var value = {}; 43 | var lines = data.split(/\r\n|\r|\n/); 44 | var section = null; 45 | lines.forEach(function(line){ 46 | if(regex.comment.test(line)){ 47 | return; 48 | }else if(regex.param.test(line)){ 49 | var match = line.match(regex.param); 50 | if(section){ 51 | value[section][match[1]] = match[2]; 52 | }else{ 53 | value[match[1]] = match[2]; 54 | } 55 | }else if(regex.section.test(line)){ 56 | var match = line.match(regex.section); 57 | value[match[1]] = {}; 58 | section = match[1]; 59 | }else if(line.length == 0 && section){ 60 | section = null; 61 | }; 62 | }); 63 | return value; 64 | } 65 | 66 | module.exports.parseString = parse; 67 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Node .ini parser 2 | 3 | Simple .ini parser for node, supports sections 4 | 5 | To parse a .ini file async: 6 |
 7 | var iniparser = require('iniparser');
 8 | iniparser.parse('./config.ini', function(err,data){
 9 | 	var version = data.version;
10 | });
11 | 
12 | 13 | To parse a .ini file sync: 14 |
15 | var iniparser = require('iniparser');
16 | var config = iniparser.parseSync('./config.ini');
17 | 
18 | 19 | To parse a string in .ini format: 20 |
21 | var iniparser = require('iniparser');
22 | var config = iniparser.parseString('foo=bar');
23 | 
24 | ## Installation 25 | npm: 26 | 27 | `npm install iniparser` 28 | ## License 29 | 30 | (The MIT License) 31 | 32 | Copyright (c) 2009-2010 Jordy van Gelder <jordyvangelder@gmail.com> 33 | 34 | Permission is hereby granted, free of charge, to any person obtaining 35 | a copy of this software and associated documentation files (the 36 | 'Software'), to deal in the Software without restriction, including 37 | without limitation the rights to use, copy, modify, merge, publish, 38 | distribute, sublicense, and/or sell copies of the Software, and to 39 | permit persons to whom the Software is furnished to do so, subject to 40 | the following conditions: 41 | 42 | The above copyright notice and this permission notice shall be 43 | included in all copies or substantial portions of the Software. 44 | 45 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 46 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 47 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 48 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 49 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 50 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 51 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 52 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'), 2 | iniparser = require('../lib/node-iniparser'); 3 | 4 | module.exports = { 5 | 'async read file': function(){ 6 | iniparser.parse('./files/test.ini', function(err, config){ 7 | assert.equal(err, null); 8 | }); 9 | }, 10 | 'async read non-existing file': function(){ 11 | iniparser.parse('./files/doesnotexists.ini', function(err, config){ 12 | assert.equal(err.code, 'ENOENT'); 13 | assert.equal(config, null); 14 | }); 15 | }, 16 | 'sync read file': function(){ 17 | var config = iniparser.parseSync('./files/test.ini'); 18 | assert.notEqual(config, null); 19 | }, 20 | 'sync read non-existing file': function(){ 21 | assert.throws(function(){ 22 | var config = iniparser.parseSync('./files/doesnotexists.ini'); 23 | }); 24 | }, 25 | 'async read file and look for variable': function(){ 26 | iniparser.parse('./files/test.ini', function(err, config){ 27 | assert.equal(err, null); 28 | assert.equal(config.foo, 'bar'); 29 | }); 30 | }, 31 | 'async read file and look for section': function(){ 32 | iniparser.parse('./files/test.ini', function(err, config){ 33 | assert.equal(err, null); 34 | assert.notEqual(config.worlds, null); 35 | assert.equal(config.worlds.earth, 'awesome'); 36 | assert.notEqual(config.section2, null); 37 | assert.equal(config.section2.bar, 'foo'); 38 | }); 39 | }, 40 | 'sync read file and look for variable': function(){ 41 | var config = iniparser.parseSync('./files/test.ini'); 42 | assert.equal(config.foo, 'bar'); 43 | }, 44 | 'sync read file and look for section': function(){ 45 | var config = iniparser.parseSync('./files/test.ini'); 46 | assert.notEqual(config.worlds, null); 47 | assert.equal(config.worlds.earth, 'awesome'); 48 | assert.notEqual(config.section2, null); 49 | assert.equal(config.section2.bar, 'foo'); 50 | }, 51 | 'variable with space at the end': function () { 52 | var config = iniparser.parseSync('./files/test.ini'); 53 | assert.notEqual('bar ', config.var_with_space_at_end); 54 | assert.equal('bar', config.var_with_space_at_end); 55 | }, 56 | 'look for a commented out variable': function(){ 57 | var config = iniparser.parseSync('./files/test.ini'); 58 | assert.equal(config.section2.test, null); 59 | }, 60 | 'variable with space in value': function(){ 61 | var config = iniparser.parseSync('./files/test.ini'); 62 | assert.equal(config.section2.there_is, "a space in here with = and trailing tab"); 63 | } 64 | }; 65 | --------------------------------------------------------------------------------