diff options
author | isaacs <i@izs.me> | 2012-08-15 06:46:53 +0400 |
---|---|---|
committer | isaacs <i@izs.me> | 2012-08-15 06:47:43 +0400 |
commit | fdd5b7ec7bea41e548cabedafb7a55ec60aeb5b1 (patch) | |
tree | 7b4a6b3e77211ca21ea50c1ab36a4ffc978a0682 | |
parent | be6389a0c40b132a09d211944480d5ec4bdc2f2f (diff) |
ini@1.0.4
-rw-r--r-- | node_modules/ini/ini.js | 32 | ||||
-rw-r--r-- | node_modules/ini/package.json | 16 | ||||
-rw-r--r-- | node_modules/ini/test/bar.js | 23 | ||||
-rw-r--r-- | node_modules/ini/test/fixtures/foo.ini | 29 | ||||
-rw-r--r-- | node_modules/ini/test/foo.js | 59 |
5 files changed, 146 insertions, 13 deletions
diff --git a/node_modules/ini/ini.js b/node_modules/ini/ini.js index e8a949f94..7939f20e5 100644 --- a/node_modules/ini/ini.js +++ b/node_modules/ini/ini.js @@ -5,6 +5,8 @@ exports.stringify = exports.encode = encode exports.safe = safe exports.unsafe = unsafe +var eol = process.platform === "win32" ? "\r\n" : "\n" + function encode (obj, section) { var children = [] , out = "" @@ -14,18 +16,19 @@ function encode (obj, section) { if (val && typeof val === "object") { children.push(k) } else { - out += safe(k) + " = " + safe(val) + "\n" + out += safe(k) + " = " + safe(val) + eol } }) if (section && out.length) { - out = "[" + safe(section) + "]" + "\n" + out + out = "[" + safe(section) + "]" + eol + out } children.forEach(function (k, _, __) { - var child = encode(obj[k], (section ? section + "." : "") + k) + var nk = dotSplit(k).join('\\.') + var child = encode(obj[k], (section ? section + "." : "") + nk) if (out.length && child.length) { - out += "\n" + out += eol } out += child }) @@ -33,6 +36,15 @@ function encode (obj, section) { return out } +function dotSplit (str) { + return str.replace(/\1/g, '\2LITERAL\\1LITERAL\2') + .replace(/\\\./g, '\1') + .split(/\./).map(function (part) { + return part.replace(/\1/g, '\\.') + .replace(/\2LITERAL\\1LITERAL\2/g, '\1') + }) +} + function decode (str) { var out = {} , p = out @@ -57,6 +69,11 @@ function decode (str) { } var key = unsafe(match[2]) , value = match[3] ? unsafe((match[4] || "")) : true + switch (value) { + case 'true': + case 'false': + case 'null': value = JSON.parse(value) + } p[key] = value }) @@ -66,15 +83,16 @@ function decode (str) { if (!out[k] || typeof out[k] !== "object") return false // see if the parent section is also an object. // if so, add it to that, and mark this one for deletion - var parts = k.split(".") + var parts = dotSplit(k) , p = out , l = parts.pop() + , nl = l.replace(/\\\./g, '.') parts.forEach(function (part, _, __) { if (!p[part] || typeof p[part] !== "object") p[part] = {} p = p[part] }) - if (p === out) return false - p[l] = out[k] + if (p === out && nl === l) return false + p[nl] = out[k] return true }).forEach(function (del, _, __) { delete out[del] diff --git a/node_modules/ini/package.json b/node_modules/ini/package.json index a022b598f..1ab148897 100644 --- a/node_modules/ini/package.json +++ b/node_modules/ini/package.json @@ -1,15 +1,19 @@ { - "author": "Isaac Z. Schlueter <i@izs.me> (http://blog.izs.me/)", + "author": { + "name": "Isaac Z. Schlueter", + "email": "i@izs.me", + "url": "http://blog.izs.me/" + }, "name": "ini", "description": "An ini encoder/decoder for node", - "version": "1.0.2", + "version": "1.0.4", "repository": { "type": "git", "url": "git://github.com/isaacs/ini.git" }, "main": "ini.js", "scripts": { - "test": "node ini.js" + "test": "tap test/*.js" }, "engines": { "node": "*" @@ -18,7 +22,7 @@ "devDependencies": { "tap": "~0.0.9" }, - "scripts": { - "test": "tap test/*.js" - } + "readme": "An ini format parser and serializer for node.\n\nSections are treated as nested objects. Items before the first heading\nare saved on the object directly.\n\n## Usage\n\nConsider an ini-file `config.ini` that looks like this:\n\n ; this comment is being ignored\n scope = global\n\n [database]\n user = dbuser\n password = dbpassword\n database = use_this_database\n\n [paths.default]\n datadir = /var/lib/data\n\nYou can read, manipulate and write the ini-file like so:\n\n var fs = require('fs')\n , ini = require('ini')\n\n var config = ini.parse(fs.readFileSync('./config.ini', 'utf-8'))\n\n config.scope = 'local'\n config.database.database = 'use_another_database'\n config.paths.default.tmpdir = '/tmp'\n delete config.paths.default.datadir\n\n fs.writeFileSync('./config_modified.ini', ini.stringify(config, 'section'))\n\nThis will result in a file called `config_modified.ini` being written to the filesystem with the following content:\n\n [section]\n scope = local\n [section.database]\n user = dbuser\n password = dbpassword\n database = use_another_database\n [section.paths.default]\n tmpdir = /tmp\n\n## API\n\n### decode(inistring)\nDecode the ini-style formatted `inistring` into a nested object.\n\n### parse(inistring)\nAlias for `decode(inistring)`\n\n### encode(object, [section])\nEncode the object `object` into an ini-style formatted string. If the optional parameter `section` is given, then all top-level properties of the object are put into this section and the `section`-string is prepended to all sub-sections, see the usage example above.\n\n### stringify(object, [section])\nAlias for `encode(object, [section])`\n\n### safe(val)\nEscapes the string `val` such that it is safe to be used as a key or value in an ini-file. Basically escapes quotes. For example\n\n ini.safe('\"unsafe string\"')\n\nwould result in\n\n \"\\\"unsafe string\\\"\"\n\n### unsafe(val)\nUnescapes the string `val`\n\n", + "_id": "ini@1.0.4", + "_from": "ini@latest" } diff --git a/node_modules/ini/test/bar.js b/node_modules/ini/test/bar.js new file mode 100644 index 000000000..cb16176ef --- /dev/null +++ b/node_modules/ini/test/bar.js @@ -0,0 +1,23 @@ +//test that parse(stringify(obj) deepEqu + +var ini = require('../') +var test = require('tap').test + +var data = { + 'number': {count: 10}, + 'string': {drink: 'white russian'}, + 'boolean': {isTrue: true}, + 'nested boolean': {theDude: {abides: true, rugCount: 1}} +} + + +test('parse(stringify(x)) deepEqual x', function (t) { + + for (var k in data) { + var s = ini.stringify(data[k]) + console.log(s, data[k]) + t.deepEqual(ini.parse(s), data[k]) + } + + t.end() +}) diff --git a/node_modules/ini/test/fixtures/foo.ini b/node_modules/ini/test/fixtures/foo.ini new file mode 100644 index 000000000..e5b186604 --- /dev/null +++ b/node_modules/ini/test/fixtures/foo.ini @@ -0,0 +1,29 @@ +o = p + + a with spaces = b c + +; wrap in quotes to JSON-decode and preserve spaces +" xa n p " = "\"\r\nyoyoyo\r\r\n" + +; wrap in quotes to get a key with a bracket, not a section. +"[disturbing]" = hey you never know + +; a section +[a] +av = a val +e = { o: p, a: { av: a val, b: { c: { e: "this [value]" } } } } +j = "{ o: "p", a: { av: "a val", b: { c: { e: "this [value]" } } } }" +"[]" = a square? + +; nested child without middle parent +; should create otherwise-empty a.b +[a.b.c] +e = 1 +j = 2 + +; dots in the section name should be literally interpreted +[x\.y\.z] +x.y.z = xyz + +[x\.y\.z.a\.b\.c] +a.b.c = abc diff --git a/node_modules/ini/test/foo.js b/node_modules/ini/test/foo.js new file mode 100644 index 000000000..2b32bb62f --- /dev/null +++ b/node_modules/ini/test/foo.js @@ -0,0 +1,59 @@ +var i = require("../") + , tap = require("tap") + , test = tap.test + , fs = require("fs") + , path = require("path") + , fixture = path.resolve(__dirname, "./fixtures/foo.ini") + , data = fs.readFileSync(fixture, "utf8") + , d + , expectE = 'o = p\n' + + 'a with spaces = b c\n' + + '" xa n p " = "\\"\\r\\nyoyoyo\\r\\r\\n"\n' + + '"[disturbing]" = hey you never know\n' + + '\n' + + '[a]\n' + + 'av = a val\n' + + 'e = { o: p, a: ' + + '{ av: a val, b: { c: { e: "this [value]" ' + + '} } } }\nj = "\\"{ o: \\"p\\", a: { av:' + + ' \\"a val\\", b: { c: { e: \\"this [value]' + + '\\" } } } }\\""\n"[]" = a square?\n\n[a.b.c]\ne = 1\n' + + 'j = 2\n\n[x\\.y\\.z]\nx.y.z = xyz\n\n' + + '[x\\.y\\.z.a\\.b\\.c]\n' + + 'a.b.c = abc\n' + , expectD = + { o: 'p', + 'a with spaces': 'b c', + " xa n p ":'"\r\nyoyoyo\r\r\n', + '[disturbing]': 'hey you never know', + a: + { av: 'a val', + e: '{ o: p, a: { av: a val, b: { c: { e: "this [value]" } } } }', + j: '"{ o: "p", a: { av: "a val", b: { c: { e: "this [value]" } } } }"', + "[]": "a square?", + b: { c: { e: '1', j: '2' } } }, + 'x.y.z': { + 'x.y.z': 'xyz', + 'a.b.c': { + 'a.b.c': 'abc' + } + } + } + +test("decode from file", function (t) { + var d = i.decode(data) + t.deepEqual(d, expectD) + t.end() +}) + +test("encode from data", function (t) { + var e = i.encode(expectD) + t.deepEqual(e, expectE) + + var obj = {log: { type:'file', level: {label:'debug', value:10} } } + e = i.encode(obj) + t.notEqual(e.slice(0, 1), '\n', 'Never a blank first line') + t.notEqual(e.slice(-2), '\n\n', 'Never a blank final line') + + t.end() +}) |