mirror of https://github.com/lukechilds/node.git
Ryan Dahl
15 years ago
3 changed files with 0 additions and 141 deletions
@ -1,81 +0,0 @@ |
|||||
var sys = require('sys'); |
|
||||
|
|
||||
sys.error('The "ini" module will be removed in future versions of Node, please extract it into your own code.'); |
|
||||
|
|
||||
// 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) { |
|
||||
var ini = {'-':{}}; |
|
||||
|
|
||||
var section = '-'; |
|
||||
|
|
||||
var lines = d.split('\n'); |
|
||||
for (var i=0; i<lines.length; i++) { |
|
||||
var line = lines[i].trim(), |
|
||||
rem = line.indexOf(";"); |
|
||||
|
|
||||
if (rem !== -1) line = line.substr(0, rem); |
|
||||
|
|
||||
var re = /^\[([^\]]*)\]$|^([^=]+)(=(.*))?$/i; |
|
||||
|
|
||||
var match = line.match(re); |
|
||||
if (match != null) { |
|
||||
if (match[1] != undefined) { |
|
||||
section = match[1].trim(); |
|
||||
ini[section] = {}; |
|
||||
} else { |
|
||||
var key = match[2].trim(), |
|
||||
value = (match[3]) ? (match[4] || "").trim() : true; |
|
||||
ini[section][key] = value; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
return ini; |
|
||||
}; |
|
||||
|
|
||||
function safe (val) { |
|
||||
return (val+"").replace(/[\n\r]+/g, " "); |
|
||||
} |
|
||||
|
|
||||
// ForEaches over an object. The only thing faster is to inline this function.
|
|
||||
function objectEach(obj, fn, thisObj) { |
|
||||
var keys, key, i, length; |
|
||||
keys = Object.keys(obj); |
|
||||
length = keys.length; |
|
||||
for (i = 0; i < length; i++) { |
|
||||
key = keys[i]; |
|
||||
fn.call(thisObj, obj[key], key, obj); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
exports.stringify = function (obj) { |
|
||||
// if the obj has a "-" section, then do that first.
|
|
||||
var ini = []; |
|
||||
if ("-" in obj) { |
|
||||
objectEach(obj["-"], function (value, key) { |
|
||||
ini[ini.length] = safe(key) + " = " + safe(value) + "\n"; |
|
||||
}); |
|
||||
} |
|
||||
objectEach(obj, function (section, name) { |
|
||||
if (name === "-") return; |
|
||||
ini[ini.length] = "[" + safe(name) + "]\n"; |
|
||||
objectEach(section, function (value, key) { |
|
||||
ini[ini.length] = safe(key) + ((value === true) |
|
||||
? "\n" |
|
||||
: " = "+safe(value)+"\n"); |
|
||||
}); |
|
||||
}); |
|
||||
return ini.join(""); |
|
||||
}; |
|
||||
|
|
||||
exports.encode = exports.stringify; |
|
||||
exports.decode = exports.parse; |
|
@ -1,59 +0,0 @@ |
|||||
require("../common"); |
|
||||
var path = require('path'), |
|
||||
fs = require("fs"), |
|
||||
ini = require("ini"), |
|
||||
parse = require("ini").parse; |
|
||||
|
|
||||
debug("load fixtures/fixture.ini"); |
|
||||
|
|
||||
p = path.join(fixturesDir, "fixture.ini"); |
|
||||
|
|
||||
fs.readFile(p, 'utf8', function(err, data) { |
|
||||
if (err) throw err; |
|
||||
|
|
||||
assert.equal(typeof parse, 'function'); |
|
||||
|
|
||||
var iniContents = parse(data); |
|
||||
assert.equal(typeof iniContents, 'object'); |
|
||||
|
|
||||
var expect = { "-" : |
|
||||
{ "root" : "something" |
|
||||
, "url" : "http://example.com/?foo=bar" |
|
||||
} |
|
||||
, "the section with whitespace" : |
|
||||
{ "this has whitespace" : "yep" |
|
||||
, "just a flag, no value." : true |
|
||||
} |
|
||||
, "section" : |
|
||||
{ "one" : "two" |
|
||||
, "Foo" : "Bar" |
|
||||
, "this" : "Your Mother!" |
|
||||
, "blank" : "" |
|
||||
} |
|
||||
, "Section Two" : |
|
||||
{ "something else" : "blah" |
|
||||
, "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, |
|
||||
"actual: \n"+inspect(iniContents) +"\n≠\nexpected:\n"+inspect(expect)); |
|
||||
|
|
||||
assert.equal(ini.stringify(iniContents), expectStr, |
|
||||
"actual: \n"+inspect(ini.stringify(iniContents)) +"\n≠\nexpected:\n"+inspect(expectStr)); |
|
||||
}); |
|
||||
|
|
||||
|
|
Loading…
Reference in new issue