Permalink
Browse files

Merge pull request #29 from yaniv-aknin/master

Add '-D' alternate lookup delimiter option
  • Loading branch information...
2 parents 3d28781 + e39bfb6 commit 69ff0b370ea606c5624d4045c01256e1348e7f30 @trentm committed May 22, 2012
Showing with 19 additions and 5 deletions.
  1. +14 −5 lib/jsontool.js
  2. +5 −0 test/test.js
View
@@ -152,6 +152,7 @@ function printHelp() {
util.puts(" '-e' and '-c' automatically processing each");
util.puts(" item of an input array.");
util.puts(" -d DELIM Delimiter string for tabular output (default is ' ').");
+ util.puts(" -D DELIM Delimiter character between lookups (default is '.').");
util.puts("");
util.puts(" -e CODE Execute the given code on the input. If input is an");
util.puts(" array, then each item of the array is processed");
@@ -196,7 +197,8 @@ function parseArgv(argv) {
outputMode: OM_JSONY,
jsonIndent: 2,
array: null,
- delim: ' '
+ delim: ' ',
+ lookupDelim: '.'
};
// Turn '-iH' into '-i -H', except for argument-accepting options.
@@ -275,6 +277,9 @@ function parseArgv(argv) {
case "-d":
parsed.delim = _parseString(args.shift());
break;
+ case "-D":
+ parsed.lookupDelim = args.shift()[0];
+ break;
case "-e":
parsed.exeSnippets.push(args.shift());
break;
@@ -304,14 +309,16 @@ function isInteger(s) {
// Parse a lookup string into a list of lookup bits. E.g.:
// "a.b.c" -> ["a","b","c"]
// "b['a']" -> ["b","['a']"]
-function parseLookup(lookup) {
+// Optionally receives an alternative lookup delimiter (other than '.')
+function parseLookup(lookup, lookupDelim) {
//var debug = console.warn;
var debug = function () {};
var bits = [];
debug("\n*** "+lookup+" ***")
bits = [];
+ lookupDelim = lookupDelim || ".";
var bit = "";
var states = [null];
var escaped = false;
@@ -343,7 +350,7 @@ function parseLookup(lookup) {
}
bit += ch;
break;
- case '.':
+ case lookupDelim:
if (bit !== "") {
bits.push(bit);
bit = ""
@@ -475,7 +482,7 @@ function parseInput(buffer) {
*
* @argument datum {Object}
* @argument lookup {Array} The parsed lookup (from
- * `parseLookup(<string>)`). Might be empty.
+ * `parseLookup(<string>, <string>)`). Might be empty.
* @returns {Object} The result of the lookup.
*/
function lookupDatum(datum, lookup) {
@@ -778,7 +785,9 @@ function main(argv) {
// Process: lookups
var lookupsAreIndeces = false;
- var lookups = lookupStrs.map(parseLookup);
+ var lookups = lookupStrs.map(function(lookup) {
+ return parseLookup(lookup, opts.lookupDelim);
+ });
if (lookups.length) {
if (opts.array) {
if (!Array.isArray(data)) data = [data];
View
@@ -59,6 +59,11 @@ var data = {
test.deepEqual(parseLookup("['a\\'[b']"), ["['a\\'[b']"]);
test.deepEqual(parseLookup("['a\\'[b'].c"), ["['a\\'[b']", "c"]);
+ test.deepEqual(parseLookup("a/b", "/"), ["a", "b"]);
+ test.deepEqual(parseLookup("a.b/c", "/"), ["a.b", "c"]);
+ test.deepEqual(parseLookup("a.b/c[42]", "/"), ["a.b", "c", "[42]"]);
+ test.deepEqual(parseLookup('["a/b"]', "/"), ['["a/b"]']);
+
test.done();
}
};

0 comments on commit 69ff0b3

Please sign in to comment.