postman-url-encoder
Advanced tools
Comparing version 2.1.0-beta.2 to 2.1.0-beta.3
146
index.js
@@ -29,8 +29,11 @@ /** | ||
COLON = ':', | ||
BACK_SLASH = '\\', | ||
DOUBLE_SLASH = '//', | ||
DOUBLE_BACK_SLASH = '\\\\', | ||
STRING = 'string', | ||
OBJECT = 'object', | ||
FUNCTION = 'function', | ||
DOUBLE_SLASH = '//', | ||
DEFAULT_PROTOCOL = 'http', | ||
PATH_SEPARATOR = '/', | ||
QUERY_SEPARATOR = '?', | ||
@@ -40,2 +43,5 @@ SEARCH_SEPARATOR = '#', | ||
// @note this regular expression is referred from Node.js URL parser | ||
PROTOCOL_RE = /^[a-z0-9.+-]+:(?:\/\/|\\\\)./i, | ||
/** | ||
@@ -58,2 +64,38 @@ * Protocols that always contain a // bit. | ||
/** | ||
* Returns stringified URL from Url object but only includes parts till given | ||
* part name. | ||
* | ||
* @example | ||
* var url = 'http://postman.com/foo?q=v#hash'; | ||
* getUrlTill(toNodeUrl(url), 'host') | ||
* // returns 'http://postman.com' | ||
* | ||
* @private | ||
* @param {Object} url base URL | ||
* @param {String} [urlPart='query'] one of ['host', 'pathname', 'query'] | ||
*/ | ||
function getUrlTill (url, urlPart) { | ||
var result = ''; | ||
if (url.protocol) { | ||
result += url.protocol + DOUBLE_SLASH; | ||
} | ||
if (url.auth) { | ||
result += url.auth + AUTH_CREDENTIALS_SEPARATOR; | ||
} | ||
result += url.host || E; | ||
if (urlPart === 'host') { return result; } | ||
result += url.pathname; | ||
if (urlPart === 'pathname') { return result; } | ||
// urlPart must be query at this point | ||
return result + (url.search || E); | ||
} | ||
/** | ||
* Percent-encode the given string using QUERY_ENCODE_SET. | ||
@@ -270,2 +312,103 @@ * | ||
/** | ||
* Resolves a relative URL with respect to given base URL. | ||
* This is a replacement method for Node's url.resolve() which is compatible | ||
* with URL object generated by toNodeUrl(). | ||
* | ||
* @example | ||
* // returns 'http://postman.com/baz' | ||
* resolveNodeUrl('http://postman.com/foo/bar', '/baz') | ||
* | ||
* @param {Object|String} base URL string or toNodeUrl() object | ||
* @param {String} relative Relative URL to resolve | ||
* @returns {String} Resolved URL | ||
*/ | ||
function resolveNodeUrl (base, relative) { | ||
// normalize arguments | ||
typeof base === STRING && (base = toNodeUrl(base)); | ||
typeof relative !== STRING && (relative = E); | ||
// bail out if base is not an object | ||
if (!(base && typeof base === OBJECT)) { | ||
return relative; | ||
} | ||
var i, | ||
ii, | ||
index, | ||
baseHref, | ||
relative_0, | ||
relative_01, | ||
basePathname, | ||
requiredProps = ['protocol', 'auth', 'host', 'pathname', 'search', 'href']; | ||
// bail out if base is not like Node url object | ||
for (i = 0, ii = requiredProps.length; i < ii; i++) { | ||
if (!base.hasOwnProperty(requiredProps[i])) { | ||
return relative; | ||
} | ||
} | ||
// cache base.href and base.pathname | ||
baseHref = base.href; | ||
basePathname = base.pathname; | ||
// cache relative's first two chars | ||
relative_0 = relative.slice(0, 1); | ||
relative_01 = relative.slice(0, 2); | ||
// @note relative can be one of | ||
// #1 empty string | ||
// #2 protocol relative, starts with // or \\ | ||
// #3 path relative, starts with / or \ | ||
// #4 just query or hash, starts with ? or # | ||
// #5 absolute URL, starts with :// or :\\ | ||
// #6 free from path, with or without query and hash | ||
// #1 empty string | ||
if (!relative) { | ||
// return base as it is if there is no hash | ||
if ((index = baseHref.indexOf(SEARCH_SEPARATOR)) === -1) { | ||
return baseHref; | ||
} | ||
// else, return base without the hash | ||
return baseHref.slice(0, index); | ||
} | ||
// #2 protocol relative, starts with // or \\ | ||
// @note \\ is not converted to // | ||
if (relative_01 === DOUBLE_SLASH || relative_01 === DOUBLE_BACK_SLASH) { | ||
return base.protocol + relative; | ||
} | ||
// #3 path relative, starts with / or \ | ||
// @note \(s) are not converted to / | ||
if (relative_0 === PATH_SEPARATOR || relative_0 === BACK_SLASH) { | ||
return getUrlTill(base, 'host') + relative; | ||
} | ||
// #4 just hash, starts with # | ||
if (relative_0 === SEARCH_SEPARATOR) { | ||
return getUrlTill(base, 'query') + relative; | ||
} | ||
// #4 just query, starts with ? | ||
if (relative_0 === QUERY_SEPARATOR) { | ||
return getUrlTill(base, 'pathname') + relative; | ||
} | ||
// #5 absolute URL, starts with :// or :\\ | ||
// @note :\\ is not converted to :// | ||
if (PROTOCOL_RE.test(relative)) { | ||
return relative; | ||
} | ||
// #6 free from path, with or without query and hash | ||
// remove last path segment form base path | ||
basePathname = basePathname.slice(0, basePathname.lastIndexOf(PATH_SEPARATOR) + 1); | ||
return getUrlTill(base, 'host') + basePathname + relative; | ||
} | ||
/** | ||
* Converts URL string into Node.js compatible Url object using the v1 encoder. | ||
@@ -285,4 +428,5 @@ * | ||
toNodeUrl, | ||
resolveNodeUrl, | ||
toLegacyNodeUrl, | ||
encodeQueryString | ||
}; |
@@ -5,3 +5,3 @@ { | ||
"author": "Postman Labs <help@getpostman.com>", | ||
"version": "2.1.0-beta.2", | ||
"version": "2.1.0-beta.3", | ||
"license": "Apache-2.0", | ||
@@ -31,3 +31,3 @@ "keywords": [ | ||
"dependencies": { | ||
"postman-collection": "3.6.0-beta.2", | ||
"postman-collection": "3.6.0-beta.3", | ||
"punycode": "^2.1.1" | ||
@@ -34,0 +34,0 @@ }, |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
176605
36
4024
+ Addedpostman-collection@3.6.0-beta.3(transitive)
- Removedpostman-collection@3.6.0-beta.2(transitive)