Skip to content

Commit

Permalink
() simple array indexing.
Browse files Browse the repository at this point in the history
  • Loading branch information
zpoley committed Feb 26, 2011
1 parent eb0aac1 commit b728ef6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
33 changes: 23 additions & 10 deletions lib/jsonCommand.js
Expand Up @@ -249,21 +249,34 @@ JSON.Command.prototype.processKeys = function(parsedObject) {
var hsh = {}, cols = []; var hsh = {}, cols = [];
for (var i = 0; (i < this.keys.length); i++) { for (var i = 0; (i < this.keys.length); i++) {
try { 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 // create any keys that don't exist in the object chain
var s = this.keys[i].split("."); if (this.keys[i].indexOf(".") > -1) {
for (var j = 1; (j < s.length); j++) { var s = this.keys[i].split(".");
// create necessary keys for (var j = 1; (j < s.length); j++) {
var evalStr = "hsh." + s.slice(0,j).join("."); // create necessary keys
if (!eval(evalStr)) { var evalStr = "hsh." + s.slice(0,j).join(".");
eval("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 { else {
// no expansion
hsh[this.keys[i]] = parsedObject[this.keys[i]]; hsh[this.keys[i]] = parsedObject[this.keys[i]];
cols.push(parsedObject[this.keys[i]]); cols.push(parsedObject[this.keys[i]]);
} }
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,5 +1,5 @@
{ "name": "json" { "name": "json"
, "version": "0.0.6" , "version": "0.0.7"
, "engines": [ "node >=0.2.0" ] , "engines": [ "node >=0.2.0" ]
, "description": "JSON command line processing toolkit." , "description": "JSON command line processing toolkit."
, "author": "Zachary Poley <zpoley@gmail.com> (http://zpoley.net)" , "author": "Zachary Poley <zpoley@gmail.com> (http://zpoley.net)"
Expand Down
12 changes: 9 additions & 3 deletions test/test.js
Expand Up @@ -9,6 +9,9 @@ var testObj = {
id : 1310571, id : 1310571,
name : "foo" name : "foo"
}, },
arr : [
'a', 'b', 'c'
],
created_at : 127817599 created_at : 127817599
}; };


Expand Down Expand Up @@ -101,9 +104,9 @@ function printTestName(testName) {
printTestName("testProcessKeys"); printTestName("testProcessKeys");


var jsonC = new JSON.Command(); var jsonC = new JSON.Command();
jsonC.processArgs([ "user.name", "text" ]); jsonC.processArgs([ "user.name", "text", "arr[1]" ]);
assert.equal(jsonC.keys.length, 2, assert.equal(jsonC.keys.length, 3,
"processedKeys keys length == 2"); "processedKeys keys length == 3");


var resObj = jsonC.processKeys(testObj); var resObj = jsonC.processKeys(testObj);


Expand All @@ -117,6 +120,9 @@ function printTestName(testName) {
"processKeys result object created_at is undefined is true"); "processKeys result object created_at is undefined is true");
assert.equal(resObj.user.id, undefined, assert.equal(resObj.user.id, undefined,
"processKeys result object user.id is undefined is true"); "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");

})(); })();


})(); })();
Expand Down

0 comments on commit b728ef6

Please sign in to comment.