fs: getOption to populate passed in default values#11630
fs: getOption to populate passed in default values#11630fhalde wants to merge 1 commit intonodejs:masterfrom
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.
|
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). |
|
@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
} |
|
Updated the description to explain what this PR would lead to |
|
@nodejs/collaborators ... thoughts? |
| 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.
|
Why are the tests changed to use |
|
@rmg I can recall that |
Doh! I should have caught that 😊 |
|
@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 |
|
@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 :-( |
|
Agree too. I'll close it ? @rmg |
|
@fhalde agreed, may as well close this. FWIW, I liked the idea behind the change. |
|
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