# all files / TypeScript.NET/dist/commonjs/System/Serialization/ Utility.js

43.75% Statements 21/48
23.26% Branches 10/43
20% Functions 1/5
55.26% Lines 21/38

    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

    1×

    1×
    1×
    1×

    1×

    1×
    1×

    1×
    1×
    2×
    2×
    2×

    2×
    2×
    2×

    2×
    2×

    2×
    2×

    2×

    1×

     /*!
     * @author electricessence / https://github.com/electricessence/
     * Licensing: MIT https://github.com/electricessence/TypeScript.NET/blob/master/LICENSE.md
     */
    "use strict";

    var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };

    var Types_1 = require('../Types');
    var InvalidOperationException_1 = require('../Exceptions/InvalidOperationException');
    var EMPTY = '',
        TRUE = 'true',
        FALSE = 'false';
    function toString(value, defaultForUnknown) {
        var v = value;
        switch (typeof v === 'undefined' ? 'undefined' : _typeof(v)) {
            case Types_1.default.UNDEFINED:
            case Types_1.default.STRING:
                return v;
            case Types_1.default.BOOLEAN:
                return v ? TRUE : FALSE;
            case Types_1.default.NUMBER:
                return EMPTY + v;
            default:
                if (v === null) return v;
                if (isSerializable(v)) return v.serialize();else if (arguments.length > 1) return defaultForUnknown;
                var ex = new InvalidOperationException_1.default('Attempting to serialize unidentifiable type.');
                ex.data['value'] = v;
                throw ex;
        }
    }
    exports.toString = toString;
    function isSerializable(instance) {
        return Types_1.default.hasMemberOfType(instance, 'serialize', Types_1.default.FUNCTION);
    }
    exports.isSerializable = isSerializable;
    function toPrimitive(value, caseInsensitive, unknownHandler) {
        Eif (value) {
            Iif (caseInsensitive) value = value.toLowerCase();
            switch (value) {
                case 'null':
                    return null;
                case Types_1.default.UNDEFINED:
                    return undefined;
                case TRUE:
                    return true;
                case FALSE:
                    return false;
                default:
                    var cleaned = value.replace(/^\s+|,|\s+$/g, EMPTY);
                    Eif (cleaned) {
                        Iif (/^\d+$/g.test(cleaned)) {
                            var int = parseInt(cleaned);
                            if (!isNaN(int)) return int;
                        } else {
                            var number = parseFloat(value);
                            Iif (!isNaN(number)) return number;
                        }
                    }
                    Iif (unknownHandler) value = unknownHandler(value);
                    break;
            }
        }
        return value;
    }
    exports.toPrimitive = toPrimitive;
    //# sourceMappingURL=Utility.js.map