-
Notifications
You must be signed in to change notification settings - Fork 21
Solution for assignment 1 #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 4 commits
965b593
9501e87
cb29122
be9d4b6
9f2d066
a841a1b
d9e2313
68fc69a
84d4f6f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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"); | ||
|
|
||
|
|
||
|
|
||
| /* | ||
| * Function for extracting out | ||
| * argument's name from | ||
| * the commands array | ||
| */ | ||
| function extractArgumentName(argumentName) { | ||
|
||
| 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); | ||
|
||
| } | ||
|
|
||
| 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 }; | ||
|
|
||
| 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"); | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
| }) | ||
There was a problem hiding this comment.
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):