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

Remove curry option and migrate to prototypes #13

Merged
merged 1 commit into from
Jan 15, 2018

Conversation

marcbachmann
Copy link
Contributor

@marcbachmann marcbachmann commented Dec 29, 2017

The changes in here remove the curry option and migrate to prototypes as discussed in #12 and #6.
This pull request currently also includes a change done in #9. So we could do two releases (minor & major) or just a major one.

Api changes

  • The curry option got removed as mentioned in
  • We've migrated the api to prototypes, so the returned router function doesn't exist anymore. Please use router.emit(path) instead
const router = require('nanorouter')({default: '/404'})

// Registers a handler to a specific route
router.on('/path', function (params) {
  return 'something'
})

// Executes a handler, passes `params` as first argument
// Returns the value of the executed handler
const returnValue = router.emit('/path')

// Returns a matched route, falls back to the default route or throws if nothing is found
router.match('/path')

Migration from the old curry option

The curry option was confusing to use and read. Therefore we removed that completely.
Previously it was possible to use the curry option like that:

const router = require('nanorouter')({curry: true})
router.on('/hello/:someparam', function (params) {
  return function () {
    return {hello: params.someparam}
  }
})

const returnvalue = router.emit('/something/world')
// returnvalue === '{"hello": "world"}'

Now that this option doesn't exist anymore, we advise you to migrate to the .match() method as it's much more flexible in case you'll need more advanced parameters.

const router = require('nanorouter')()
router.on('/hello/:someparam', function (params) {
  return {hello: params.someparam}
})

const route = router.match('/something/world')
const returnvalue = route.cb(route.params)
// returnvalue === '{"hello": "world"}'

if you won't have time to migrate your code and want to keep the old behavior, just wrap everything in a self-executing function.

router.on('/hello/:someparam', function (params) {
  return (function () {
    return {hello: params.someparam}
  })()
})

// or wrap the registration of the handler in a function
const router = require('nanorouter')()
function onRoute (path, handler) {
  router.on(path, function (params) {
    var modifiedParameters = {params: params}
    return handler(modifiedParameters)
  })
}

@marcbachmann marcbachmann changed the title Remove curry option and migrate to Prototype Remove curry option and migrate to prototypes Dec 29, 2017
@yoshuawuyts yoshuawuyts self-requested a review January 2, 2018 15:31
Copy link
Member

@yoshuawuyts yoshuawuyts left a comment

Choose a reason for hiding this comment

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

Happy new year! This is great! Left some minor comments that will probably allow us to make the bundle slightly smaller. Other than that this is 👌

index.js Outdated
}
}
}
function assertRoutename (routename) {
Copy link
Member

Choose a reason for hiding this comment

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

I think we should remove this function, and write these statements inline. This has two benefits:

  1. writing duplicate code compresses better. So while having more code in source, it'll probably end up being less code sent over the wire.
  2. this no longer works with static transforms such as unassertify and tinyify.

Hope this makes sense!

Copy link
Contributor Author

@marcbachmann marcbachmann Jan 2, 2018

Choose a reason for hiding this comment

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

Yes, definitely. I didn't think about lz/gzip compression. 👍
I'll just use assert then; that's basically already in use every setup and works better for the reason you mentioned.

index.js Outdated
this.router = wayfarer(opts.default || '/404')
}

Nanorouter.prototype.on = function onRoute (routename, listener) {
Copy link
Member

Choose a reason for hiding this comment

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

The onRoute variable name doesn't do anything in this code. Could we perhaps remove the variable names from all methods, and only keep the method names?

Copy link
Contributor Author

@marcbachmann marcbachmann Jan 2, 2018

Choose a reason for hiding this comment

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

I've just added it to have a named function.
But usually nobody does this for prototype methods, nor do I usually 😁 .
ES6 improved anonymous functions a lot by defining the name automatically for the new syntaxes and let and const. But as we're still using prototypes, I'm good with removing it.
This module is quite small anyways and the function name of the constructor might be good enough.

@@ -0,0 +1,104 @@
var tape = require('tape')
Copy link
Member

Choose a reason for hiding this comment

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

Really digging the tests! 🎉

New api:
```
const router = require('nanorouter')()
router.on('/path', noop) // registers route
router.emit('/path') // executes handler
router.match('/path') // returns match {route: 'path', params: {}, cb: noop}
```
@marcbachmann
Copy link
Contributor Author

marcbachmann commented Jan 2, 2018

@yoshuawuyts Happy new year and thanks for the review.
I just pushed the requested changes.

Copy link
Member

@yoshuawuyts yoshuawuyts left a comment

Choose a reason for hiding this comment

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

nice! - sorry for the delay in reviewing, this is amazing :D

@yoshuawuyts yoshuawuyts merged commit c32c146 into choojs:master Jan 15, 2018
@yoshuawuyts
Copy link
Member

v3.0.0 🎉

@marcbachmann
Copy link
Contributor Author

No problem. Thanks for your work. 🎉

@marcbachmann marcbachmann deleted the remove-curry-option branch January 15, 2018 14:04
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