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

resolve parameters in Page.url #19

Open
2 tasks
emadalam opened this issue Jan 1, 2017 · 1 comment
Open
2 tasks

resolve parameters in Page.url #19

emadalam opened this issue Jan 1, 2017 · 1 comment

Comments

@emadalam
Copy link
Owner

emadalam commented Jan 1, 2017

From one of the PRs #12

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

  • evaluate configuration format
  • implement a stripped down version without using path-to-regexp

Handy code snippet

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

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 += '?' + q;
   }
  return urlBuffer;
 }

This felt like an easy fix to get rid of reg-exp, if am wrong care to specify why the reg-exp needed in the first place?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants