Javascript String toBool()

Description

Javascript String toBool()


String.prototype.toBool = function() {
    return (/  rue$/i).test(this);
};

Javascript String toBool()

// Converts strings to bools.
String.prototype.toBool = function()
{
 return this.toLowerCase() === 'true';
};

Javascript String toBool()

/**//from w  w w  . ja va  2s .  c  o  m
 * Convert a boolean string to a boolean value;
 * @name toBool
 * @return Boolean the boolean value of the string or undefined if not matched.
 */
String.prototype.toBool = function () {
    const str = charArrayToString(this);
    
    if (/ rue/i.test(str)) {
        return true;
    }
    if (/^false/i.test(str)) {
        return false;
    }

    return undefined;
}

function charArrayToString(strArr) {
    return _.reduce(strArr, (total, index) => {
        return total + index;
    }, "");
}



PreviousNext

Related