Skip to content

Instantly share code, notes, and snippets.

@CMCDragonkai
Created November 9, 2013 20:06
Show Gist options
  • Select an option

  • Save CMCDragonkai/7389368 to your computer and use it in GitHub Desktop.

Select an option

Save CMCDragonkai/7389368 to your computer and use it in GitHub Desktop.

Revisions

  1. CMCDragonkai created this gist Nov 9, 2013.
    34 changes: 34 additions & 0 deletions parseBooleanStyle.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    /**
    * Parses mixed type values into booleans. This is the same function as filter_var in PHP using boolean validation
    * @param {Mixed} value
    * @param {Boolean} nullOnFailure = false
    * @return {Boolean|Null}
    */
    var parseBooleanStyle = function(value, nullOnFailure = false){
    switch(value){
    case true:
    case 'true':
    case 1:
    case '1':
    case 'on':
    case 'yes':
    value = true;
    break;
    case false:
    case 'false':
    case 0:
    case '0':
    case 'off':
    case 'no':
    value = false;
    break;
    default:
    if(nullOnFailure){
    value = null;
    }else{
    value = false;
    }
    break;
    }
    return value;
    };