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

feature: resolve parameters in Page.url #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

0ff
Copy link

@0ff 0ff commented Jul 6, 2016

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”.

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”.
@emadalam
Copy link
Owner

emadalam commented Jan 1, 2017

@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 emadalam mentioned this pull request Jan 1, 2017
2 tasks
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