Code coverage report for master/lib/jsdoc/util/cast.js

Statements: 100% (36 / 36)      Branches: 95.45% (21 / 22)      Functions: 100% (3 / 3)      Lines: 100% (36 / 36)      Ignored: none     

All files » master/lib/jsdoc/util/ » cast.js
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                                1 94 94   94   8 8     2 2     1 1     1 1     1 1     81 81 4     77     81 61     20         94                           1 105   105 4 4 7     101 6 6 11     95 94     1     105    
'use strict';
 
/**
 * Module to convert values between various JavaScript types.
 * @module
 * @private
 */
 
/**
 * Check whether a string contains a boolean or numeric value, and convert the string to the
 * appropriate type if necessary.
 *
 * @private
 * @param {string} str - The string to convert.
 * @return {(string|number|boolean)} The converted value.
 */
function castString(str) {
    var number;
    var result;
 
    switch (str) {
        case 'true':
            result = true;
            break;
 
        case 'false':
            result = false;
            break;
 
        case 'NaN':
            result = NaN;
            break;
 
        case 'null':
            result = null;
            break;
 
        case 'undefined':
            result = undefined;
            break;
 
        default:
            Eif (typeof str === 'string') {
                if (str.indexOf('.') !== -1) {
                    number = parseFloat(str);
                }
                else {
                    number = parseInt(str, 10);
                }
 
                if ( String(number) === str && !isNaN(number) ) {
                    result = number;
                }
                else {
                    result = str;
                }
            }
    }
 
    return result;
}
 
/**
 * Check whether a string contains a boolean or numeric value, and convert the string to the
 * appropriate type if necessary.
 *
 * If an object or array is passed to this method, the object or array's values will be recursively
 * converted to the appropriate types. The original object or array is not modified.
 *
 * @private
 * @param {(string|Object|Array)} item - The item whose type will be converted.
 * @return {(string|number|boolean|Object|Array)} The converted value.
 */
exports.cast = function cast(item) {
    var result;
 
    if ( Array.isArray(item) ) {
        result = [];
        for (var i = 0, l = item.length; i < l; i++) {
            result[i] = cast(item[i]);
        }
    }
    else if (typeof item === 'object' && item !== null) {
        result = {};
        Object.keys(item).forEach(function(prop) {
            result[prop] = cast(item[prop]);
        });
    }
    else if (typeof item === 'string') {
        result = castString(item);
    }
    else {
        result = item;
    }
 
    return result;
};