all files / TypeScript.NET/source/System/Uri/ Uri.js

97.94% Statements 190/194
95.95% Branches 166/173
100% Functions 30/30
98.42% Lines 187/190
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261                      27× 27× 26× 26× 26× 22× 22× 22× 18× 22× 22×       22× 22× 22× 22× 22×           13×         22×   22×   22×                             12× 12× 10× 12× 84× 84× 31×   12×   59× 59× 19× 18× 18×   18× 17×     40× 39×     27× 26× 19×       56× 38× 37×   54× 54× 18× 18× 14× 18×   54×   72×   50×   50× 50×       28× 28×     28×     28× 27× 27×   15× 12× 12× 12×   12× 12×   12× 12×         10× 10×   10× 10×   10× 10×        
/*!
 * @author electricessence / https://github.com/electricessence/
 * Licensing: MIT https://github.com/electricessence/TypeScript.NET/blob/master/LICENSE.md
 * Based on: https://en.wikipedia.org/wiki/Uniform_Resource_Identifier
 */
(function (factory) {
    Eif (typeof module === 'object' && typeof module.exports === 'object') {
        var v = factory(require, exports); Iif (v !== undefined) module.exports = v;
    }
    else if (typeof define === 'function' && define.amd) {
        define(["require", "exports", "../Types", "./QueryParams", "./Scheme", "../Text/Utility", "../Exceptions/ArgumentException", "../Exceptions/ArgumentOutOfRangeException"], factory);
    }
})(function (require, exports) {
    "use strict";
    var Types_1 = require("../Types");
    var QueryParams = require("./QueryParams");
    var Scheme = require("./Scheme");
    var Utility_1 = require("../Text/Utility");
    var ArgumentException_1 = require("../Exceptions/ArgumentException");
    var ArgumentOutOfRangeException_1 = require("../Exceptions/ArgumentOutOfRangeException");
    var VOID0 = void (0);
    var Uri = (function () {
        function Uri(scheme, userInfo, host, port, path, query, fragment) {
            var _ = this;
            _.scheme = getScheme(scheme) || null;
            _.userInfo = userInfo || null;
            _.host = host || null;
            _.port = getPort(port);
            _.authority = _.getAuthority() || null;
            _.path = path || null;
            if (!Types_1.Type.isString(query))
                query = QueryParams.encode(query);
            _.query = formatQuery(query) || null;
            Object.freeze(_.queryParams
                = _.query
                    ? QueryParams.parseToMap(_.query)
                    : {});
            _.pathAndQuery = _.getPathAndQuery() || null;
            _.fragment = formatFragment(fragment) || null;
            _.absoluteUri = _.getAbsoluteUri();
            _.baseUri = _.absoluteUri.replace(/[?#].*/, '');
            Object.freeze(_);
        }
        Uri.prototype.equals = function (other) {
            return this === other || this.absoluteUri == Uri.toString(other);
        };
        Uri.from = function (uri, defaults) {
            var u = (!uri || Types_1.Type.isString(uri))
                ? Uri.parse(uri) : uri;
            return new Uri(u.scheme || defaults && defaults.scheme, u.userInfo || defaults && defaults.userInfo, u.host || defaults && defaults.host, isNaN(u.port) ? defaults && defaults.port : u.port, u.path || defaults && defaults.path, u.query || defaults && defaults.query, u.fragment || defaults && defaults.fragment);
        };
        Uri.parse = function (url, throwIfInvalid) {
            if (throwIfInvalid === void 0) { throwIfInvalid = true; }
            var result = null;
            var ex = tryParse(url, function (out) { result = out; });
            if (throwIfInvalid && ex)
                throw ex;
            return result;
        };
        Uri.tryParse = function (url, out) {
            return !tryParse(url, out);
        };
        Uri.copyOf = function (map) {
            return copyUri(map);
        };
        Uri.prototype.copyTo = function (map) {
            return copyUri(this, map);
        };
        Uri.prototype.updateQuery = function (query) {
            var map = this.toMap();
            map.query = query;
            return Uri.from(map);
        };
        Uri.prototype.getAbsoluteUri = function () {
            return uriToString(this);
        };
        Uri.prototype.getAuthority = function () {
            return getAuthority(this);
        };
        Uri.prototype.getPathAndQuery = function () {
            return getPathAndQuery(this);
        };
        Object.defineProperty(Uri.prototype, "pathSegments", {
            get: function () {
                return this.path.match(/^[/]|[^/]*[/]|[^/]+$/g);
            },
            enumerable: true,
            configurable: true
        });
        Uri.prototype.toMap = function () {
            return this.copyTo({});
        };
        Uri.prototype.toString = function () {
            return this.absoluteUri;
        };
        Uri.toString = function (uri) {
            return uri instanceof Uri
                ? uri.absoluteUri
                : uriToString(uri);
        };
        Uri.getAuthority = function (uri) {
            return getAuthority(uri);
        };
        return Uri;
    }());
    exports.Uri = Uri;
    (function (Fields) {
        Fields[Fields["scheme"] = 0] = "scheme";
        Fields[Fields["userInfo"] = 1] = "userInfo";
        Fields[Fields["host"] = 2] = "host";
        Fields[Fields["port"] = 3] = "port";
        Fields[Fields["path"] = 4] = "path";
        Fields[Fields["query"] = 5] = "query";
        Fields[Fields["fragment"] = 6] = "fragment";
    })(exports.Fields || (exports.Fields = {}));
    var Fields = exports.Fields;
    Object.freeze(Fields);
    function copyUri(from, to) {
        var i = 0, field;
        if (!to)
            to = {};
        while (field = Fields[i++]) {
            var value = from[field];
            if (value)
                to[field] = value;
        }
        return to;
    }
    var SLASH = '/', SLASH2 = '//', QM = QueryParams.Separator.Query, HASH = '#', EMPTY = '', AT = '@';
    function getScheme(scheme) {
        var s = scheme;
        if (Types_1.Type.isString(s)) {
            if (!s)
                return null;
            s = Utility_1.trim(s).toLowerCase().replace(/[^a-z0-9+.-]+$/g, EMPTY);
            Iif (!s)
                return null;
            if (Scheme.isValid(s))
                return s;
        }
        else {
            if (s === null || s === undefined)
                return s;
        }
        throw new ArgumentOutOfRangeException_1.ArgumentOutOfRangeException('scheme', scheme, 'Invalid scheme.');
    }
    function getPort(port) {
        if (port === 0)
            return port;
        if (!port)
            return null;
        var p;
        if (Types_1.Type.isNumber(port, true)) {
            p = port;
            if (p >= 0 && isFinite(p))
                return p;
        }
        else if (Types_1.Type.isString(port) && (p = parseInt(port)) && !isNaN(p)) {
            return getPort(p);
        }
        throw new ArgumentException_1.ArgumentException("port", "invalid value");
    }
    function getAuthority(uri) {
        if (!uri.host) {
            if (uri.userInfo)
                throw new ArgumentException_1.ArgumentException('host', 'Cannot include user info when there is no host.');
            if (Types_1.Type.isNumber(uri.port, false))
                throw new ArgumentException_1.ArgumentException('host', 'Cannot include a port when there is no host.');
        }
        var result = uri.host || EMPTY;
        if (result) {
            if (uri.userInfo)
                result = uri.userInfo + AT + result;
            if (!isNaN(uri.port))
                result += ':' + uri.port;
            result = SLASH2 + result;
        }
        return result;
    }
    function formatQuery(query) {
        return query && ((query.indexOf(QM) !== 0 ? QM : EMPTY) + query);
    }
    function formatFragment(fragment) {
        return fragment && ((fragment.indexOf(HASH) !== 0 ? HASH : EMPTY) + fragment);
    }
    function getPathAndQuery(uri) {
        var path = uri.path, query = uri.query;
        return EMPTY
            + (path || EMPTY)
            + (formatQuery(query) || EMPTY);
    }
    function uriToString(uri) {
        var scheme = getScheme(uri.scheme), authority = getAuthority(uri), pathAndQuery = getPathAndQuery(uri), fragment = formatFragment(uri.fragment);
        var part1 = EMPTY
            + ((scheme && (scheme + ':')) || EMPTY)
            + (authority || EMPTY);
        var part2 = EMPTY
            + (pathAndQuery || EMPTY)
            + (fragment || EMPTY);
        if (part1 && part2 && scheme && !authority)
            throw new ArgumentException_1.ArgumentException('authority', "Cannot format schemed Uri with missing authority.");
        if (part1 && pathAndQuery && pathAndQuery.indexOf(SLASH) !== 0)
            part2 = SLASH + part2;
        return part1 + part2;
    }
    function tryParse(url, out) {
        if (!url)
            return new ArgumentException_1.ArgumentException('url', 'Nothing to parse.');
        var i, result = {};
        i = url.indexOf(HASH);
        if (i != -1) {
            result.fragment = url.substring(i + 1) || VOID0;
            url = url.substring(0, i);
        }
        i = url.indexOf(QM);
        if (i != -1) {
            result.query = url.substring(i + 1) || VOID0;
            url = url.substring(0, i);
        }
        i = url.indexOf(SLASH2);
        if (i != -1) {
            var scheme = Utility_1.trim(url.substring(0, i)), c = /:$/;
            if (!c.test(scheme))
                return new ArgumentException_1.ArgumentException('url', 'Scheme was improperly formatted');
            scheme = Utility_1.trim(scheme.replace(c, EMPTY));
            try {
                result.scheme = getScheme(scheme) || VOID0;
            }
            catch (ex) {
                return ex;
            }
            url = url.substring(i + 2);
        }
        i = url.indexOf(SLASH);
        if (i != -1) {
            result.path = url.substring(i);
            url = url.substring(0, i);
        }
        i = url.indexOf(AT);
        if (i != -1) {
            result.userInfo = url.substring(0, i) || VOID0;
            url = url.substring(i + 1);
        }
        i = url.indexOf(':');
        if (i != -1) {
            var port = parseInt(Utility_1.trim(url.substring(i + 1)));
            if (isNaN(port))
                return new ArgumentException_1.ArgumentException('url', 'Port was invalid.');
            result.port = port;
            url = url.substring(0, i);
        }
        url = Utility_1.trim(url);
        if (url)
            result.host = url;
        out(copyUri(result));
        return null;
    }
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.default = Uri;
});
//# sourceMappingURL=Uri.js.map