Dictionary Data Object#1974
Conversation
lmccart
left a comment
There was a problem hiding this comment.
looks good so far, I made a few small notes
|
|
||
| var p5 = require('../core/core'); | ||
|
|
||
| //TESTED |
|
|
||
|
|
||
| //TESTED | ||
| p5.TypedDict.prototype.create = function(key, value) { |
There was a problem hiding this comment.
with a variable number of params, our convention is to leave the parens empty
There was a problem hiding this comment.
it this a public method? seems like it could be private in which case let's call it _create.
| this.count = 0; | ||
|
|
||
| if(arguments.length === 1) { | ||
| if(keys instanceof Object) { |
There was a problem hiding this comment.
hm is it keys or key? or are these two different things?
|
Okay this is getting close. All of the included unit tests work. I feel like what it mainly needs at this point is the documentation and methods to write out to a file. I was thinking about adding the ability to write to json and csv. Any opinions on any of the above or anything else about the code? |
| } | ||
| } | ||
| else if(arguments.length === 2) { | ||
| var key = arguments[0]; |
There was a problem hiding this comment.
this code below looks a little familiar... could we just call this._addObj() here with the arguments obj?
There was a problem hiding this comment.
hm I guess I am a bit confused, I was trying to handle if the user says stringDict.create('string1', 'string2' or they say stringDict.create( {'string1': 'string2', 'string3' : 'string4'} ). Sorry for my confusion!
There was a problem hiding this comment.
right so the first part of the if takes care of your second case: stringDict.create( {'string1': 'string2', 'string3' : 'string4'} ).
the else if part could read like:
else if (arguments.length == 2) {
this.addObj(arguments);
}
to take care of the case:
stringDict.create('string1', 'string2');
| // */ | ||
| p5.StringDict = function(key, value) { | ||
| this.data = {}; | ||
| this.count = 0; |
There was a problem hiding this comment.
I'm wondering about the need to keep track of count vs just calling Object.keys(this.data).length?
| // * @extends p5.TypedDict | ||
| // * @param | ||
| // */ | ||
| p5.NumberDict = function(key, value) { |
There was a problem hiding this comment.
are all these constructors generally the same? can we pass this work on to the p5.TypedDict() constructor instead to reduce redundancy?
There was a problem hiding this comment.
In the latest commit, I used apply to set call the TypedDict constructor which works but creates a certain degree of messiness with the passing of arguments. I tried other approaches like Object.create and setting their constructor to the TypedDict constructor but they weren't retaining the variables made by the constructor like data. Not sure if there is a more elegant way.
| }; | ||
|
|
||
| p5.TypedDict.prototype._validate = function(key, value) { | ||
| return true; |
There was a problem hiding this comment.
maybe the default method could make use of the type of object.. we already have number or string in the name, instead of having to write a custom validation method for each.
for example, on construction we could store a type:
this.type = m.constructor.name.replace('Dict', '').toLowerCase();
then the test would be
return (typeof value === this.type);
something like that?
There was a problem hiding this comment.
I can't figure out how to do this now that both types are falling on the TypedDict constructor. I tried things like p5.StringDict.constructor = p5.StringDict to unexpected results.
| } else { | ||
| var result = Object.keys(this.data)[0]; | ||
| for(var i=1; i>this.data.count; i++) { | ||
| if(Object.keys(this.data)[i] < result) { |
| * | ||
| * | ||
| */ | ||
| p5.NumberDict.prototype.minValue = function() { |
There was a problem hiding this comment.
i have the feeling these four blocks could be reduced into one function, since only one symbol or line changes between them. for example, if it's just a < that flips.. you can't pass operators as arguments but you could write a function that takes 1 or -1, and multiply both sides of the comparison by that operator. multiplying by -1 would have the effect of flipping the < to a >.
|
looking good! I left a few comments. I love the idea of being able to export to json and csv. |
as per lmccart’s suggestions
|
I made a save function but the way it currently works is that the user needs to pass in either |
|
we actually have saveJSON() and saveTable() methods in p5 already, so it may make sense to duplicate this syntax. |
|
Don't know how I overlooked those, I should probably duplicate that syntax but I should also just use them to do the work. slaps head |
Didn’t use p5.prototype.saveTable in TypedDict.saveTable because the data object would have to be converted to a p5.Table first which seemed like overkill
|
@mlarghydracept is this ready for final review? |
|
@lmccart Thanks for checking in! I am going to do another once over, write comments/examples, and a few more unit tests. I will do that this week and let you know when it is finished |
|
@mlarghydracept great, I'm going to close this until it's ready for review. just reopen whenever it is. this way I'll get the email notification to review. |
|
I think this is ready for review! A few notes -- I said this elsewhere in you comments on my code but I couldn't put all of the construction work in the TypedDict constructor because of the way the initializing data (the first arguments) were being retained. I tried a bunch of different approaches but maybe I am missing something. Also this is my first time writing a new file with new functions so I wasn't completely sure what I was doing with the commenting and documentation work. I tried to follow examples from other files. |
| * @return {Number|String} the value stored at that key | ||
| * | ||
| * @example | ||
| * <div><code> |
There was a problem hiding this comment.
if the examples aren't drawing anything, let's add class="norender" so we don't have blank gray squares in the ref.
https://github.com/processing/p5.js/wiki/Inline-documentation#adding-example-code
| * | ||
| */ | ||
|
|
||
| p5.TypedDict.prototype.clear = function(){ |
There was a problem hiding this comment.
this could be simplified as
this.data = [];
i think?
|
@mlarghydracept looks good! thanks for this big contribution. I'm going to go ahead and merge. if you can make a later pull request with the two small notes I made, that'd be great. will you make a note of that question about constructors and we can look at it again when we're in the same place? I couldn't quite remember what I was thinking so maybe my point didn't even make sense, but I want to take one last look together at some point just in case there was some further refinement we could make. |
Not ready to merge. I just wanted to put this here in progress to get feedback on my design. I also welcome any thoughts on methods that people think would be useful.
closes #422