2017-07-18 22:10:57 +00:00
|
|
|
// @flow
|
2016-11-10 05:56:23 +00:00
|
|
|
import * as util from './misc.js';
|
2016-08-15 17:04:36 +00:00
|
|
|
|
2017-07-18 23:38:19 +00:00
|
|
|
type Parameter = Object & {
|
|
|
|
name: string,
|
|
|
|
value: string
|
|
|
|
}
|
|
|
|
|
2016-11-10 02:45:43 +00:00
|
|
|
/** Join a URL with a querystring */
|
2017-07-18 22:10:57 +00:00
|
|
|
export function joinUrl (url: string, qs: string): string {
|
2016-07-22 20:02:17 +00:00
|
|
|
if (!qs) {
|
|
|
|
return url;
|
|
|
|
}
|
2016-11-10 02:45:43 +00:00
|
|
|
|
2017-03-28 22:45:23 +00:00
|
|
|
if (!url) {
|
|
|
|
return qs;
|
|
|
|
}
|
|
|
|
|
2017-03-01 21:15:56 +00:00
|
|
|
const [base, ...hashes] = url.split('#');
|
|
|
|
|
2016-11-10 02:45:43 +00:00
|
|
|
// TODO: Make this work with URLs that have a #hash component
|
2017-03-01 21:15:56 +00:00
|
|
|
const baseUrl = base || '';
|
|
|
|
const joiner = getJoiner(base);
|
|
|
|
const hash = hashes.length ? `#${hashes.join('#')}` : '';
|
|
|
|
return `${baseUrl}${joiner}${qs}${hash}`;
|
|
|
|
}
|
|
|
|
|
2017-07-18 22:10:57 +00:00
|
|
|
export function extractFromUrl (url: string): string {
|
2017-03-28 22:45:23 +00:00
|
|
|
if (!url) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2017-03-01 21:15:56 +00:00
|
|
|
// NOTE: This only splits on first ? sign. '1=2=3' --> ['1', '2=3']
|
|
|
|
const things = url.split('?');
|
|
|
|
if (things.length === 1) {
|
|
|
|
return '';
|
|
|
|
} else {
|
|
|
|
const qsWithHash = things.slice(1).join('?');
|
|
|
|
return qsWithHash.replace(/#.*/, '');
|
|
|
|
}
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-07-14 22:48:56 +00:00
|
|
|
|
2017-07-18 22:10:57 +00:00
|
|
|
export function getJoiner (url: string): string {
|
2016-11-10 02:45:43 +00:00
|
|
|
url = url || '';
|
|
|
|
return url.indexOf('?') === -1 ? '?' : '&';
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Build a querystring param out of a name/value pair */
|
2017-07-18 22:10:57 +00:00
|
|
|
export function build (param: Parameter, strict: boolean = true): string {
|
2016-07-22 20:02:17 +00:00
|
|
|
// Skip non-name ones in strict mode
|
|
|
|
if (strict && !param.name) {
|
|
|
|
return '';
|
|
|
|
}
|
2016-07-14 22:48:56 +00:00
|
|
|
|
2017-01-11 20:58:37 +00:00
|
|
|
// Cast number values to strings
|
|
|
|
if (typeof param.value === 'number') {
|
|
|
|
param.value += '';
|
|
|
|
}
|
|
|
|
|
2016-07-22 20:02:17 +00:00
|
|
|
if (!strict || param.value) {
|
2017-01-11 20:58:37 +00:00
|
|
|
// Don't encode ',' in values
|
|
|
|
const value = util.flexibleEncodeComponent(param.value || '').replace(/%2C/gi, ',');
|
2016-09-24 03:26:24 +00:00
|
|
|
const name = util.flexibleEncodeComponent(param.name || '');
|
2016-11-17 21:55:48 +00:00
|
|
|
|
2017-03-03 20:09:08 +00:00
|
|
|
return `${name}=${value}`;
|
2016-07-14 22:48:56 +00:00
|
|
|
} else {
|
2016-09-24 03:26:24 +00:00
|
|
|
return util.flexibleEncodeComponent(param.name);
|
2016-07-14 22:48:56 +00:00
|
|
|
}
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-07-14 22:48:56 +00:00
|
|
|
|
2016-07-22 20:02:17 +00:00
|
|
|
/**
|
2016-11-10 02:45:43 +00:00
|
|
|
* Build a querystring from a list of name/value pairs
|
2016-07-22 20:02:17 +00:00
|
|
|
* @param parameters
|
|
|
|
* @param strict allow empty names and values
|
|
|
|
*/
|
2017-07-18 22:10:57 +00:00
|
|
|
export function buildFromParams (
|
|
|
|
parameters: Array<Parameter>,
|
|
|
|
strict: boolean = true
|
|
|
|
): string {
|
2016-07-20 23:16:28 +00:00
|
|
|
let items = [];
|
2016-12-08 17:20:10 +00:00
|
|
|
|
2016-09-24 03:26:24 +00:00
|
|
|
for (const param of parameters) {
|
2016-10-02 20:57:00 +00:00
|
|
|
let built = build(param, strict);
|
2016-07-20 23:16:28 +00:00
|
|
|
|
|
|
|
if (!built) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
items.push(built);
|
2016-07-14 22:48:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return items.join('&');
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-07-22 20:02:17 +00:00
|
|
|
|
|
|
|
/**
|
2016-11-10 02:45:43 +00:00
|
|
|
* Deconstruct a querystring to name/value pairs
|
2016-07-22 20:02:17 +00:00
|
|
|
* @param qs
|
|
|
|
* @param strict allow empty names and values
|
|
|
|
*/
|
2017-07-18 22:10:57 +00:00
|
|
|
export function deconstructToParams (qs: ?string, strict: boolean = true): Array<Parameter> {
|
|
|
|
const pairs: Array<Parameter> = [];
|
|
|
|
|
2016-12-01 18:48:49 +00:00
|
|
|
if (!qs) {
|
2017-07-18 22:10:57 +00:00
|
|
|
return pairs;
|
2016-11-22 19:42:10 +00:00
|
|
|
}
|
|
|
|
|
2016-07-22 20:02:17 +00:00
|
|
|
const stringPairs = qs.split('&');
|
|
|
|
|
|
|
|
for (let stringPair of stringPairs) {
|
2017-02-27 22:54:56 +00:00
|
|
|
// NOTE: This only splits on first equals sign. '1=2=3' --> ['1', '2=3']
|
|
|
|
const [encodedName, ...encodedValues] = stringPair.split('=');
|
|
|
|
const encodedValue = encodedValues.join('=');
|
2016-07-22 20:02:17 +00:00
|
|
|
|
2016-11-17 18:45:54 +00:00
|
|
|
let name = '';
|
|
|
|
try {
|
2016-11-17 18:57:18 +00:00
|
|
|
name = decodeURIComponent(encodedName || '');
|
2016-11-17 18:45:54 +00:00
|
|
|
} catch (e) {
|
2016-11-17 21:36:55 +00:00
|
|
|
// Just leave it
|
|
|
|
name = encodedName;
|
2016-11-17 18:45:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let value = '';
|
|
|
|
try {
|
2016-11-17 18:57:18 +00:00
|
|
|
value = decodeURIComponent(encodedValue || '');
|
2016-11-17 18:45:54 +00:00
|
|
|
} catch (e) {
|
2016-11-17 21:36:55 +00:00
|
|
|
// Just leave it
|
|
|
|
value = encodedValue;
|
2016-11-17 18:45:54 +00:00
|
|
|
}
|
2016-07-22 20:02:17 +00:00
|
|
|
|
|
|
|
if (strict && !name) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
pairs.push({name, value});
|
|
|
|
}
|
|
|
|
|
|
|
|
return pairs;
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|