pipwerks / scorm-api-wrapper

The pipwerks SCORM API Wrapper
http://pipwerks.com
357 stars 125 forks source link

StringToBoolean function incorrect result with String() object #3

Closed jjs105 closed 11 years ago

jjs105 commented 11 years ago

Hi,

The LMS I am currently using returns a JavaScript String() object (containing true/false) when calling the various string functions.

When passed to typeof this returns 'object' which is not caught in the UTILS.StringToBoolean function and therefore always returns false.

Simple fix (within the switch statement) is as follows. ... case "object": return value == "true" ? true : false; ...

Regards, Jon

moloko commented 11 years ago

Is a good point... if you do:

var s = new String("true");

which is unusual - but valid - then

typeof s

will return 'object'.

If you look at something like is.js, that checks for data being a String by using the following combination: is_string = function(s) { return (typeof s === 'string') || s instanceof String; },

pipwerks commented 11 years ago

Unusual use case, but I see your point.

The fix is simpler than the one specified: just add case: "object" above case "string": return (/(true|1)/i).test(value);

and let the fall-through handle the object as a string. If the object is not an instanceof String, the regex will correctly return false. I have tested in Chrome, Safari, Opera and Firefox. (Haven't tried IE yet, but will get to it)

Thanks for the suggestion