Add ini.stringify functionality, a test, and some TODOs in ini.js
This commit is contained in:
parent
1b758ef268
commit
4befe93a4b
37
lib/ini.js
37
lib/ini.js
@ -1,3 +1,13 @@
|
|||||||
|
// TODO:
|
||||||
|
// 1. Handle quoted strings, including line breaks, so that this:
|
||||||
|
// foo = "bar
|
||||||
|
// baz"
|
||||||
|
// parses to {foo:"bar\n baz"}
|
||||||
|
// 2. Escape with \, so that this:
|
||||||
|
// foo = bar\
|
||||||
|
// \"baz
|
||||||
|
// parses to {foo:"bar\n \"baz"}
|
||||||
|
|
||||||
exports.parse = function(d) {
|
exports.parse = function(d) {
|
||||||
var ini = {'-':{}};
|
var ini = {'-':{}};
|
||||||
|
|
||||||
@ -26,4 +36,31 @@ exports.parse = function(d) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return ini;
|
return ini;
|
||||||
|
};
|
||||||
|
|
||||||
|
function safe (val) {
|
||||||
|
return (val+"").replace(/[\n\r]+/g, " ");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
exports.stringify = function (obj) {
|
||||||
|
// if the obj has a "-" section, then do that first.
|
||||||
|
var ini = "";
|
||||||
|
if ("-" in obj) {
|
||||||
|
for (var key in obj["-"]) {
|
||||||
|
ini += safe(key)+" = "+safe(obj["-"][key])+"\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (var section in obj) if (section !== "-") {
|
||||||
|
ini += "[" + safe(section) + "]\n";
|
||||||
|
for (var key in obj[section]) {
|
||||||
|
|
||||||
|
ini += safe(key) + ((obj[section][key] === true)
|
||||||
|
? "\n"
|
||||||
|
: " = "+safe(obj[section][key])+"\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ini;
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.encode = exports.stringify;
|
||||||
|
exports.decode = exports.parse;
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
require("../common");
|
require("../common");
|
||||||
var path = require('path');
|
var path = require('path'),
|
||||||
var fs = require("fs");
|
fs = require("fs"),
|
||||||
parse = require("ini").parse;
|
ini = require("ini"),
|
||||||
|
parse = require("ini").parse;
|
||||||
|
|
||||||
debug("load fixtures/fixture.ini");
|
debug("load fixtures/fixture.ini");
|
||||||
|
|
||||||
@ -15,32 +16,44 @@ fs.readFile(p,function(err, data) {
|
|||||||
var iniContents = parse(data);
|
var iniContents = parse(data);
|
||||||
assert.equal(typeof iniContents, 'object');
|
assert.equal(typeof iniContents, 'object');
|
||||||
|
|
||||||
var expect =
|
var expect = { "-" :
|
||||||
{ "-" :
|
{ "root" : "something"
|
||||||
{ "root" : "something"
|
, "url" : "http://example.com/?foo=bar"
|
||||||
, "url" : "http://example.com/?foo=bar"
|
}
|
||||||
}
|
, "the section with whitespace" :
|
||||||
, "the section with whitespace" :
|
{ "this has whitespace" : "yep"
|
||||||
{ "this has whitespace" : "yep"
|
, "just a flag, no value." : true
|
||||||
, "just a flag, no value." : true
|
}
|
||||||
}
|
, "section" :
|
||||||
, "section" :
|
{ "one" : "two"
|
||||||
{ "one" : "two"
|
, "Foo" : "Bar"
|
||||||
, "Foo" : "Bar"
|
, "this" : "Your Mother!"
|
||||||
, "this" : "Your Mother!"
|
, "blank" : ""
|
||||||
, "blank" : ""
|
}
|
||||||
}
|
, "Section Two" :
|
||||||
, "Section Two" :
|
{ "something else" : "blah"
|
||||||
{ "something else" : "blah"
|
, "remove" : "whitespace"
|
||||||
, "remove" : "whitespace"
|
}
|
||||||
}
|
},
|
||||||
};
|
expectStr = "root = something\n"+
|
||||||
|
"url = http://example.com/?foo=bar\n"+
|
||||||
|
"[the section with whitespace]\n"+
|
||||||
|
"this has whitespace = yep\n"+
|
||||||
|
"just a flag, no value.\n"+
|
||||||
|
"[section]\n"+
|
||||||
|
"one = two\n"+
|
||||||
|
"Foo = Bar\n"+
|
||||||
|
"this = Your Mother!\n"+
|
||||||
|
"blank = \n"+
|
||||||
|
"[Section Two]\n"+
|
||||||
|
"something else = blah\n"+
|
||||||
|
"remove = whitespace\n";
|
||||||
|
|
||||||
assert.deepEqual(iniContents, expect,
|
assert.deepEqual(iniContents, expect,
|
||||||
"actual: \n"+inspect(iniContents) +"\n≠\nexpected:\n"+inspect(expect))
|
"actual: \n"+inspect(iniContents) +"\n≠\nexpected:\n"+inspect(expect));
|
||||||
|
|
||||||
assert.equal(iniContents['-']['root'],'something');
|
|
||||||
assert.equal(iniContents['section']['blank'],'');
|
|
||||||
assert.equal(iniContents['Section Two']['remove'],'whitespace');
|
|
||||||
|
|
||||||
|
assert.equal(ini.stringify(iniContents), expectStr,
|
||||||
|
"actual: \n"+inspect(ini.stringify(iniContents)) +"\n≠\nexpected:\n"+inspect(expectStr));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user