-
-
Notifications
You must be signed in to change notification settings - Fork 16.7k
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
Add app.createRouter function #3623
Conversation
bda0c07
to
d9ea79d
Compare
lib/application.js
Outdated
* @public | ||
*/ | ||
app.createRouter = function (options) { | ||
options = 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.
Since this modifies the options
below, can you make it not modify the incoming options object?
var opts = options || {};
// ...
opts.strict = this.enabled('strict routing')
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.
Done!
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.
It still looks the same. Did you commit the fix?
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.
@dougwilson Yeah, it just doesn't seem like it. Look at the whole file and you'll see its fine.
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 did and it still says options = options || {}
can you write here what you see this line as?
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.
@dougwilson Um yes... But it's not modifying the original object anymore because the code changed below.
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.
Well I mean, technically it is modifying it but only if it returns a falsy value in which case the user didn't actually pass anything into the function and in which case it doesn't matter.
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.
The argument is still being redefined. Just use the code Wes suggested. Do not reassign arguments.
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.
It does matter due to how v8 optimized and compiles functions.
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.
@dougwilson Oh I didn't realize. I fixed it now.
d9ea79d
to
1a48254
Compare
Seems like I broke the tests with my latest commit. I'll look into it. |
1a48254
to
0f645b6
Compare
Fixed. |
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.
Awesome, looking good 👍 I'll fix the coverage reporting on the PR as it should be failing because one of your lines is not covered by a test. You can run npm run test-cov to generate a coverage report locally until I get the CI upload fixed.
lib/application.js
Outdated
caseSensitive: this.enabled('case sensitive routing') | ||
} | ||
|
||
if (options.hasOwnProperty('caseSensitive')) { |
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.
Don't use hasOwnProperty to access the key. Just follow the existing patterns so we don't break people who configure with plain objects
lib/application.js
Outdated
opts.caseSensitive = options.caseSensitive | ||
} | ||
|
||
if (options.hasOwnProperty('strict')) { |
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.
Same hop concern here.
4a4a681
to
0b475bb
Compare
@dougwilson The CI test should be 100% now and I stopped using hasOwnProperty |
test/app.createRouter.js
Outdated
}) | ||
|
||
assert.equal(router.strict, false, 'options param strict did not override app.settings') | ||
assert.equal(router.caseSensitive, false, 'options param caseSensitive did not override app.settings') |
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.
How do you know if this false is coming from the app settings or from the override?
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.
It's true by accident, I'll get it fixed.
0b475bb
to
f9296aa
Compare
lib/application.js
Outdated
* and allows them to be overriden by passing in an options | ||
* object | ||
* | ||
* @param {Object} 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.
We had a PR about this right now, so we should make sure we land it right this time :) The options
argument needs to be optional in the JSDoc here since it's optional in the code 👍
f9296aa
to
b207a68
Compare
lib/application.js
Outdated
*/ | ||
app.createRouter = function (options) { | ||
var opts = { | ||
strict: this.enabled('case sensitive routing'), |
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 think the case sensitive routing
is a typo on this line. Let's fix that and add a test that will actually catch that mistake in the future 👍
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.
After staring at a screen for this many hours eventually you go blind 😆
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.
That's why we review code 😄 happens to everyone
b207a68
to
2d62933
Compare
Fixed the optional argument, tests and me being blind (hopefully). |
|
||
return Router(opts) | ||
} | ||
|
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 is mostly just a style thing, so if there is strong opposition feel free to ignore me, but I find that reducing the code required to achieve the same result often reduces maintenance and understanding costs in the future. For example, this could be written much more succinctly as follows:
app.createRouter = function (options) {
var opts = options || {}
opts.strict = options.strict !== undefined ? options.strict : this.enabled('strict routing')
opts.caseSensitive = options.caseSensitive !== undefined ? options.caseSensitive : this.enabled('case sensitive routing')
return Router(opts)
}
Sometimes more lines of code is a good thing for clarity, but in this case, there are a bunch of lines which can be achieved with one usage of a ternary. This also has the upside of not unnecessarily calling this.enabled
if you pass the option.
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 like that we don't have to call this.enabled if the options are passed in, that's good. But to me this looks like a jumbled mess. It takes multiple seconds to decode what it actually means while undefined checks are really easy to make sense of imo.
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.
Like I said, mostly a style thing :), but maybe you can find a middle ground without the unnecessary calls?
Also, technically it is just the same undefined
checks you had but in a single line. Maybe other people like the more verbose syntax with the same result, I don't know and it probably shouldn't be the focus of any more energy.
app.createRouter returns a router which inherits settings from app.settings unless they're overriden in the options param
2d62933
to
447e756
Compare
@wesleytodd The new code skips uneccessary |
Awesome, this is much more clean and clear. Thanks for the good work! |
Implement PR #3622 but against 4.x master branch instead