UNPKG

3.15 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
5 * This code may only be used under the BSD style license found at
6 * http://polymer.github.io/LICENSE.txt
7 * The complete set of authors may be found at
8 * http://polymer.github.io/AUTHORS.txt
9 * The complete set of contributors may be found at
10 * http://polymer.github.io/CONTRIBUTORS.txt
11 * Code distributed by Google as part of the polymer project is also
12 * subject to an additional IP rights grant found at
13 * http://polymer.github.io/PATENTS.txt
14 */
15Object.defineProperty(exports, "__esModule", { value: true });
16const url_1 = require("url");
17const unspecifiedProtocol = '-';
18function parseUrl(url, defaultProtocol = unspecifiedProtocol) {
19 const urlObject = url_1.parse(ensureProtocol(url, defaultProtocol));
20 if (urlObject.protocol &&
21 urlObject.protocol.slice(0, -1) === unspecifiedProtocol) {
22 urlObject.protocol = undefined;
23 urlObject.href =
24 urlObject.href && urlObject.href.slice(unspecifiedProtocol.length + 1);
25 }
26 return urlObject;
27}
28exports.parseUrl = parseUrl;
29function ensureProtocol(url, defaultProtocol) {
30 return url.startsWith('//') ? `${defaultProtocol}:${url}` : url;
31}
32exports.ensureProtocol = ensureProtocol;
33function resolveUrl(baseUrl, targetUrl, defaultProtocol) {
34 const baseProtocol = (url_1.parse(baseUrl).protocol || '').slice(0, -1);
35 const protocol = baseProtocol !== 'file' && baseProtocol || defaultProtocol;
36 return url_1.resolve(ensureProtocol(baseUrl, protocol), ensureProtocol(targetUrl, protocol));
37}
38exports.resolveUrl = resolveUrl;
39function trimLeft(str, char) {
40 let leftEdge = 0;
41 while (str[leftEdge] === char) {
42 leftEdge++;
43 }
44 return str.substring(leftEdge);
45}
46exports.trimLeft = trimLeft;
47class Deferred {
48 constructor() {
49 this.resolved = false;
50 this.rejected = false;
51 this.promise = new Promise((resolve, reject) => {
52 this.resolve = (result) => {
53 if (this.resolved) {
54 throw new Error('Already resolved');
55 }
56 if (this.rejected) {
57 throw new Error('Already rejected');
58 }
59 this.resolved = true;
60 resolve(result);
61 };
62 this.reject = (error) => {
63 if (this.resolved) {
64 throw new Error('Already resolved');
65 }
66 if (this.rejected) {
67 throw new Error('Already rejected');
68 }
69 this.rejected = true;
70 this.error = error;
71 reject(error);
72 };
73 });
74 }
75 toNodeCallback() {
76 return (error, value) => {
77 if (error) {
78 this.reject(error);
79 }
80 else {
81 this.resolve(value);
82 }
83 };
84 }
85}
86exports.Deferred = Deferred;
87function addAll(set1, set2) {
88 for (const val of set2) {
89 set1.add(val);
90 }
91 return set1;
92}
93exports.addAll = addAll;
94//# sourceMappingURL=utils.js.map
\No newline at end of file