Last active
August 29, 2015 14:14
-
-
Save NoumanSaleem/782c00d8aa4121adf6c3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function keyToRegExp(key) { | |
return new RegExp(':(' + key + ')(?:\\(([^\\(]+)\\))?'); | |
} | |
function link(url, params) { | |
return Object.keys(params).reduce(function (url, key) { | |
url = url.replace(keyToRegExp(key), params[key]); | |
return url; | |
}, url); | |
} | |
module.exports = link; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"profile": "/user/:id(\\d+)", | |
"profileComment": "/user/:id(\\d+)/comments/:commentId(\\d+)" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var routes = require('routes'); | |
var link = require('link'); | |
link(routes.profile, { id: 123 }); // /user/123 | |
link(routes.profileComment, { id: 123, commentId: 321 }) // /user/123/comments/321 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment