insomnia/app/backend/querystring.js
Gregory Schier 318c35c2cb Move a bunch of stuff to async/await (#39)
* Some minor implementations

* Some more

* Lots more

* Removed 'backend' alias

* removed all promises

* Removed a bunch of module exports stuff

* Some morE'

* Fix

* custom DNS

* Tests for DNS

* bug fix

* Some small adjustments

* Small stuff
2016-10-02 13:57:00 -07:00

77 lines
1.5 KiB
JavaScript

import * as util from './util.js';
export function getJoiner (url) {
url = url || '';
return url.indexOf('?') === -1 ? '?' : '&';
}
export function joinURL (url, qs) {
if (!qs) {
return url;
}
url = url || '';
return url + getJoiner(url) + qs;
}
export function build (param, strict = true) {
// Skip non-name ones in strict mode
if (strict && !param.name) {
return '';
}
if (!strict || param.value) {
const name = util.flexibleEncodeComponent(param.name || '');
const value = util.flexibleEncodeComponent(param.value || '');
return `${name}=${value}`
} else {
return util.flexibleEncodeComponent(param.name);
}
}
/**
*
* @param parameters
* @param strict allow empty names and values
* @returns {string}
*/
export function buildFromParams (parameters, strict = true) {
let items = [];
for (const param of parameters) {
let built = build(param, strict);
if (!built) {
continue;
}
items.push(built);
}
return items.join('&');
}
/**
*
* @param qs
* @param strict allow empty names and values
* @returns {Array}
*/
export function deconstructToParams (qs, strict = true) {
const stringPairs = qs.split('&');
const pairs = [];
for (let stringPair of stringPairs) {
const tmp = stringPair.split('=');
const name = decodeURIComponent(tmp[0] || '');
const value = decodeURIComponent(tmp[1] || '');
if (strict && !name) {
continue;
}
pairs.push({name, value});
}
return pairs;
}