jellyfin-web/dashboard-ui/bower_components/query-string/index.js

54 lines
1.2 KiB
JavaScript
Raw Normal View History

2016-03-14 13:00:18 -07:00
'use strict';
2016-03-14 13:27:35 -07:00
window.queryString = {};
window.queryString.extract = function (maybeUrl) {
return maybeUrl.split('?')[1] || '';
2016-03-14 13:00:18 -07:00
};
2016-03-14 13:27:35 -07:00
window.queryString.parse = function (str) {
2016-03-14 13:00:18 -07:00
if (typeof str !== 'string') {
return {};
}
str = str.trim().replace(/^(\?|#|&)/, '');
if (!str) {
return {};
}
return str.split('&').reduce(function (ret, param) {
var parts = param.replace(/\+/g, ' ').split('=');
2016-03-14 13:27:35 -07:00
var key = parts[0];
var val = parts[1];
2016-03-14 13:00:18 -07:00
key = decodeURIComponent(key);
// missing `=` should be `null`:
// http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
val = val === undefined ? null : decodeURIComponent(val);
if (!ret.hasOwnProperty(key)) {
ret[key] = val;
} else if (Array.isArray(ret[key])) {
ret[key].push(val);
} else {
ret[key] = [ret[key], val];
}
return ret;
}, {});
};
2016-03-14 13:27:35 -07:00
window.queryString.stringify = function (obj) {
2016-03-14 13:00:18 -07:00
return obj ? Object.keys(obj).sort().map(function (key) {
var val = obj[key];
if (Array.isArray(val)) {
2016-03-14 13:27:35 -07:00
return val.sort().map(function (val2) {
return encodeURIComponent(key) + '=' + encodeURIComponent(val2);
2016-03-14 13:00:18 -07:00
}).join('&');
}
2016-03-14 13:27:35 -07:00
return encodeURIComponent(key) + '=' + encodeURIComponent(val);
2016-03-14 13:00:18 -07:00
}).join('&') : '';
};