-
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?
Conversation
…ana/Assignment-1/assignment1.js
…-asthana/Assignment-1/assignment1.test.js
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.
Excellent start!
| 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"); |
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):
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})
| * argument's name from | ||
| * the commands array | ||
| */ | ||
| function extractArgumentName(argumentName) { |
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.
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.
| @@ -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 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"?
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.
Yes, I'll do that!
f9e6d92 to
4bfac84
Compare
| this.options.push({ name: name, isReq: isReq, type: type, cantBeUsedWith: cantBeUsedWith }); | ||
| } | ||
| } | ||
| Parser.prototype.options = []; |
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.
p = new Parser();
p.options.length; // 0
p.addOption('test')
p.options.length; // 1
q = new Parser();
q.options.length // 1, which is wrong
Lets not deal with prototypes in this problem, that concept isn't needed here.
| parser.addOption("key", true, "number"); | ||
| parser.addOption("name", false, "string"); | ||
| parser.addOption("local", false, 'boolean', "remote"); | ||
| parser.addOption("remote", false, "boolean", "local"); |
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.
Boolean arguments are bad in general, since it is not possible for me to figure out what the true/false means by looking at these calls alone. You're forcing me to look at your implementation to figure it out, which hurts readability.
Instead, you can do something like this:
parser.addOption({name: 'key', isRequired: true, type: "number"});
That way, it is clear.
|
|
||
|
|
||
| //MAIN FUNCTION | ||
| function parseArgumentsIntoJSON(commands) { |
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.
This should be a method in your class (along with the previous ones). That way,
const p = new Parser();
p.addOption(...)
result = p. parseArgumentsIntoJSON(commands);
Btw, you're not allowed to create the Parser object in this file, you can only define the class.
You should only create class instances outside the library, in your test (this is how your integration tests will look like).
| for (var i = 0; i < commands.length; i++) { | ||
|
|
||
| var argumentName = extractArgumentName(commands[i]); | ||
| var argumentValue = extractArgumentValue(commands[i]); |
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.
Do you really need separate functions for this? How about:
var [name, value] = commands[ii].split("=")
| //MAIN FUNCTION | ||
| function parseArgumentsIntoJSON(commands) { | ||
|
|
||
| for (var i = 0; i < commands.length; i++) { |
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.
| return !isNaN(argumentValueToBeChecked); | ||
| } | ||
| if (parser.options[i].type === "string") { | ||
| return /^[a-zA-Z]+$/.test(argumentValueToBeChecked); |
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.
Why does a string have restrictions like this?
All characters can be valid inside a string.
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.
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.
| 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`; |
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.
https://www.facebook.com/groups/856311934764144/permalink/861189610943043/
no global variables, no standalone functions, etc.
errorStatement & parser are global variables inside this library file.
I'd recommend raising exceptions here.
| */ | ||
| function ifArgumentsCanBeUsedTogether(argumentNameArray) { | ||
| for (var i = 0; i < argumentNameArray.length; i++) { | ||
| if (argumentNameArray.includes(parser.options[i].cantBeUsedWith)) { |
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.
You're assuming that only 2 arguments can be mutually exclusive.
What if we wanted to allow exactly one of 5 options?
| finalResult[argumentNameArray[i]] = argumentValueArray[i]; | ||
| } | ||
|
|
||
| console.log(JSON.stringify(finalResult)); |
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 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).
|
|
||
|
|
||
| argumentNameArray.push(argumentName); | ||
| argumentValueArray.push(argumentValue); |
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.
Why not use a map here? Instead of these 2 arrays which represent keys and values of that map.
Added property names while adding options in the parser object
Changed the validation criteria of the options of string type.
No description provided.