UNPKG

url-lib

Version:

A simple, lightweight string utility for Node and browsers that supports serializing and parsing URLs and query strings.

39 lines (30 loc) 1.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.decode = exports.encode = exports.splitOnQuery = void 0; 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) }; }; exports.splitOnQuery = splitOnQuery; var encode = function encode(str) { return encodeURIComponent("".concat(str)); }; exports.encode = encode; var decode = function decode(str) { return str != null ? decodeURIComponent(str) : ''; }; exports.decode = decode;