JSFiddle - React, Tailwind, and code Playground

by robaldred

HTML

<h2>Defined results</h2>
<textarea id="ifdefined_result"></textarea>
<br><br>

CSS

h2 {
  font-weight:bold;
  font-size:18px;
}
textarea {
    width:300px;
    height:400px;
}

JavaScript

var ifdefined = function(var_name) {
    try {
        if(
            typeof(var_name) == 'undefined' 
            || !var_name
            || (
               typeof(var_name) == 'string'
               && var_name.match(/^(?:\s*<p>)?\s*<br( \/)?>\s*(?:<\/p>\s*)?$/)
            )
        ) {
            return false;
        } else {
            return true;
        }
    } catch(e) {
        return;        
    }
};

// [[value,expected],[anothervalue,expected]]

var tests = [
    [undefined,false],
    [null,false],
    ["true",true],
    [true,true],
    [false,false],
    //true,
    ["Apple",true],
    ["<p>dfgfdgdfgdf<br></p>",true],
    ["",false],
    //false,
    ["<p><br></p>",false],
    ["<br>",false],
    ["tabbed",true],
    [0,false],
    [1,true]
];


//test runner, no need to change these

$.each(tests,function() {
    var escaped = $.trim($('<div>').text(this[0]).html());
    $('#ifdefined_result').append('testing ' + escaped + '\n');
    var result = ifdefined(this[0])
    if(result == this[1]) {
        $('#ifdefined_result').append("OK");
    } else {
        $('#ifdefined_result').append("FAILED");
    }
    $('#ifdefined_result').append('\n\n');
});