# all files / TypeScript.NET/source/System/Serialization/ Utility.js

55.77% Statements 29/52
33.33% Branches 14/42
80% Functions 4/5
56% Lines 28/50

    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

    1×
    1×
    1×

    1×
    1×
    1×
    1×
    18×
    18×

    18×
    18×

    1×
    1×

    1×
    1×
    25×
    25×

    25×

    18×

    7×
    7×
    7×

    7×
    7×

    7×

    7×

    7×

    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", "../Types", "../Exceptions/InvalidOperationException"], factory);
        }
    })(function (require, exports) {
        "use strict";
        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) {
                case Types_1.Type.UNDEFINED:
                case Types_1.Type.STRING:
                    return v;
                case Types_1.Type.BOOLEAN:
                    return v ? TRUE : FALSE;
                case Types_1.Type.NUMBER:
                    return EMPTY + v;
                default:
                    Eif (v === null)
                        return v;
                    if (isSerializable(v))
                        return v.serialize();
                    else if (arguments.length > 1)
                        return defaultForUnknown;
                    var ex = new InvalidOperationException_1.InvalidOperationException('Attempting to serialize unidentifiable type.');
                    ex.data['value'] = v;
                    throw ex;
            }
        }
        exports.toString = toString;
        function isSerializable(instance) {
            return Types_1.Type.hasMemberOfType(instance, 'serialize', Types_1.Type.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.Type.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