Skip to content
Browse files

() simple array indexing.

  • Loading branch information...
1 parent eb0aac1 commit b728ef693a8f9cfb6c923458a8aa314e5d796547 @zpoley committed
Showing with 33 additions and 14 deletions.
  1. +23 −10 lib/jsonCommand.js
  2. +1 −1 package.json
  3. +9 −3 test/test.js
View
33 lib/jsonCommand.js
@@ -249,21 +249,34 @@ JSON.Command.prototype.processKeys = function(parsedObject) {
var hsh = {}, cols = [];
for (var i = 0; (i < this.keys.length); i++) {
try {
- if (this.keys[i].indexOf(".") > -1) {
+ if ((this.keys[i].indexOf(".") > -1) || (this.keys[i].indexOf("[") > -1)) {
// create any keys that don't exist in the object chain
- var s = this.keys[i].split(".");
- for (var j = 1; (j < s.length); j++) {
- // create necessary keys
- var evalStr = "hsh." + s.slice(0,j).join(".");
- if (!eval(evalStr)) {
- eval("hsh." + s.slice(0,j).join(".") + " = {};");
+ if (this.keys[i].indexOf(".") > -1) {
+ var s = this.keys[i].split(".");
+ for (var j = 1; (j < s.length); j++) {
+ // create necessary keys
+ var evalStr = "hsh." + s.slice(0,j).join(".");
+ if (!eval(evalStr)) {
+ eval("hsh." + s.slice(0,j).join(".") + " = {};");
+ }
}
+ var evalStr = "hsh." + s.join(".") + " = " + "parsedObject." + s.join(".");
+ eval(evalStr);
+ cols.push(eval("parsedObject." + s.join(".")));
+ }
+ if (this.keys[i].indexOf("[") > -1) {
+ var simpleKey = this.keys[i].split("[").shift();
+ // instantiate array
+ var instStr = "if (!hsh." + simpleKey + ") { hsh." + simpleKey + " = []; }";;
+ eval(instStr);
+ // push val to array
+ var evalStr = "hsh." + simpleKey + ".push(" + "parsedObject." + this.keys[i] + ")";
+ eval(evalStr);
+ cols.push(eval("parsedObject." + this.keys[i]));
}
- var evalStr = "hsh." + s.join(".") + " = " + "parsedObject." + s.join(".");
- eval(evalStr);
- cols.push(eval("parsedObject." + s.join(".")));
}
else {
+ // no expansion
hsh[this.keys[i]] = parsedObject[this.keys[i]];
cols.push(parsedObject[this.keys[i]]);
}
View
2 package.json
@@ -1,5 +1,5 @@
{ "name": "json"
-, "version": "0.0.6"
+, "version": "0.0.7"
, "engines": [ "node >=0.2.0" ]
, "description": "JSON command line processing toolkit."
, "author": "Zachary Poley <zpoley@gmail.com> (http://zpoley.net)"
View
12 test/test.js
@@ -9,6 +9,9 @@ var testObj = {
id : 1310571,
name : "foo"
},
+ arr : [
+ 'a', 'b', 'c'
+ ],
created_at : 127817599
};
@@ -101,9 +104,9 @@ function printTestName(testName) {
printTestName("testProcessKeys");
var jsonC = new JSON.Command();
- jsonC.processArgs([ "user.name", "text" ]);
- assert.equal(jsonC.keys.length, 2,
- "processedKeys keys length == 2");
+ jsonC.processArgs([ "user.name", "text", "arr[1]" ]);
+ assert.equal(jsonC.keys.length, 3,
+ "processedKeys keys length == 3");
var resObj = jsonC.processKeys(testObj);
@@ -117,6 +120,9 @@ function printTestName(testName) {
"processKeys result object created_at is undefined is true");
assert.equal(resObj.user.id, undefined,
"processKeys result object user.id is undefined is true");
+ assert.equal(resObj.arr[0], testObj.arr[1],
+ "processKeys result object arr[0] = testObj.arr[0] is true");
+
})();
})();

0 comments on commit b728ef6

Please sign in to comment.
Something went wrong with that request. Please try again.