A trivial question, what would you name this funct...
# suitescript
m
A trivial question, what would you name this function? And would you add something to it?
Copy code
function(value) {
    if (value == null || value == "" || typeof value == "undefined") {
      return false;
    }
    return true;
}
c
I would just use
!!
instead
m
Is it exactly the same? Maybe it is just my distrust of JS.
n
Just don't rely on !! if zero is a valid value and could be in the variable you're evaluating, it'll return false
m
Will keep that in mind.
n
(this is where CD scrabbles around thinking oh damn, I wonder how many times I've evaluated a zero tax amount as false because it was zero... 😄 )
c
Heh, nope 🙂
🤭 2
m
JS is made by the IRS then. It is all clear now.
🤭 1
b
its probably okay in this case,
Copy code
0 == ""
is true
👍🏻 1
j
I wrote a similar function that takes some options for what it wants to consider as having a value. It’s literally called “hasValue()” and I’ve got it in a module called md_integrity.
Looks like this
Copy code
// Returns true if the passed in thing has a value, false if blank, null, undefined, empty, or 'To Be Generated'.
	// We can override whether or not to include blank, undefined, empty, or 'To Be Generated' as null.
	function hasValue(value_to_check, options) {
		
		// Set up some default options.
		if(options === undefined) {
			
			options = {
				check_to_be_generated: true,
				check_blank: true,
				check_undefined: true,
				check_empty_array: true,
				check_is_nan: false,
				check_null_string: false
			}
			
		}
		
		if(options.check_to_be_generated === undefined)
			options.check_to_be_generated = true;
		
		if(options.check_blank === undefined)
			options.check_blank = true;
		
		if(options.check_undefined === undefined)
			options.check_undefined = true;
		
		if(options.check_empty_array === undefined)
			options.check_empty_array = true;
		
		if(options.check_is_nan === undefined)
			options.check_is_nan = false;
		
		if(options.check_null_string === undefined)
			options.check_null_string = false;
		
		
		// Check if the passed-in-value has a value!
		if(value_to_check === null)
			return false;
		
		if(options.check_blank && value_to_check === '')
			return false;
		
		if(options.check_undefined && value_to_check === undefined)
			return false;
		
		if(options.check_to_be_generated && value_to_check == 'To Be Generated')
			return false;
		
		if(options.check_empty_array && typeof value_to_check !== 'string' && value_to_check.length == 0)
			return false;
		
		if(options.check_is_nan && isNaN(value_to_check))
			return false;
		
		if(options.check_null_string && value_to_check === 'null')
			return false;
		
		// At this point it passed all the checks so return true.
		return true;
		
	}
m
Copy code
if(options.check_to_be_generated && value_to_check == 'To Be Generated')
			return false;
Handling NetSuite problems on top of JS problems, I feel your pain 🙂
I tried it in the console, it is a little bit verbose, but I can see the benefits. Or maybe this is the way it has to be.