Uh oh!
There was an error while loading. Please reload this page.
fs: getOption to populate passed in default values - #11630
Conversation
In fs.js, the getOption can be improved to populate the option object with the passed in defaultOptions if the default options are not there in the original option object. This way we don't have to do options.flag || 'w' etc.
sam-github
commented
Mar 1, 2017
This PR would be easier to understand if the description contained some example code that does not work as expected before your change (with a description of what you consider expected). |
fhalde
commented
Mar 1, 2017
@sam-github There is nothing that is working unexpectedly. I just felt we are duplicating the work of checking if default values exists or not in the options. For e.g. fs.writeFile=function(file,options,cb){options=getOptions(options,{flag: 'w'});/* later somewhere */flags=options.flags||'w'/* could have just been the below had getOptions populated the default values if not exist */flags=options.flags} |
fhalde
commented
Mar 1, 2017
Updated the description to explain what this PR would lead to |
jasnell
commented
Mar 15, 2017
@nodejs/collaborators ... thoughts? |
jasnell
commented
Mar 15, 2017
| if (options.encoding !== 'buffer') | ||
| assertEncoding(options.encoding); | ||
| return options; | ||
| return Object.assign({}, defaultOptions, options); |
There was a problem hiding this comment.
Have you benchmarked this? I thought Object.assign() was still slow?
There was a problem hiding this comment.
never done a benchmark. do we have any existing ones to compare it with? or any suggestions how to do a benchmark? Thanks
There was a problem hiding this comment.
I was suggested to use util.extend, would it be faster ?
There was a problem hiding this comment.
Yes, util._extend() would probably be a better choice, at least in core. Object.assign() is probably fine for tests and non-runtime stuff.
rmg
commented
Mar 15, 2017
Why are the tests changed to use |
fhalde
commented
Mar 15, 2017
@rmg I can recall that |
rmg
commented
Mar 15, 2017
Doh! I should have caught that 😊 |
fhalde
commented
Mar 16, 2017
@rmg it shouldn't be a problem though right ? I mean was there a reason why in the tests the properties were put in the prototype ? |
@fhalde after some digging, it seems those tests are intentionally written to use edit forgot to add, this is actually what the |
@rmg |
rmg
commented
Mar 23, 2017
@fhalde it seems like this might end up being a lost cause then, since a solution that supports prototypes might end up being more complicated than just leaving things as they are :-( |
fhalde
commented
Mar 26, 2017
Agree too. I'll close it ? @rmg |
rmg
commented
Mar 27, 2017
@fhalde agreed, may as well close this. FWIW, I liked the idea behind the change. |
jasnell
commented
Mar 27, 2017
Closing based on the discussion. |
In
fs.js, thegetOptioncan be improved to populate the option object with the passed in defaultOptions if the default options are not there in the original option object. This way we don't have to dooptions.flag || 'w'etc.For example in the
fs.writeFileSimilar patterns are present in few more parts of the
fsmoduleChecklist
make -j4 test(UNIX), orvcbuild test(Windows) passesAffected core subsystem(s)
fs, test