How can I convert string to boolean? - actionscript-3

I have this code:
trace(arr[0][2]);
cb.selected = Boolean(arr[0][2]);
This outputs "false" but the checkbox is selected. How can I fix this?

This is expected behaviour. You are using the top level function Boolean / explicitly converting to a different type.
If the argument is a non-empty String it will return true.
Why do you have this string in the first place?
Why don't you simply store boolean values?

Check arr[0][2] value:
cb.selected = arr[0][2] == "true" ? true : false;
or simple:
cb.selected = arr[0][2] == "true";

Related

no implicit conversion between string and int in razor view javascript area

<script type="text/javascript">
var destinationId = #(Model.DestinationId == 0?"":Model.DestinationId);
</script>
I want to output "" if Model.DestinationId is 0 else display Model.DestinationId
Because your C# code is trying to return a string when the if condition yields true and returns an int(value of DestinationId) when the condition expression returns false. The compiler is telling you that it is not valid! You need to return the same type from both the cases.
To fix the issue, return the same type from both the cases. You can use ToString() method on the int type property so that your ternary expression will always return the same type (string)
var destinationId = #(Model.DestinationId == 0 ? "" : Model.DestinationId.ToString());
Although the above will fix your compiler error, it might not be giving what you are after. The above code will render something like this when your DestinationId property value is 0.
var destinationId = ;
Which is going to give you a script error
Uncaught SyntaxError: Unexpected token ;
and when DestinationId property has a non zero value, for example, 10.
var destinationId = 10;
There are multiple ways to solve the issue. One simple approach is to replace the empty string with something JavaScript understands. Here in the below sample, I am rendering null as the value
var destinationId = #(Model.DestinationId == 0 ? "null" : Model.DestinationId.ToString());
if (destinationId===null) {
console.log('value does not exist');
}
else {
console.log('value is '+ destinationId);
}
Another option is to simply read the DestinationId property value and check for 0 in your JavaScript code as needed.
Another option is to wrap your entire C# expression in single or double quotes. But then again you number value will be represented as a string :(, which is not good.
I suggest you to use the correct type(even in JavaScript)

Trying to check the value of a cell and compare it to another value to see if it matches(Google Script)

I am trying to creating a booking system, and as of now I am going to allow my user to insert their name and then check if there is a value. But whenever I try to simply accomplish this by using if( m == 'string) it thinks that all of the empty spaces are strings which results in everything saying booked.
Function checkifBooked(name)
{
var string ='BOOKED';
var string2 ='FREE';
if(typeof name == 'string')
{
return string;
}
else{
return string2;
}
}
When you get the value of an empty cell you get an empty string '', which is still a string. Thus, your if always evaluates to True.
In javascript, an empty string evaluates to false, so you can test the string directly.
try:
if(name)
Or if you prefer to be more explicit, you could check the type of name, then check it's length.
if(typeof(name) == 'string' && name.length > 0)

How can I create a default boolean value for a configuration using BND

I am using BND annotations to assist with creating a configuration managed by OSGI cm.
Here is my simple config
#Meta.AD(required = false, type = Type.Boolean, deflt = "false")
boolean enabled();
I have used the BND configuration annotation library quite a few times, but this is the first time that I would like to use a boolean type.
I have read through this
And it talks about an integer or other alternative number based handling of booleans for convenience. The thing is the deflt method always returns a string value, if my type was an integer I would do "2" (these are parsed). But booleans don't seem to be parsed in the configurable BND code up to this assignment point.
if (resultType == boolean.class || resultType == Boolean.class) {
if ( actualType == boolean.class || actualType == Boolean.class)
return o;
if (Number.class.isAssignableFrom(actualType)) {
double b = ((Number) o).doubleValue();
if (b == 0)
return false;
else
return true;
}
return true;
I would like to further know why this returns true when the deflt value is never even parsed. I expected this to more closely follow the spec and return false, since cm would try to do Boolean.parseFrom, so anything not "true" equal ignore case is false.
All of this isn't a complete failure because if I change the value through cm it works correctly after setting to true, and then false again, but obviously that was just an effort at wondering if it would ever work.
Simply I would like to know if anyone knows how to set a BOOLEAN default value using BND's configuration annotations.
Thanks

if ( object != null ) instead of if ( object ) in AS3

Is there any reason to use if ( object != null ) in a conditional instead of the more concise if ( object ). I see the former more often, but it seems like the two are equivalent and the latter shorter.
different objects are treated differently. there are some values that can never be null and might erroneously trigger false when you don't want it to.
Booleans can never be null.
if a Boolean is defined but not given a value it is treated as false;
var bool:Boolean;
trace(bool); // false
bool = true;
trace(bool); // true
int can never be null as well.
if an int is defined but not given a value, it is treated as 0;
var i:int;
trace(i); // 0
trace(Boolean(i)); // false
i = -1;
trace(Boolean(i)); // true
i = 0;
trace(Boolean(i)); // false
i = 1;
trace(Boolean(i)); // true
Number act similar to int.
if a Number is defined but not given a value, it is treated as NaN
var num:Number;
trace(num); // NaN
trace(Boolean(num)); // false
num = -1.1;
trace(Boolean(num)); // true
num = 0;
trace(Boolean(num)); // false
num = 1.1;
trace(Boolean(num)); // true
Strings are the worst.
if a String is defined but not given a value, it is treated as null!
var str:String;
trace(str); // null
trace(Boolean(str)); // false
str = ""
trace(Boolean(str)); // false
str = "hello";
trace(Boolean(str)); // true
it is very rare in my code that i only do 'if (obj)' since most of the time there is something more specific that i truly care about.
The following code will have an object evaluate to false, even though the object is not null:
class SomeWrapper
{
var value: Object;
SomeWrapper(Object value)
{
this.value = value;
}
/// Overriden method from Object, see ActionScript 3 reference
function valueOf()
{
return value;
}
}
var myWrapper = new SomeWrapper(false);
if(myWrapper)
{
trace("myWrapper evaluates to true.");
}
else
{
trace("myWrapper evaluates to false.");
}
The else block will execute in the example above, because myWrapper evaluates to false (its valueOf method returns whatever value the wrapper contained, in this case false) even though myWrapper is not a null reference. The problem is that the above does not only test nullability of reference, it implicitly invokes valueOf method, courtesy of Flash Player virtual machine. This behavior of course may or may not be what you wanted - do you want to test whether myWrapper is a null value or whether it carries a null value? (or both perhaps).
Verbosity in this case is your friend, otherwise you gain code readability in return for potential runtime errors.
if (expression) tests the truthiness of expression by getting its boolean value. Since null and undefined are both falsy, if (object) "works" for making sure object isn't null.
Problem is, though, 0, false, and '' are also falsy. When you use if (object), the code you're trying to keep from running when it's null would also be ignored when object is zero, an empty string, or the actual boolean false. Often, that's not what you want.
If you can't guarantee that object is either null or a type other than string, number, or boolean (or their respective object types), then if (object != null) is less likely to mean something unintended.

JSON empty string

why does the JSON.stringify-Function converts a string.Empty ("") to a "null"-String?
The problem, why i'm not using:
JSON.parse(json, function(key, value) {
if (typeof value === 'string') {
if (value == 'null')
return '';
return value;
}
});
...is, if somebody really write "null" (is very unlikely, but possible), i have a problem to...
thank for each answer!
Old question - but its the top result when you search for 'json stringify empty string' so I'll share the answer I found.
This appears to be a bug in certain versions of IE8, where empty DOM elements return a value which looks like an empty string, evaluates true when compared to an empty string, but actually has some different encoding denoting that it is a null value.
One solution is to do a replace whenever you call stringify.
JSON.stringify(foo, function(key, value) { return value === "" ? "" : value });
See also http://blogs.msdn.com/b/jscript/archive/2009/06/23/serializing-the-value-of-empty-dom-elements-using-native-json-in-ie8.aspx
now the esiest solution for this problem is, to pack the "document.getElementById('id').value" expression in the constructor of the String class:
JSON.stringify({a:new String(document.getElementById('id').value)}); -> {"a":""}
i can't find the primary problem, but with this, it's working well in Internet Explorer as well in FireFox.
i'm not very happy with this dirty solution, but the effort is not to much.
JSON library: https://github.com/douglascrockford/JSON-js/blob/master/json2.js