url-lib
Version:
A simple, lightweight string utility for Node and browsers that supports serializing and parsing URLs and query strings.
24 lines (22 loc) • 961 B
JavaScript
export var splitOnQuery = function splitOnQuery() {
var url = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
var favorQuery = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var urlString = "".concat(url);
var queryPos = urlString.indexOf('?'); // If the URL doesn't have a "?" we have to decide how we want to handle the string.
// If favorQuery === true, then we'll assume the entire string is the query string.
// If !favorQuery then we set the queryPos to the end of the string (meaning the
// query string is empty)
if (queryPos < 0 && !favorQuery) {
queryPos = urlString.length;
}
return {
urlPath: urlString.slice(0, queryPos),
queryString: urlString.slice(queryPos + 1)
};
};
export var encode = function encode(str) {
return encodeURIComponent("".concat(str));
};
export var decode = function decode(str) {
return str != null ? decodeURIComponent(str) : '';
};