1

In javascript, we often see 0 and 1 representing boolean TRUE and FALSE.

When using javascript in conjunction with HTML, values that originate within an HTML element (e.g. a form or ELEMENT attribute) may have a "type" conversion (e.g. "1" or "true" and could be handed through to javascript as a string.

In addition, we may also have different values for TRUE in string form as lower/upper/camel case (TRUE / true / True).

So determining true TRUE can be tricky. I'm wondering if the following function covers all bases, or if i've missed something?

function isFalse (val){
    // Just use isTrue so we don't have to test against "" and null
    return ! isTrue(val);

    // This is the actual code needed to test against FALSE, notice the extra "" and NULL checks 
    /*
    if(typeof val == "string"){
        return !(val === "0" || val === "" || val.toLowerCase() === "false");
    } else if (typeof val == "object" && val !== null) {
        return Object.keys(val).length;
    }
    return !!val;
    */
}

function isTrue(val){
    if(typeof val == "string"){
        return (val === "1" || val.toLowerCase() === "true");
    } else if (typeof val == "object" && val !== null) {
        return Object.keys(val).length;
    }
    return !!val;
}

// An empty var for testing
var emptyvar;

// A list of test values
// [value is t/f, value]
var list = [
  [1, 1]
, [1, "1"]
, [1, true]
, [1, "true"]
, [1, "TRUE"]
, [1, "True"]
, [0, 0]
, [0, "0"]
, [0, false]
, [0, "false"]
, [0, "FALSE"]
, [0, "False"]
, [0, NaN]
, [0, undefined]
, [0, emptyvar]
, [0, null]
, [0, ""]
, [0, {}]
, [0, []]
];

console.log("-------------- is true ---------------------");

for(var i=0; i<list.length; i++){
    var item = list[i][1];
    var tf = isTrue(item);
    console.log( "pass : " + (list[i][0] == tf ? 1 : 0) + "  -   isTrue : " + tf +  " : " + (typeof item) + " : " + item );
}

console.log("-------------- is false ---------------------");

for(var i=0; i<list.length; i++){
    var item = list[i][1];
    var tf = isFalse(item);
    console.log( " pass : " + (list[i][0] == tf ? 0 : 1) + "  -   isFalse : " + tf +  " : " + (typeof item) + " : " + item );
}

The code above passes the tests correctly, but I'm wondering if I've missed something obvious?

null
  • 5,207
  • 1
  • 18
  • 34
bob
  • 6,861
  • 2
  • 44
  • 41
  • True and False are a condition not an actual value. I may actually want to know when a value is NaN and thus true for example. Can you explain what problem your code is solving? – Radio Mar 12 '15 at 20:21
  • Also curious what problem you're trying to solve here. Are you just trying to treat things like `0` and `"0"` consistently? – bvaughn Mar 12 '15 at 20:56

1 Answers1

1

In javascript, we often see 0 and 1 representing boolean TRUE and FALSE.

I don't? But yes, 0 and 1 are pretty interchangable with false and true (just do + or !! for conversion).

When using javascript in conjunction with HTML, values that originate within an HTML element (e.g. a form or ELEMENT attribute) may have a "type" conversion (e.g. "1" or "true" and could be handed through to javascript as a string).

No, you seldomly would. The standard HTML boolean attributes can should be accessed via DOM properties that will have a real boolean type. And pretty well-defined conversion rules.

I'm wondering if the following function covers all bases, or if i've missed something?

A function never covers all cases. As follows from the above, you hardly need such a conversion function anyway, and when you do it's for a very restricted domain. In that case, define what cases you have in your domain and cover them.

So for your test case - a domain definition - your proposed function apparently works well. I do however find that domain a bit odd, I never would consider an object (even if empty) to be falsy.

Bergi
  • 572,313
  • 128
  • 898
  • 1,281
  • Many minifiers such as Google Closure and YUI Compressor replace boolean with 0/1. And it is lazy coding for slugs. Often times a DOM checkbox will be boolean in nature when "checked" or not checked -- but not true boolean? For "attributes" picked up from HTML elements, I'm thinking of data-custom attributes, as in
    . I agree, checking if an object or array is empty is kinda not part of the requirements.
    – bob Mar 13 '15 at 03:22
  • @bob: OK, minifiers sometimes bend the rules of a language - which doesn't means you should. Though afaik they use `!0` and `!1`. Then, `typeof inputElement.checked` is very much boolean. What you place in custom data attributes is your choice (you select the value domain), a good choice here would be JSON which can serialize and parse booleans to strings. – Bergi Mar 13 '15 at 03:52
  • The idea is kind of a cross between "exists", "has a value" and "false positive". For example on a – bob Aug 30 '15 at 05:19
  • @bob: You have to distinguish between HTML and JavaScript, between tags and elements, between attributes and properties. In HTML, there's ` – Bergi Aug 30 '15 at 10:23