mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-24 06:52:40 -05:00
Merge pull request #438 from griffpatch/bug/WhiteSpaceEqualsZero
Fix for the "Empty or white space strings equal 0" bug
This commit is contained in:
commit
01abda152e
1 changed files with 14 additions and 0 deletions
|
@ -90,6 +90,15 @@ Cast.toRgbColorObject = function (value) {
|
|||
return color;
|
||||
};
|
||||
|
||||
/**
|
||||
* Determine if a Scratch argument is a white space string (or null / empty).
|
||||
* @param {*} val value to check.
|
||||
* @return {boolean} True if the argument is all white spaces or null / empty.
|
||||
*/
|
||||
Cast.isWhiteSpace = function (val) {
|
||||
return val === null || typeof val === 'string' && val.trim().length === 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* Compare two values, using Scratch cast, case-insensitive string compare, etc.
|
||||
* In Scratch 2.0, this is captured by `interp.compare.`
|
||||
|
@ -100,6 +109,11 @@ Cast.toRgbColorObject = function (value) {
|
|||
Cast.compare = function (v1, v2) {
|
||||
var n1 = Number(v1);
|
||||
var n2 = Number(v2);
|
||||
if (n1 === 0 && Cast.isWhiteSpace(v1)) {
|
||||
n1 = NaN;
|
||||
} else if (n2 === 0 && Cast.isWhiteSpace(v2)) {
|
||||
n2 = NaN;
|
||||
}
|
||||
if (isNaN(n1) || isNaN(n2)) {
|
||||
// At least one argument can't be converted to a number.
|
||||
// Scratch compares strings as case insensitive.
|
||||
|
|
Loading…
Reference in a new issue