Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 188 additions & 0 deletions solutions/akash-asthana/Assignment-1/assignment1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
/*Declaration of variables */
var commands = process.argv.slice(2);
var validArguments = ["key", "name", "local", "remote"];
var argumentNameArray = [];
var argumentValueArray = [];
var errorStatement = "";
var errorPresent = false;



/*
* Defining a class for an
* argument's properties
*/
class Arguments {
constructor(isReq, type, cantBeUsedWith) {
this.isReq = isReq;
this.type = type;
this.cantBeUsedWith = cantBeUsedWith;
}
}



/*
* Defining valid arguments as objects
* with properties like isReq,
* type and cantBeUsedWith
*/
var key = new Arguments(true, "number");
var name = new Arguments(false, "string");
var local = new Arguments(false, "boolean", "remote");
var remote = new Arguments(false, "boolean", "local");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to be truly reusable (coding instruction 4.2), you can create a class like this (where we can add different options for each test):

parser = Parser()
parser.add_option('key', type=int, required=True)
parser.add_option('name', type=str)
self.assertEqual(parser.parse(['--key=123', '--name=abc']), {'key': 123, 'name': 'abc'})
self.assertEqual(parser.parse(['--key=456']), {'key': 456})




/*
* Function for extracting out
* argument's name from
* the commands array
*/
function extractArgumentName(argumentName) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the method takes argumentName as input, why does it have to do any process to extract stuff.
Ie - the variable name is not accurate (coding instruction 3), you can just use argument or option if you wish.

var key = "";
for (var i = 2; i < argumentName.length; i++) {
if (argumentName[i] === '=') {
return key;
}
key += argumentName[i];
}
return key;
}



/*
* Function for extracting out
* argument's value from
* the commands array
*/
function extractArgumentValue(argumentName) {
if (!argumentName.includes("=")) {
return true;
}
var argumentValue = "";
var index = argumentName.indexOf('=');
for (var i = index + 1; i < argumentName.length; i++) {
argumentValue += argumentName[i];
}
return argumentValue;
}



/*
* Function to validate the
* type of argument's value
*/
function typeCheck(argumentNameToBeChecked, argumentValueToBeChecked) {
if (eval(argumentNameToBeChecked).type === "number") {
return !isNaN(argumentValueToBeChecked);
}

if (eval(argumentNameToBeChecked).type === "string") {
return /^[a-zA-Z]+$/.test(argumentValueToBeChecked);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does a string have restrictions like this?
All characters can be valid inside a string.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intended a "string" to consist of only letters so that the option "--name" can have a proper name like "akash" instead of something like "akash123". Anyway i'll change it.

}

if (eval(argumentNameToBeChecked).type === "boolean") {
return true;
}
}



/*
* Iterating the commands array
* to extract argument's
* name and value
*/
for (var i = 0; i < commands.length; i++) {

var argumentName = extractArgumentName(commands[i]);
var argumentValue = extractArgumentValue(commands[i]);

//Checking if the argument provided is valid
if (!validArguments.includes(argumentName)) {
errorPresent = true;
errorStatement += `Error : ${argumentName} is undefined`;
//break;
}

argumentNameArray.push(argumentName);
argumentValueArray.push(argumentValue);
}



/*
* Iterating the validArguments array
* to check if the mandatory
* arguments are present in the
* commands given by the user
*/
for (var i = 0; i < validArguments.length; i++) {
if (eval(validArguments[i]).isReq == true) {
if (!argumentNameArray.includes(validArguments[i])) {
errorStatement += `Error : --${validArguments[i]} argument is required\n`;
errorPresent = true;
break;
}
}
}



/*
* Iterating the argumentNameArray
* to validate the type of values
* provided by the user
*/
for (i = 0; i < argumentNameArray.length; i++) {
if (!typeCheck(argumentNameArray[i], argumentValueArray[i])) {
errorStatement += `Error : Type of ${argumentNameArray[i]} is invalid\n`;
errorPresent = true;
}
}



/*
* Iterating the argumentNameArray
* to check if the arguments provided
* by the user can be used together
*/
for (var i = 0; i < argumentNameArray.length; i++) {
if (argumentNameArray.includes(eval(argumentNameArray[i]).cantBeUsedWith)) {
errorStatement += `Error : the --${eval(argumentNameArray[i]).cantBeUsedWith} ` +
`argument cannot be used with the` +
`--${argumentNameArray[i]} argument`;
errorPresent = true;
break;
}
}



/*
* Printing out the JSON output
* if no error is found
*/
if (!errorPresent) {
var finalResult = {};
for (var i = 0; i < argumentNameArray.length; i++) {
finalResult[argumentNameArray[i]] = argumentValueArray[i];
}

console.log(JSON.stringify(finalResult));
}
/*
* Printing out the error statement
* if error is found
*/
else {
console.log(errorStatement)
}


module.exports = { extractArgumentName, extractArgumentValue, typeCheck };

28 changes: 28 additions & 0 deletions solutions/akash-asthana/Assignment-1/assignment1.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//USED JEST FOR TESTING THE PROJECT
const { extractArgumentName, extractArgumentValue, typeCheck } = require("./assignment1");

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What you have here are some "unit tests", which is a good start.
Can you also write some "integration tests"?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'll do that!

test("Should return the name of the argument passed", () => {
expect(extractArgumentName("--key=212")).toEqual("key");
});

test("Should return the name of the argument passed", () => {
expect(extractArgumentName("--name=akash")).toEqual("name");
});


test('Should return value of the argument passed', () => {
expect(extractArgumentValue("--name=akash")).toEqual("akash");
})

test('Should return value of the argument passed', () => {
expect(extractArgumentValue("--name=2434")).toEqual("2434");
})


test('Should check valid type of the argument passed', () => {
expect(typeCheck("key", "213jkbkj12")).toEqual(false)
})

test('Should check valid type of the argument passed', () => {
expect(typeCheck("name", "kaustubh")).toEqual(true)
})