atvjs icon indicating copy to clipboard operation
atvjs copied to clipboard

feature: resolve parameters in Page.url

Open 0ff opened this issue 8 years ago • 1 comments

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” with options = {urlParams: {test: true}} to get “http://httpbin.org/get?test=true”.

0ff avatar Jul 06 '16 08:07 0ff

@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('');
}

emadalam avatar Jan 01 '17 13:01 emadalam