Skip to content
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

Allow to pass name/options as third argument in standalone mode #26

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

damianstasik
Copy link

This PR changes how router.add works:

// Name after route path + no need for an array when using more than one middleware
router.add('get', '/user/:id', 'user', isAuthenticated, someOtherMiddleware, (req, res) => {
    res.send('Hello!');
});

// ... but, you still can use array
router.add('get', '/post/:id', 'post', [someOtherMiddleware, (req, res) => {
    res.send('Hello!');
}]);

// Have more than one option? Just pass an object before your middlewares
router.add('get', '/page/:id', {name: 'page', caseSensitive: true}, (req, res) => {
    res.send('Hello!');
});

// Don't want to use a name? Sure, just ditch it
router.add('get', '/category/:id', someOtherMiddleware, (req, res) => {
    res.send('Hello!');
});

Closes #25

}
Router.prototype.add = function (method, path, callback) {
var hasOptions = (typeof callback !== 'function' && !Array.isArray(callback));
var callbacks = [].slice.call(arguments, 2);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slicing arguments prevents the whole function from being optimized by v8. I think that instead we should change the function to expect four arguments (method, path, options, callbacks) and, if options is a function or an array, set callbacks to options (or [options]) and then set options to an empty object.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I know and my first version was using Bluebird workaround, but even then function was marked as not optimized. Instead, I've used what was in the code:
https://github.com/visualfanatic/named-routes/blob/d24b2538213279cca0437224e8afbc30d36c1338/router.js#L312

@alubbe
Copy link
Owner

alubbe commented Aug 29, 2016

Thanks for putting together this PR!
I believe this PR also changes the behaviour of router.add so that it in addition to an array of callbacks/middlewares, it can now take any length of arguments. I think I prefer that behaviour, so now there are two options:

  1. Update the tests to include cases with multiple callbacks without using arrays and update the documentation
  2. Extract this change for a second PR
    What do you think?

@damianstasik
Copy link
Author

Thanks for quick reply! I'll add a few new test cases and update the docs accordingly.

@damianstasik
Copy link
Author

Sorry for the delay, but the missing tests are now included. @alubbe, could you verify if everything is now good? Would it be okay if I would rewrite the tests (add more cases and split existing into smaller groups)?

@alubbe
Copy link
Owner

alubbe commented Oct 3, 2016

Could you please update the docs, as well?
The tests are a left-over from the original fork and are in serious need of a makeover. Aside from new cases and smaller groups, the whole thing should really use describe and it instead of that object notation. So that would be two PRs - one converting the current tests into something more modern and another one adding / rewriting them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants