Skip to content
Browse files

() a bit more array indexing.

  • Loading branch information...
1 parent 4c71b37 commit fa991c36454708e2c53ba6949cba3709052b7479 @zpoley committed Feb 26, 2011
Showing with 38 additions and 22 deletions.
  1. +25 −16 lib/jsonCommand.js
  2. +13 −6 test/test.js
View
41 lib/jsonCommand.js
@@ -248,7 +248,7 @@ JSON.Command.prototype.processExecutables = function(parsedObject) {
/*
Process requested keys against parsedObject.
- This may be one of the most complicated parts of this code, and it may
+ This is one of the most complicated parts of this code, and it may
very well not need to be. If you have a better idea of how to do this
please let me know: zpoley@gmail.com.
@@ -257,14 +257,25 @@ JSON.Command.prototype.processExecutables = function(parsedObject) {
the keys from the old object.
2. Create each object necessary in the chain in order for the resulting
object to retain the same structure of the parsedObject.
+ (arrays and dictionaries require separate handling)
3. Assign each requested key value from the parsedObject into the new
- object.
+ object. (arrays and dictionaries require separate handling)
*/
JSON.Command.prototype.processKeys = function(parsedObject) {
if (this.keys.length) {
- var hsh = {}, cols = [];
+ var hsh = {}, cols = [], evalStr = null, instStr = null, simpleKey = null;
+ function instantiateArrayAndPush(key) {
+ simpleKey = key.split("[").shift();
+ // instantiate array in new object if not exists
+ instStr = "if (!hsh." + simpleKey + ") { hsh." + simpleKey + " = []; }";
+ eval(instStr);
+ // push new value into array (command line order matters)
+ evalStr = "hsh." + simpleKey + ".push(" + "parsedObject." + key + ")";
+ eval(evalStr);
+ cols.push(eval("parsedObject." + key));
+ }
for (var i = 0; (i < this.keys.length); i++) {
try {
if ((this.keys[i].indexOf(".") > -1) || (this.keys[i].indexOf("[") > -1)) {
@@ -273,24 +284,22 @@ JSON.Command.prototype.processKeys = function(parsedObject) {
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(".");
+ 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) {
+ instantiateArrayAndPush(this.keys[i]);
+ }
+ else {
+ 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]));
+ else if (this.keys[i].indexOf("[") > -1) {
+ instantiateArrayAndPush(this.keys[i]);
}
}
else {
View
19 test/test.js
@@ -9,9 +9,14 @@ var testObj = {
id : 1310571,
name : "foo"
},
- arr : [
+ arr1 : [
'a', 'b', 'c'
],
+ obj1 : {
+ arr2 : [
+ 'd', 'e', 'f'
+ ]
+ },
created_at : 127817599
};
@@ -104,9 +109,9 @@ function printTestName(testName) {
printTestName("testProcessKeys");
var jsonC = new JSON.Command();
- jsonC.processArgs([ "user.name", "text", "arr[1]" ]);
- assert.equal(jsonC.keys.length, 3,
- "processedKeys keys length == 3");
+ jsonC.processArgs([ "user.name", "text", "arr1[1]", "obj1.arr2[2]" ]);
+ assert.equal(jsonC.keys.length, 4,
+ "processedKeys keys length == 4");
var resObj = jsonC.processKeys(testObj);
@@ -120,8 +125,10 @@ 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");
+ assert.equal(resObj.arr1[0], testObj.arr1[1],
+ "processKeys result object arr1[0] = testObj.arr1[0] is true");
+ assert.equal(resObj.obj1.arr2[0], testObj.obj1.arr2[2],
+ "processKeys result object obj1.arr2[0] = testObj.obj1.arr2[2] is true");
})();

0 comments on commit fa991c3

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