Working with true | false properties

treycarroll
Giga Guru

//CREATE THE PROPERTY AND THEN RUN THIS IN A BACKGROUND SCRIPT TO VERIFY THE OUTPUT

 

 

// PROPERTY 'chg.requireApproval'IS OF TYPE TRUE/FALSE.  

// THESE TYPE OF PROPERTIES STILL COME IN AS A STRING.

 

 

// THIS EXECUTES EVEN WHEN THE PROPERTY IS SET TO "false"!!!   BEWARE OF THE PHANTOM JS TRUE

if (gs.getProperty('chg.requireApproval')) {

      gs.log('1');

}

 

 

// THIS NEVER PRINTS, EVEN WHEN THE PROPERTY IS "true"!   IN JS 'true' != true

if (gs.getProperty('chg.requireApproval') == true) {

      gs.log('2');

}

 

 

// WARNING!!!   This won't print since true != "true" in Javascript!!!

if ('true' == true) {

      gs.print('3');

}

 

 

// REGEX TEST.   GREAT IF YOU NEED A CASE INSENSITIVE COMPARISON

if (/true/i.test(gs.getProperty('chg.requireApproval'))) {

      gs.log('4');

}

 

 

// A VERY TERSE WAY TO CAST A VALUE TO A BOOLEAN. SCARES PROPLE, BUT IT IS THE BEST WAY TO QUICK CAST THINGS TO A BOOLEAN

// ! #1. CONVERT TO A BOOLEAN AND INVERT

// ! #2. REINVERT BACK TO THE ORIGINAL BOOLEAN VALUE

if (!!gs.getProperty('chg.requireApproval')) {

      gs.log('5');

}

 

 

//IF THIS IS TOO DISTASTEFUL, WRAP IT IN A FUNCTION TO HIDE THE !! SYNTAX

function toBool(val) { return !!val; }

if( toBool('TRue') ) {

      gs.log('5.5');

}

 

 

// PLAIN OLD WAY OF DOING THINGS IN OUR EXISTING CODE. WE SHOULD AT LEAST ADD A .toLowerCase() call.

if (gs.getProperty('chg.requireApproval') == 'true') {

      gs.log('6');

}

 

 

// WHEN THE PROPERTY IS "false", OUTPUT WILL BE...

// *** Script: 1

// *** Script: 5

// *** Script: 5.5

 

 

// WHEN THE PROPERTY IS "true", OUTPUT WILL BE...

// *** Script: 1

// *** Script: 4

// *** Script: 5

// *** Script: 5.5

// *** Script: 6

4 REPLIES 4

Jim Coyne
Kilo Patron

Yeah, the "type" of property is very misleading.   It really relates to how the property is shown within a properties page:



ServiceNow.png


Regardless of the type, the data is always stored and retrieved as strings because all data is stored in the "value" field, which is defined as a string.



You can see the properties page here - https://demo003.service-now.com/nav_to.do?uri=system_properties_ui.do?sysparm_title=Test%20%20Proper...



And the actual records are here - https://demo003.service-now.com/nav_to.do?uri=sys_properties_list.do?sysparm_query=GOTOnameSTARTSWIT...


Thanks, Jim!   What a helpful and concise explanation.


You are welcome.


treycarroll
Giga Guru

We can't edit our own posts?     That's disappointing.



5 and 5.5 are probably not casting to bool in the way that most people would expect.   It is forcing the value to be what it would be if you surrounded it with an if.   Somethings are not what most people would expect.   Namely, strings are always seen as true.   So, '0' and 'false' would be true.   If you're not a dyed-in-the-wool js developer, that is probably going to cause you some problems.



if(!!'false'){ gs.log(' DANGER - BAD ADVICE! ');



//OUTPUT


*** script:   DANGER - BAD ADVICE!



Can someone with higher privileges help me out and edit my original post to remove 5 and 5.5?



This script actually works in the intuitive way that most people would probably want:



function toBool(val){


      if( typeof val == 'string')


              return /(^true$|^1$|^on$|^yes$)/i.test(val);


      return !!val;


}


console.log(toBool('false'));   //false


console.log(toBool('true'));   //true


console.log(toBool('0'));   //false


console.log(toBool('1')); //true


console.log(toBool('off')); //false


console.log(toBool('on')); //true


console.log(toBool('no')); //false


console.log(toBool('yes')); //true


console.log(toBool(0)); //false


console.log(toBool(1)); //true


console.log(toBool(false)); //false


console.log(toBool(true));   //true


console.log(toBool(undefined)); //false


console.log(toBool(null)); //false



If you want to allow leading zeros and want all positive numbers to evaluate to true then you can use this:



function toBool(val) {  


      if( typeof val == 'string'){


              val = val.trim();


              return /(^true$|^[1-9][0-9]*$|^0+[1-9]+[0-9]*$|^on$|^yes$)/i.test(val);


      }


      return !!val;


}



console.log(toBool(' 001')) //true


console.log(toBool('00')) //false


console.log(toBool('101')) //true


console.log(toBool('-1')) //false