Skip to content

Commit

Permalink
perf: optimise url handling (#414)
Browse files Browse the repository at this point in the history
  • Loading branch information
baldurh authored and sherwinski committed Jul 16, 2019
1 parent 1a6cf3a commit 8d14dcb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
22 changes: 15 additions & 7 deletions src/constructUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,14 @@ var DEFAULT_OPTIONS = Object.freeze({
});

function constructUrlFromParams(src, params) {
return `${src}?${Object.entries(params)
.map(([a, b]) => `${a}=${encodeURIComponent(b)}`)
.join("&")}`;
const keys = Object.keys(params);
const keysLength = keys.length;
let url = src + "?";
for (let i = 0; i < keysLength; i++) {
const key = keys[i];
url += key + "=" + encodeURIComponent(params[key]) + "&";
}
return url.slice(0, -1);
}

/**
Expand All @@ -123,9 +128,12 @@ function constructUrl(src, longOptions) {
return "";
}

var shortOptions = Object.assign({}, DEFAULT_OPTIONS);
Object.keys(longOptions).forEach(function(key) {
var val = longOptions[key];
const shortOptions = Object.assign({}, DEFAULT_OPTIONS);
const keys = Object.keys(longOptions);
const keysLength = keys.length;
for (let i = 0; i < keysLength; i++) {
let key = keys[i];
let val = longOptions[key];

if (PARAM_EXPANSION[key]) {
key = PARAM_EXPANSION[key];
Expand All @@ -138,7 +146,7 @@ function constructUrl(src, longOptions) {
}

shortOptions[key] = val;
});
}

return constructUrlFromParams(src, shortOptions);
}
Expand Down
26 changes: 16 additions & 10 deletions src/extractQueryParams.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
export default function extractQueryParams(src) {
const [url, query] = src.split("?");
const params = query
? query
.split("&")
.map(x => {
const [key, val] = x.split("=");
return [key, decodeURIComponent(val)];
})
.reduce((obj, [key, val]) => Object.assign(obj, { [key]: val }), {})
: {};
const splitSrc = src.split("?");
const url = splitSrc[0];
const query = splitSrc[1];
if (!query) {
return [url, {}];
}
const paramsPairs = query.split("&");
const params = {};
const len = paramsPairs.length;
for (let i = 0; i < len; i++) {
const param = paramsPairs[i];
const splitParam = param.split("=");
const key = splitParam[0];
const val = splitParam[1];
params[key] = decodeURIComponent(val);
}
return [url, params];
}

0 comments on commit 8d14dcb

Please sign in to comment.