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

81.72% Statements 76/93
53.7% Branches 29/54
88.24% Functions 15/17
86.42% Lines 70/81

    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

    1×
    1×
    1×

    1×
    1×
    1×
    1×
    1×
    1×
    18×
    16×
    2×
    2×
    1×
    2×

    2×

    2×

    1×
    1×
    18×

    1×
    4×
    18×

    1×
    18×
    18×

    18×

    18×

    1×
    1×
    18×

    1×
    1×
    6×
    6×
    6×
    6×
    6×
    25×
    25×
    25×
    25×
    25×
    25×
    25×
    25×
    25×
    25×

    1×
    1×
    6×
    6×
    6×
    6×
    25×
    14×
    14×
    4×
    14×

    11×

    6×

    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
     */
    (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", "../Serialization/Utility", "../Types", "../KeyValueExtract", "../Collections/Enumeration/Enumerator"], factory);
        }
    })(function (require, exports) {
        "use strict";
        var Serialization = require("../Serialization/Utility");
        var Types_1 = require("../Types");
        var KeyValueExtract_1 = require("../KeyValueExtract");
        var Enumerator_1 = require("../Collections/Enumeration/Enumerator");
        var EMPTY = "", QUERY_SEPARATOR = "?", ENTRY_SEPARATOR = "&", KEY_VALUE_SEPARATOR = "=", TO_URI_COMPONENT = "toUriComponent";
        function encode(values, prefixIfNotEmpty) {
            if (!values)
                return EMPTY;
            var entries = [];
            if (Enumerator_1.isEnumerableOrArrayLike(values)) {
                Enumerator_1.forEach(values, function (entry) {
                    return KeyValueExtract_1.extractKeyValue(entry, function (key, value) { return appendKeyValue(entries, key, value); });
                });
            }
            else {
                Object.keys(values).forEach(function (key) { return appendKeyValue(entries, key, values[key]); });
            }
            return (entries.length && prefixIfNotEmpty ? QUERY_SEPARATOR : EMPTY)
                + entries.join(ENTRY_SEPARATOR);
        }
        exports.encode = encode;
        function appendKeyValueSingle(entries, key, value) {
            entries.push(key + KEY_VALUE_SEPARATOR + encodeValue(value));
        }
        function appendKeyValue(entries, key, value) {
            Eif (Enumerator_1.isEnumerableOrArrayLike(value)) {
                Enumerator_1.forEach(value, function (v) { return appendKeyValueSingle(entries, key, v); });
            }
            else {
                appendKeyValueSingle(entries, key, value);
            }
        }
        function encodeValue(value) {
            var v = null;
            Iif (isUriComponentFormattable(value)) {
                v = value.toUriComponent();
                if (v && v.indexOf(ENTRY_SEPARATOR) != 1)
                    throw '.toUriComponent() did not encode the value.';
            }
            else {
                v = encodeURIComponent(Serialization.toString(v));
            }
            return v;
        }
        exports.encodeValue = encodeValue;
        function isUriComponentFormattable(instance) {
            return Types_1.Type.hasMemberOfType(instance, TO_URI_COMPONENT, Types_1.Type.FUNCTION);
        }
        exports.isUriComponentFormattable = isUriComponentFormattable;
        function parse(query, entryHandler, deserialize, decodeValues) {
            Iif (deserialize === void 0) { deserialize = true; }
            Iif (decodeValues === void 0) { decodeValues = true; }
            Eif (query && (query = query.replace(/^\s*\?+/, ''))) {
                var entries = query.split(ENTRY_SEPARATOR);
                for (var _i = 0, entries_1 = entries; _i < entries_1.length; _i++) {
                    var entry = entries_1[_i];
                    var si = entry.indexOf(KEY_VALUE_SEPARATOR);
                    Eif (si != -1) {
                        var key = entry.substring(0, si);
                        var value = entry.substring(si + 1);
                        Eif (decodeValues)
                            value = decodeURIComponent(value);
                        Eif (deserialize)
                            value = Serialization.toPrimitive(value);
                        entryHandler(key, value);
                    }
                }
            }
        }
        exports.parse = parse;
        function parseToMap(query, deserialize, decodeValues) {
            Eif (deserialize === void 0) { deserialize = true; }
            Eif (decodeValues === void 0) { decodeValues = true; }
            var result = {};
            parse(query, function (key, value) {
                if ((key) in (result)) {
                    var prev = result[key];
                    if (!(Array.isArray(prev)))
                        result[key] = prev = [prev];
                    prev.push(value);
                }
                else
                    result[key] = value;
            }, deserialize, decodeValues);
            return result;
        }
        exports.parseToMap = parseToMap;
        function parseToArray(query, deserialize, decodeValues) {
            if (deserialize === void 0) { deserialize = true; }
            if (decodeValues === void 0) { decodeValues = true; }
            var result = [];
            parse(query, function (key, value) { result.push({ key: key, value: value }); }, deserialize, decodeValues);
            return result;
        }
        exports.parseToArray = parseToArray;
        var Separator;
        (function (Separator) {
            Separator.Query = QUERY_SEPARATOR;
            Separator.Entry = ENTRY_SEPARATOR;
            Separator.KeyValue = KEY_VALUE_SEPARATOR;
        })(Separator = exports.Separator || (exports.Separator = {}));
        Object.freeze(Separator);
    });
    //# sourceMappingURL=QueryParams.js.map