Skip to content

Commit

Permalink
comments for gist file
Browse files Browse the repository at this point in the history
  • Loading branch information
jhauga committed Dec 14, 2023
1 parent 0b7fd0b commit d90c8ad
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
2 changes: 1 addition & 1 deletion API/logs/factOfTheDay.log
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Last run:
Wed Dec 13 22:35:03 EST 2023
Thu Dec 14 00:05:03 EST 2023
45 changes: 29 additions & 16 deletions API/scripts/checkJSONReturn.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,48 @@
// Check that a property was created in json file and ouput pass or fail.

var fs = require("fs");
var parOneCheckJSONReturn = process.argv[2];
var parTwoCheckJSONReturn = process.argv[3];

// Required parameters.
var parOneCheckJSONReturn = process.argv[2]; // full path to json file being checked
var parTwoCheckJSONReturn = process.argv[3]; // property to check for

// Format file to be read as JSON and add clarity with variable naming.
var checkFile = fs.readFileSync(parOneCheckJSONReturn);
var fileJSON = JSON.parse(checkFile);
var propCheck = parTwoCheckJSONReturn;

// Turn off by default.
var checkReturn = 0;

// Recurse function.
function recurseCheckJSONReturn(cur) {
for (i in cur) {
if (typeof cur[i] == "object") {
if (i == propCheck) {
checkReturn = 1;
break;
} else {
recurseCheckJSONReturn(cur[i]);
for (i in cur) { // check each property
if (typeof cur[i] == "object") { // check if object
if (i == propCheck) { // is this the property
checkReturn = 1; // it is the propery
break; // check passed so end loop
} else { // property not found check the next one
recurseCheckJSONReturn(cur[i]); // check the next property in iteration
}
} else {
if (i == propCheck) {
checkReturn = 1;
break;
} else { // check this property
if (i == propCheck) { // is this the property
checkReturn = 1; // it is the property
break; // check assed so end loop
}
}
}
}
// Call function to check if property in file.
recurseCheckJSONReturn(fileJSON);

// Was the property found?
if (checkReturn == 1) {
console.log("pass");
console.log("pass"); // output pass to terminal
} else {
console.log("fail");
}
console.log("fail"); // output fail to terminal
}

// Practical use:
// > node checkJSONReturn.js "path/toFile.json" "propertyToCheck" > storeCheckedJSON.txt
// > if [ $(cat storeCheckedJSON.txt) - "pass" ]; then
// > # some set of commands if property found

0 comments on commit d90c8ad

Please sign in to comment.