Browse Source

Added Parse INI to Node, tests

v0.7.4-release
Rob Ellis 15 years ago
committed by Ryan Dahl
parent
commit
5c78c45fa3
  1. 26
      lib/ini.js
  2. 11
      test/fixtures/fixture.ini
  3. 22
      test/simple/test-ini.js

26
lib/ini.js

@ -0,0 +1,26 @@
exports.parse = function(d) {
var trim = function(str) { return str.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); }
var ini = {'-':{}};
var section = '-';
var lines = d.split('\n');
for (var i=0; i<lines.length; i++) {
var re = /(.*)=(.*)|\[([a-z:\.0-9_\s]+)\]/i;
var match = lines[i].match(re);
if (match != null) {
if (match[3] != undefined) {
section = match[3];
ini[section] = {};
} else {
var key = trim(match[1]);
var value = trim(match[2]);
ini[section][key] = value;
}
}
}
return ini;
}

11
test/fixtures/fixture.ini

@ -0,0 +1,11 @@
root=something
[section]
one=two
Foo=Bar
this=Your Mother!
blank=
[Section Two]
something else=blah
remove = whitespace

22
test/simple/test-ini.js

@ -0,0 +1,22 @@
process.mixin(require("../common"));
require("fs");
parse = require("ini").parse;
debug("load fixtures/fixture.ini");
p = path.join(fixturesDir, "fixture.ini");
fs.readFile(p,function(err, data) {
if (err) throw err;
assert.equal(typeof parse, 'function');
var iniContents = parse(data);
assert.equal(typeof iniContents, 'object');
assert.deepEqual(iniContents,{"-":{"root":"something"},"section":{"one":"two","Foo":"Bar","this":"Your Mother!","blank":""},"Section Two":{"something else":"blah","remove":"whitespace"}})
assert.equal(iniContents['-']['root'],'something');
assert.equal(iniContents['section']['blank'],'');
assert.equal(iniContents['Section Two']['remove'],'whitespace');
});
Loading…
Cancel
Save