Open 0ff opened 8 years ago
@0ff This feature is very handy, much appreciate your contributions here 👍 However due to the fact that there is an open issue #17 regarding removal of the unwanted dependency, I'd not want to add path-to-regexp
library as one of the dependency. Do you mind if I port this into an enhancement issue and then implement stripped down version without using the library. Or if you feel, you can create the issue and implement in the Milestone-Enhancements branch. I usually do something like below to achieve what you wanted to do.
function toQueryString(obj) {
return (
_.map(obj, (v, k) => {
if (_.isArray(v)) {
return (_.map(v, (av) => `${k}[]=${av}`)).join('&');
} else {
return `${encodeURIComponent(k)}=${encodeURIComponent(v)}`;
}
})
).join('&');
}
function toUrl(url = '', params = {}) {
let q = toQueryString(params);
let urlBuffer = [url];
if (q) {
urlBuffer.push(/\?.+$/.test(url) ? `&${q}` : `?${q}`);
}
return urlBuffer.join('');
}
This allows to build URLs for pages dynamically based on the
options.urlParams
that are handed over to the page. Uses the same node-module that express uses, so the very same syntax applies. For example use “http://httpbin.org/get?test=:test” withoptions = {urlParams: {test: true}}
to get “http://httpbin.org/get?test=true”.