2017-01-20 21:51:04 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
function formatApiLink(...values) {
|
|
|
|
let parts = [];
|
|
|
|
for (let value of values) {
|
|
|
|
if (value.constructor === Object) {
|
|
|
|
// assert this is the last piece
|
|
|
|
let variableParts = [];
|
|
|
|
for (let key of Object.keys(value)) {
|
|
|
|
if (value[key]) {
|
|
|
|
variableParts.push(
|
|
|
|
key + '=' + encodeURIComponent(value[key].toString()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (variableParts.length) {
|
|
|
|
parts.push('?' + variableParts.join('&'));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
parts.push(encodeURIComponent(value.toString()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return '/' + parts.join('/');
|
|
|
|
}
|
|
|
|
|
|
|
|
function escapeParam(text) {
|
2017-01-21 00:12:28 +01:00
|
|
|
return encodeURIComponent(text);
|
2017-01-20 21:51:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function unescapeParam(text) {
|
2017-01-21 00:12:28 +01:00
|
|
|
return decodeURIComponent(text);
|
2017-01-20 21:51:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function formatClientLink(...values) {
|
|
|
|
let parts = [];
|
|
|
|
for (let value of values) {
|
|
|
|
if (value.constructor === Object) {
|
|
|
|
// assert this is the last piece
|
|
|
|
let variableParts = [];
|
|
|
|
for (let key of Object.keys(value)) {
|
|
|
|
if (value[key]) {
|
|
|
|
variableParts.push(
|
|
|
|
key + '=' + escapeParam(value[key].toString()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (variableParts.length) {
|
|
|
|
parts.push(variableParts.join(';'));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
parts.push(escapeParam(value.toString()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return '/' + parts.join('/');
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
formatClientLink: formatClientLink,
|
|
|
|
formatApiLink: formatApiLink,
|
|
|
|
escapeParam: escapeParam,
|
|
|
|
unescapeParam: unescapeParam,
|
|
|
|
};
|