-
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 6 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,236 @@ | ||
| //Declaration of variables | ||
|
|
||
| var commands = process.argv.slice(2); | ||
| var argumentNameArray = []; | ||
| var argumentValueArray = []; | ||
| var errorStatement = ""; | ||
| var errorPresent = false; | ||
|
|
||
|
|
||
| class Parser { | ||
|
|
||
| addOption(name, isReq, type, cantBeUsedWith) { | ||
| this.options.push({ name: name, isReq: isReq, type: type, cantBeUsedWith: cantBeUsedWith }); | ||
| } | ||
| } | ||
| Parser.prototype.options = []; | ||
|
|
||
|
|
||
|
|
||
| var parser = new Parser(); | ||
|
|
||
| parser.addOption("key", true, "number"); | ||
| parser.addOption("name", false, "string"); | ||
| parser.addOption("local", false, 'boolean', "remote"); | ||
| parser.addOption("remote", false, "boolean", "local"); | ||
|
||
|
|
||
|
|
||
|
|
||
| /* | ||
| * Function for extracting out | ||
| * argument's name from | ||
| * the commands array | ||
| */ | ||
| function extractArgumentName(argument) { | ||
| var argumentName = ""; | ||
| for (var i = 2; i < argument.length; i++) { | ||
| if (argument[i] === '=') { | ||
| return argumentName; | ||
| } | ||
| argumentName += argument[i]; | ||
| } | ||
| return argumentName; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| /* | ||
| * Function for extracting out | ||
| * argument's value from | ||
| * the commands array | ||
| */ | ||
| function extractArgumentValue(argument) { | ||
| if (!argument.includes("=")) { | ||
| return true; | ||
| } | ||
| var argumentValue = ""; | ||
| var index = argument.indexOf('='); | ||
| for (var i = index + 1; i < argument.length; i++) { | ||
| argumentValue += argument[i]; | ||
| } | ||
| return argumentValue; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| /* | ||
| * Function to check if arguments | ||
| * given by the user are defined | ||
| */ | ||
| function checkIfArgumentDefined(argumentName) { | ||
| for (var i = 0; i < parser.options.length; i++) { | ||
|
||
| if (argumentName === parser.options[i].name) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| /* | ||
| * Function to validate the | ||
| * type of argument's value | ||
| */ | ||
| function typeCheck(argumentNameToBeChecked, argumentValueToBeChecked) { | ||
|
|
||
| var i; | ||
|
|
||
| for (i = 0; i < parser.options.length; i++) { | ||
| if (argumentNameToBeChecked === parser.options[i].name) { | ||
| argumentNameToBeChecked = parser.options[i].name; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (parser.options[i].type === "number") { | ||
| return !isNaN(argumentValueToBeChecked); | ||
| } | ||
| if (parser.options[i].type === "string") { | ||
| return /^[a-zA-Z]+$/.test(argumentValueToBeChecked); | ||
|
||
| } | ||
| if (parser.options[i].type === "boolean") { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| /* | ||
| * Function to check if | ||
| * the mandatory arguments are | ||
| * present in the commands | ||
| * given by the user | ||
| */ | ||
| function areRequiredArgumentsPresent(argumentNameArray) { | ||
| for (var i = 0; i < parser.options.length; i++) { | ||
| if (parser.options[i].isReq === true) { | ||
| if (!argumentNameArray.includes(parser.options[i].name)) { | ||
| errorStatement += `Error :The argument "--${parser.options[i].name}" is required\n`; | ||
|
||
| return false; | ||
| } | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| /* | ||
| * Function to check if arguments | ||
| * given by the user can be used together | ||
| */ | ||
| function ifArgumentsCanBeUsedTogether(argumentNameArray) { | ||
| for (var i = 0; i < argumentNameArray.length; i++) { | ||
| if (argumentNameArray.includes(parser.options[i].cantBeUsedWith)) { | ||
|
||
| errorStatement += `Error : The argument "--${parser.options[i].name}" can't be used with` + | ||
| ` the argument "--${parser.options[i].cantBeUsedWith}"\n` | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| //MAIN FUNCTION | ||
| function parseArgumentsIntoJSON(commands) { | ||
|
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. This should be a method in your class (along with the previous ones). That way, Btw, you're not allowed to create the Parser object in this file, you can only define the class. |
||
|
|
||
| for (var i = 0; i < commands.length; i++) { | ||
|
||
|
|
||
| var argumentName = extractArgumentName(commands[i]); | ||
| var argumentValue = extractArgumentValue(commands[i]); | ||
|
||
|
|
||
| //Checking if the argument provided is defined | ||
| if (!checkIfArgumentDefined(argumentName)) { | ||
| errorPresent = true; | ||
| errorStatement += `Error : The argument "--${argumentName}" is undefined`; | ||
|
|
||
| } | ||
|
|
||
| if (!typeCheck(argumentName, argumentValue)) { | ||
| errorStatement += `Error : Type of the argument "--${argumentName}" is invalid\n`; | ||
| errorPresent = true; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| argumentNameArray.push(argumentName); | ||
| argumentValueArray.push(argumentValue); | ||
|
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. Why not use a map here? Instead of these 2 arrays which represent keys and values of that map. |
||
| } | ||
|
|
||
|
|
||
|
|
||
| /* | ||
| * Checking if the mandatory arguments | ||
| * are present | ||
| */ | ||
| if (!areRequiredArgumentsPresent(argumentNameArray)) { | ||
| errorPresent = true; | ||
| } | ||
|
|
||
|
|
||
| /* | ||
| * Checking if the arguments | ||
| * can be used together | ||
| */ | ||
| if (!ifArgumentsCanBeUsedTogether(argumentNameArray)) { | ||
| errorPresent = true; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| /* | ||
| * 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)); | ||
|
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. In order to make this library actually useful, you should return the JSON object, and then let the application print it (or compare against expected value in test, or whatever else it wants to do). |
||
| } | ||
|
|
||
| /* | ||
| * Printing out the error statement | ||
| * if error is found | ||
| */ | ||
| else { | ||
| console.log(errorStatement); | ||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| /* | ||
| * Calling the main function and | ||
| * passing the command given by the user | ||
| */ | ||
| parseArgumentsIntoJSON(commands); | ||
|
|
||
|
|
||
|
|
||
| //Exporting functions to test | ||
| module.exports = { | ||
| extractArgumentName, | ||
| extractArgumentValue, | ||
| typeCheck, | ||
| checkIfArgumentDefined, | ||
| areRequiredArgumentsPresent, | ||
| ifArgumentsCanBeUsedTogether, | ||
| parseArgumentsIntoJSON | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| //USED JEST FOR TESTING THE PROJECT | ||
| const { extractArgumentName, | ||
| extractArgumentValue, | ||
| typeCheck, | ||
| checkIfArgumentDefined, | ||
| areRequiredArgumentsPresent, | ||
| ifArgumentsCanBeUsedTogether, | ||
| parseArgumentsIntoJSON } = 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 value of the argument passed', () => { | ||
| expect(extractArgumentValue("--name=akash")).toEqual("akash"); | ||
| }) | ||
|
|
||
| test('Should check valid type of the argument passed', () => { | ||
| expect(typeCheck("key", "213jkbkj12")).toEqual(false) | ||
| }) | ||
|
|
||
| test("Should check if given argument is defined", () => { | ||
| expect(checkIfArgumentDefined("name")).toEqual(true) | ||
| }); | ||
|
|
||
| test("Should check if given argument is defined", () => { | ||
| expect(checkIfArgumentDefined("lastName")).toEqual(false); | ||
| }); | ||
|
|
||
| test("Should check if given argument is defined", () => { | ||
| expect(areRequiredArgumentsPresent(["key", "name"])).toEqual(true); | ||
| }); | ||
|
|
||
| test("Should check if given argument is defined", () => { | ||
| expect(ifArgumentsCanBeUsedTogether(["key", "name", "local", "remote"])).toEqual(false); | ||
| }); | ||
|
|
||
|
|
||
|
|
||
|
|
||
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.
When you set the array directly on the prototype, it becomes a static variable, which is incorrect.
Lets not deal with prototypes in this problem, that concept isn't needed here.