From cf215bf0bcac1906393029b4e6f861ad012b4e2e Mon Sep 17 00:00:00 2001 From: griffpatch Date: Wed, 8 Feb 2017 08:38:50 +0000 Subject: [PATCH 1/2] Fix for the "Empty or white space strings equal 0" bug See: Empty or white space strings equal 0 #435 --- src/util/cast.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/util/cast.js b/src/util/cast.js index a8a1535b8..c2e1cfe0f 100644 --- a/src/util/cast.js +++ b/src/util/cast.js @@ -100,6 +100,12 @@ Cast.toRgbColorObject = function (value) { Cast.compare = function (v1, v2) { var n1 = Number(v1); var n2 = Number(v2); + if (n1 === 0 && (v1 == null || typeof v1 === 'string' && v1.trim().length === 0)) { + n1 = NaN; + } + if (n2 === 0 && (v2 == null || typeof v2 === 'string' && v2.trim().length === 0)) { + n2 = NaN; + } if (isNaN(n1) || isNaN(n2)) { // At least one argument can't be converted to a number. // Scratch compares strings as case insensitive. From ad5bc1afbda3e9e5fa0618a0d9de84a1a76d8b21 Mon Sep 17 00:00:00 2001 From: griffpatch Date: Fri, 10 Feb 2017 08:02:11 +0000 Subject: [PATCH 2/2] Pull white space checks into function Clean up code by pulling white space checks into a seperate helper function --- src/util/cast.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/util/cast.js b/src/util/cast.js index c2e1cfe0f..79e76b134 100644 --- a/src/util/cast.js +++ b/src/util/cast.js @@ -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,10 +109,9 @@ Cast.toRgbColorObject = function (value) { Cast.compare = function (v1, v2) { var n1 = Number(v1); var n2 = Number(v2); - if (n1 === 0 && (v1 == null || typeof v1 === 'string' && v1.trim().length === 0)) { + if (n1 === 0 && Cast.isWhiteSpace(v1)) { n1 = NaN; - } - if (n2 === 0 && (v2 == null || typeof v2 === 'string' && v2.trim().length === 0)) { + } else if (n2 === 0 && Cast.isWhiteSpace(v2)) { n2 = NaN; } if (isNaN(n1) || isNaN(n2)) {