# all files / TypeScript.NET/dist/commonjs/System/Uri/ Uri.js

70.76% Statements 121/171
54% Branches 81/150
67.74% Functions 21/31
74.29% Lines 104/140

    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

    15×

    1×

    1×
    1×
    1×
    1×
    1×
    1×

    1×
    1×
    1×

    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×

    1×

    1×

    1×

    1×

    1×
    1×

    1×

    1×
    1×
    1×

    1×
    1×

    1×

    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×

    1×
    1×
    1×
    1×

    1×
    1×
    7×
    7×

    1×

    1×

    1×
    2×
    2×

    2×

    2×

    1×
    2×
    2×
    2×

    2×
    2×

    2×

    1×
    3×

    1×
    2×

    1×
    2×

    2×

    1×
    1×

    1×

    1×
    1×
    1×

    1×
    1×

    1×
    1×
    1×
    1×

    1×
    1×

    1×
    1×
    1×
    1×

    1×
    1×

    1×
    1×

    1×
    1×
    1×
    1×

     /*!
     * @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
     */
    'use strict';

    var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { Eif (protoProps) defineProperties(Constructor.prototype, protoProps); Eif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

    function _classCallCheck(instance, Constructor) { Iif (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

    var Types_1 = require('../Types');
    var QueryParams = require('../Uri/QueryParams');
    var Utility_1 = require('../Text/Utility');
    var Scheme_1 = require('../Uri/Scheme');
    var ArgumentException_1 = require('../Exceptions/ArgumentException');
    var ArgumentOutOfRangeException_1 = require('../Exceptions/ArgumentOutOfRangeException');

    var Uri = function () {
        function Uri(scheme, userInfo, host, port, path, query, fragment) {
            _classCallCheck(this, Uri);

            var _ = this;
            _.scheme = getScheme(scheme) || null;
            _.userInfo = userInfo || null;
            _.host = host || null;
            _.port = port || null;
            _.authority = _.getAuthority() || null;
            _.path = path || null;
            Iif (!Types_1.default.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(_);
        }

        _createClass(Uri, [{
            key: 'equals',
            value: function equals(other) {
                return this === other || this.absoluteUri == Uri.toString(other);
            }
        }, {
            key: 'copyTo',
            value: function copyTo(map) {
                return copyUri(this, map);
            }
        }, {
            key: 'updateQuery',
            value: function updateQuery(query) {
                var map = this.toMap();
                map.query = query;
                return Uri.from(map);
            }
        }, {
            key: 'getAbsoluteUri',
            value: function getAbsoluteUri() {
                return uriToString(this);
            }
        }, {
            key: 'getAuthority',
            value: function getAuthority() {
                return _getAuthority(this);
            }
        }, {
            key: 'getPathAndQuery',
            value: function getPathAndQuery() {
                return _getPathAndQuery(this);
            }
        }, {
            key: 'toMap',
            value: function toMap() {
                return this.copyTo({});
            }
        }, {
            key: 'toString',
            value: function toString() {
                return this.absoluteUri;
            }
        }, {
            key: 'pathSegments',
            get: function get() {
                return this.path.match(/^[/]|[^/]*[/]|[^/]+$/g);
            }
        }], [{
            key: 'from',
            value: function from(uri, defaults) {
                var u = !uri || Types_1.default.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);
            }
        }, {
            key: 'parse',
            value: function parse(url) {
                var throwIfInvalid = arguments.length <= 1 || arguments[1] === undefined ? true : arguments[1];

                var result = null;
                var ex = _tryParse(url, function (out) {
                    result = out;
                });
                Iif (throwIfInvalid && ex) throw ex;
                return result;
            }
        }, {
            key: 'tryParse',
            value: function tryParse(url, out) {
                return !_tryParse(url, out);
            }
        }, {
            key: 'copyOf',
            value: function copyOf(map) {
                return copyUri(map);
            }
        }, {
            key: 'toString',
            value: function toString(uri) {
                return uri instanceof Uri ? uri.absoluteUri : uriToString(uri);
            }
        }, {
            key: 'getAuthority',
            value: function getAuthority(uri) {
                return _getAuthority(uri);
            }
        }]);

        return Uri;
    }();

    Object.defineProperty(exports, "__esModule", { value: true });
    exports.default = 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;
        Eif (!to) to = {};
        while (field = Fields[i++]) {
            var value = from[field];
            if (value) to[field] = value;
        }
        return to;
    }
    var SLASH = '/',
        SLASH2 = '//',
        QM = '?',
        HASH = '#',
        EMPTY = '',
        AT = '@';
    function getScheme(scheme) {
        var s = scheme;
        Iif (Types_1.default.isString(s)) {
            if (!s) return undefined;
            s = Scheme_1.default[Utility_1.trim(s).toLowerCase().replace(/[^a-z0-9+.-]+$/g, EMPTY)];
            if (isNaN(s)) throw new ArgumentOutOfRangeException_1.default('scheme', scheme, 'Invalid scheme.');
        }
        Iif (Types_1.default.isNumber(s, false)) {
            s = Scheme_1.default[s];
            if (!s) throw new ArgumentOutOfRangeException_1.default('scheme', scheme, 'Invalid scheme.');
            return s;
        }
        return undefined;
    }
    function _getAuthority(uri) {
        Eif (!uri.host) {
            Iif (uri.userInfo) throw new ArgumentException_1.default('host', 'Cannot include user info when there is no host.');
            Iif (Types_1.default.isNumber(uri.port, false)) throw new ArgumentException_1.default('host', 'Cannot include a port when there is no host.');
        }
        var result = uri.host || EMPTY;
        Iif (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) == -1 ? QM : EMPTY) + query;
    }
    function formatFragment(fragment) {
        return fragment && (fragment.indexOf(HASH) == -1 ? HASH : EMPTY) + fragment;
    }
    function _getPathAndQuery(uri) {
        var path = uri.path,
            query = uri.query;
        return EMPTY + (path && (path.indexOf(SLASH) == -1 ? SLASH : EMPTY) + path || EMPTY) + (formatQuery(query) || EMPTY);
    }
    function uriToString(uri) {
        var scheme = getScheme(uri.scheme),
            authority = _getAuthority(uri),
            pathAndQuery = _getPathAndQuery(uri),
            fragment = formatFragment(uri.fragment);
        return EMPTY + (scheme && scheme + ':' || EMPTY) + (authority || EMPTY) + (pathAndQuery || EMPTY) + (fragment || EMPTY);
    }
    function _tryParse(url, out) {
        Iif (!url) return new ArgumentException_1.default('url', 'Nothing to parse.');
        var i,
            result = {};
        i = url.indexOf(HASH);
        Iif (i != -1) {
            result.fragment = url.substring(i);
            url = url.substring(0, i);
        }
        i = url.indexOf(QM);
        Eif (i != -1) {
            result.query = url.substring(i);
            url = url.substring(0, i);
        }
        i = url.indexOf(SLASH2);
        Iif (i != -1) {
            var scheme = Utility_1.trim(url.substring(0, i)),
                c = /:$/;
            if (!c.test(scheme)) return new ArgumentException_1.default('url', 'Scheme was improperly formatted');
            scheme = Utility_1.trim(scheme.replace(c, EMPTY));
            try {
                result.scheme = getScheme(scheme) || undefined;
            } catch (ex) {
                return ex;
            }
            url = url.substring(i + 2);
        }
        i = url.indexOf(SLASH);
        Eif (i != -1) {
            result.path = url.substring(i) || undefined;
            url = url.substring(0, i);
        }
        i = url.indexOf(AT);
        Iif (i != -1) {
            result.userInfo = url.substring(0, i) || undefined;
            url = url.substring(i + 1);
        }
        i = url.indexOf(':');
        Iif (i != -1) {
            var port = parseInt(Utility_1.trim(url.substring(i + 1)));
            if (isNaN(port)) return new ArgumentException_1.default('url', 'Port was invalid.');
            result.port = port;
            url = url.substring(0, i);
        }
        url = Utility_1.trim(url);
        Iif (url) result.host = url;
        out(copyUri(result));
        return null;
    }
    //# sourceMappingURL=Uri.js.map