UNPKG

4.19 kBJavaScriptView Raw
1"use strict";
2// Copyright (c) Jupyter Development Team.
3// Distributed under the terms of the Modified BSD License.
4var __importDefault = (this && this.__importDefault) || function (mod) {
5 return (mod && mod.__esModule) ? mod : { "default": mod };
6};
7Object.defineProperty(exports, "__esModule", { value: true });
8exports.URLExt = void 0;
9const path_1 = require("path");
10const url_parse_1 = __importDefault(require("url-parse"));
11/**
12 * The namespace for URL-related functions.
13 */
14var URLExt;
15(function (URLExt) {
16 /**
17 * Parse a url into a URL object.
18 *
19 * @param urlString - The URL string to parse.
20 *
21 * @returns A URL object.
22 */
23 function parse(url) {
24 if (typeof document !== 'undefined' && document) {
25 const a = document.createElement('a');
26 a.href = url;
27 return a;
28 }
29 return url_parse_1.default(url);
30 }
31 URLExt.parse = parse;
32 /**
33 * Parse URL and retrieve hostname
34 *
35 * @param url - The URL string to parse
36 *
37 * @return a hostname string value
38 */
39 function getHostName(url) {
40 return url_parse_1.default(url).hostname;
41 }
42 URLExt.getHostName = getHostName;
43 function normalize(url) {
44 return url && parse(url).toString();
45 }
46 URLExt.normalize = normalize;
47 /**
48 * Join a sequence of url components and normalizes as in node `path.join`.
49 *
50 * @param parts - The url components.
51 *
52 * @returns the joined url.
53 */
54 function join(...parts) {
55 const u = url_parse_1.default(parts[0], {});
56 const prefix = `${u.protocol}${u.slashes ? '//' : ''}${u.auth}${u.auth ? '@' : ''}${u.host}`;
57 // If there was a prefix, then the first path must start at the root.
58 const path = path_1.posix.join(`${!!prefix && u.pathname[0] !== '/' ? '/' : ''}${u.pathname}`, ...parts.slice(1));
59 return `${prefix}${path === '.' ? '' : path}`;
60 }
61 URLExt.join = join;
62 /**
63 * Encode the components of a multi-segment url.
64 *
65 * @param url - The url to encode.
66 *
67 * @returns the encoded url.
68 *
69 * #### Notes
70 * Preserves the `'/'` separators.
71 * Should not include the base url, since all parts are escaped.
72 */
73 function encodeParts(url) {
74 return join(...url.split('/').map(encodeURIComponent));
75 }
76 URLExt.encodeParts = encodeParts;
77 /**
78 * Return a serialized object string suitable for a query.
79 *
80 * @param object - The source object.
81 *
82 * @returns an encoded url query.
83 *
84 * #### Notes
85 * Modified version of [stackoverflow](http://stackoverflow.com/a/30707423).
86 */
87 function objectToQueryString(value) {
88 const keys = Object.keys(value).filter(key => key.length > 0);
89 if (!keys.length) {
90 return '';
91 }
92 return ('?' +
93 keys
94 .map(key => {
95 const content = encodeURIComponent(String(value[key]));
96 return key + (content ? '=' + content : '');
97 })
98 .join('&'));
99 }
100 URLExt.objectToQueryString = objectToQueryString;
101 /**
102 * Return a parsed object that represents the values in a query string.
103 */
104 function queryStringToObject(value) {
105 return value
106 .replace(/^\?/, '')
107 .split('&')
108 .reduce((acc, val) => {
109 const [key, value] = val.split('=');
110 if (key.length > 0) {
111 acc[key] = decodeURIComponent(value || '');
112 }
113 return acc;
114 }, {});
115 }
116 URLExt.queryStringToObject = queryStringToObject;
117 /**
118 * Test whether the url is a local url.
119 *
120 * #### Notes
121 * This function returns `false` for any fully qualified url, including
122 * `data:`, `file:`, and `//` protocol URLs.
123 */
124 function isLocal(url) {
125 const { protocol } = parse(url);
126 return ((!protocol || url.toLowerCase().indexOf(protocol) !== 0) &&
127 url.indexOf('/') !== 0);
128 }
129 URLExt.isLocal = isLocal;
130})(URLExt = exports.URLExt || (exports.URLExt = {}));
131//# sourceMappingURL=url.js.map
\No newline at end of file