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

58.89% Statements 53/90
35.48% Branches 22/62
26.67% Functions 4/15
60% Lines 51/85

    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

    1×
    1×
    1×
    1×
    1×

    1×

    1×
    1×

    1×

    1×

    1×
    1×

    1×
    1×
    1×
    1×

    1×
    1×
    1×
    1×
    1×

    1×
    1×
    2×

    2×
    2×
    2×
    2×
    2×
    2×
    2×

    1×
    1×

    1×

    1×
    1×
    1×
    1×

    1×
    1×
    2×

    2×

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

    var Types_1 = require("../Types");
    var Serialization = require("../Serialization/Utility");
    var KeyValueExtract_1 = require("../KeyValueExtract");
    var Enumerator_1 = require("../Collections/Enumeration/Enumerator");
    var ENTRY_SEPARATOR = "&",
        KEY_VALUE_SEPARATOR = "=";
    function encode(values, prefixIfNotEmpty) {
        if (!values) return '';
        var entries = [];
        if (Enumerator_1.isEnumerableOrArrayLike(values)) {
            Enumerator_1.forEach(values, function (entry) {
                return KeyValueExtract_1.default(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 ? '?' : '') + 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) {
        if (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;
        if (isUriComponentFormattable(value)) {
            v = value.toUriComponent();
            if (v && v.indexOf('&') != 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.default.hasMemberOfType(instance, "toUriComponent", Types_1.default.FUNCTION);
    }
    exports.isUriComponentFormattable = isUriComponentFormattable;
    function parse(query, entryHandler) {
        var deserialize = arguments.length <= 2 || arguments[2] === undefined ? true : arguments[2];
        var decodeValues = arguments.length <= 3 || arguments[3] === undefined ? true : arguments[3];

        Eif (query && (query = query.replace(/^\s*\?+/, ''))) {
            var entries = query.split(ENTRY_SEPARATOR);
            var _iteratorNormalCompletion = true;
            var _didIteratorError = false;
            var _iteratorError = undefined;

            try {
                for (var _iterator = entries[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
                    var entry = _step.value;

                    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);
                    }
                }
            } catch (err) {
                _didIteratorError = true;
                _iteratorError = err;
            } finally {
                try {
                    Iif (!_iteratorNormalCompletion && _iterator.return) {
                        _iterator.return();
                    }
                } finally {
                    Iif (_didIteratorError) {
                        throw _iteratorError;
                    }
                }
            }
        }
    }
    exports.parse = parse;
    function parseToMap(query) {
        var deserialize = arguments.length <= 1 || arguments[1] === undefined ? true : arguments[1];
        var decodeValues = arguments.length <= 2 || arguments[2] === undefined ? true : arguments[2];

        var result = {};
        parse(query, function (key, value) {
            Iif (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) {
        var deserialize = arguments.length <= 1 || arguments[1] === undefined ? true : arguments[1];
        var decodeValues = arguments.length <= 2 || arguments[2] === undefined ? true : arguments[2];

        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.Entry = ENTRY_SEPARATOR;
        Separator.KeyValue = KEY_VALUE_SEPARATOR;
    })(Separator = exports.Separator || (exports.Separator = {}));
    Object.freeze(Separator);
    //# sourceMappingURL=QueryParams.js.map