Change parseFloat() to Number()

Number() is a bit less forgiving than parseFloat() and is more likely to generate NaN rather than some random number.  An audit of each case shows nowhere that parseFloat()’s features are needed.
This commit is contained in:
Neil Fraser
2019-07-31 18:26:14 -07:00
committed by Neil Fraser
parent 8916dbf75b
commit e77a9b7e81
32 changed files with 66 additions and 65 deletions

View File

@@ -429,7 +429,7 @@ Blockly.Events.Move.prototype.fromJson = function(json) {
if (json['newCoordinate']) {
var xy = json['newCoordinate'].split(',');
this.newCoordinate =
new Blockly.utils.Coordinate(parseFloat(xy[0]), parseFloat(xy[1]));
new Blockly.utils.Coordinate(Number(xy[0]), Number(xy[1]));
}
};

View File

@@ -77,7 +77,7 @@ Blockly.FieldNumber.prototype.SERIALIZABLE = true;
/**
* Set the maximum, minimum and precision constraints on this field.
* Any of these properties may be undefiend or NaN to be disabled.
* Any of these properties may be undefined or NaN to be disabled.
* Setting precision (usually a power of 10) enforces a minimum step between
* values. That is, the user's value will rounded to the closest multiple of
* precision. The least significant digit place is inferred from the precision.
@@ -87,15 +87,15 @@ Blockly.FieldNumber.prototype.SERIALIZABLE = true;
* @param {number|string|undefined} precision Precision for value.
*/
Blockly.FieldNumber.prototype.setConstraints = function(min, max, precision) {
precision = parseFloat(precision);
precision = Number(precision);
this.precision_ = isNaN(precision) ? 0 : precision;
var precisionString = this.precision_.toString();
var decimalIndex = precisionString.indexOf('.');
this.fractionalDigits_ = (decimalIndex == -1) ? -1 :
precisionString.length - (decimalIndex + 1);
min = parseFloat(min);
min = Number(min);
this.min_ = isNaN(min) ? -Infinity : min;
max = parseFloat(max);
max = Number(max);
this.max_ = isNaN(max) ? Infinity : max;
this.setValue(this.getValue());
};
@@ -121,7 +121,7 @@ Blockly.FieldNumber.prototype.doClassValidation_ = function(opt_newValue) {
newValue = newValue.replace(/,/g, '');
// Clean up number.
var n = parseFloat(newValue || 0);
var n = Number(newValue || 0);
if (isNaN(n)) {
// Invalid number.
return null;

View File

@@ -410,7 +410,7 @@ Blockly.FieldTextInput.numberValidator = function(text) {
text = text.replace(/O/ig, '0');
// Strip out thousands separators.
text = text.replace(/,/g, '');
var n = parseFloat(text || 0);
var n = Number(text || 0);
return isNaN(n) ? null : String(n);
};

View File

@@ -59,7 +59,7 @@ Blockly.FieldVariable = function(varname, opt_validator, opt_variableTypes,
this.menuGenerator_ = Blockly.FieldVariable.dropdownCreate;
this.size_ = new Blockly.utils.Size(0, Blockly.BlockSvg.MIN_BLOCK_Y);
opt_validator && this.setValidator(opt_validator);
this.defaultVariableName = (varname || '');
this.defaultVariableName = varname || '';
this.setTypes_(opt_variableTypes, opt_defaultType);
this.value_ = null;

View File

@@ -219,22 +219,22 @@ Blockly.Options.parseZoomOptions_ = function(options) {
if (zoom['startScale'] === undefined) {
zoomOptions.startScale = 1;
} else {
zoomOptions.startScale = parseFloat(zoom['startScale']);
zoomOptions.startScale = Number(zoom['startScale']);
}
if (zoom['maxScale'] === undefined) {
zoomOptions.maxScale = 3;
} else {
zoomOptions.maxScale = parseFloat(zoom['maxScale']);
zoomOptions.maxScale = Number(zoom['maxScale']);
}
if (zoom['minScale'] === undefined) {
zoomOptions.minScale = 0.3;
} else {
zoomOptions.minScale = parseFloat(zoom['minScale']);
zoomOptions.minScale = Number(zoom['minScale']);
}
if (zoom['scaleSpeed'] === undefined) {
zoomOptions.scaleSpeed = 1.2;
} else {
zoomOptions.scaleSpeed = parseFloat(zoom['scaleSpeed']);
zoomOptions.scaleSpeed = Number(zoom['scaleSpeed']);
}
return zoomOptions;
};
@@ -250,9 +250,9 @@ Blockly.Options.parseZoomOptions_ = function(options) {
Blockly.Options.parseGridOptions_ = function(options) {
var grid = options['grid'] || {};
var gridOptions = {};
gridOptions.spacing = parseFloat(grid['spacing']) || 0;
gridOptions.spacing = Number(grid['spacing']) || 0;
gridOptions.colour = grid['colour'] || '#888';
gridOptions.length = parseFloat(grid['length']) || 1;
gridOptions.length = Number(grid['length']) || 1;
gridOptions.snap = gridOptions.spacing > 0 && !!grid['snap'];
return gridOptions;
};

View File

@@ -85,9 +85,9 @@ Blockly.utils.getRelativeXY = function(element) {
var transform = element.getAttribute('transform');
var r = transform && transform.match(Blockly.utils.getRelativeXY.XY_REGEX_);
if (r) {
xy.x += parseFloat(r[1]);
xy.x += Number(r[1]);
if (r[3]) {
xy.y += parseFloat(r[3]);
xy.y += Number(r[3]);
}
}
@@ -97,9 +97,9 @@ Blockly.utils.getRelativeXY = function(element) {
var styleComponents =
style.match(Blockly.utils.getRelativeXY.XY_STYLE_REGEX_);
if (styleComponents) {
xy.x += parseFloat(styleComponents[1]);
xy.x += Number(styleComponents[1]);
if (styleComponents[3]) {
xy.y += parseFloat(styleComponents[3]);
xy.y += Number(styleComponents[3]);
}
}
}

View File

@@ -345,8 +345,8 @@ Blockly.Events.CommentMove.prototype.type = Blockly.Events.COMMENT_MOVE;
/**
* Override the location before the move. Use this if you don't create the
* event until the end of the move, but you know the original location.
* @param {!Blockly.utils.Coordinate} xy The location before the move, in workspace
* coordinates.
* @param {!Blockly.utils.Coordinate} xy The location before the move,
* in workspace coordinates.
*/
Blockly.Events.CommentMove.prototype.setOldCoordinate = function(xy) {
this.oldCoordinate_ = xy;
@@ -376,7 +376,7 @@ Blockly.Events.CommentMove.prototype.fromJson = function(json) {
if (json['newCoordinate']) {
var xy = json['newCoordinate'].split(',');
this.newCoordinate_ =
new Blockly.utils.Coordinate(parseFloat(xy[0]), parseFloat(xy[1]));
new Blockly.utils.Coordinate(Number(xy[0]), Number(xy[1]));
}
};
@@ -385,7 +385,8 @@ Blockly.Events.CommentMove.prototype.fromJson = function(json) {
* @return {boolean} False if something changed.
*/
Blockly.Events.CommentMove.prototype.isNull = function() {
return Blockly.utils.Coordinate.equals(this.oldCoordinate_, this.newCoordinate_);
return Blockly.utils.Coordinate.equals(this.oldCoordinate_,
this.newCoordinate_);
};
/**

View File

@@ -443,7 +443,7 @@ FactoryUtils.getFieldsJs_ = function(block) {
case 'field_angle':
// Result: new Blockly.FieldAngle(90), 'ANGLE'
fields.push('new Blockly.FieldAngle(' +
parseFloat(block.getFieldValue('ANGLE')) + '), ' +
Number(block.getFieldValue('ANGLE')) + '), ' +
JSON.stringify(block.getFieldValue('FIELDNAME')));
break;
case 'field_checkbox':
@@ -536,17 +536,17 @@ FactoryUtils.getFieldsJson_ = function(block) {
var obj = {
type: block.type,
name: block.getFieldValue('FIELDNAME'),
value: parseFloat(block.getFieldValue('VALUE'))
value: Number(block.getFieldValue('VALUE'))
};
var min = parseFloat(block.getFieldValue('MIN'));
var min = Number(block.getFieldValue('MIN'));
if (min > -Infinity) {
obj.min = min;
}
var max = parseFloat(block.getFieldValue('MAX'));
var max = Number(block.getFieldValue('MAX'));
if (max < Infinity) {
obj.max = max;
}
var precision = parseFloat(block.getFieldValue('PRECISION'));
var precision = Number(block.getFieldValue('PRECISION'));
if (precision) {
obj.precision = precision;
}

View File

@@ -1179,19 +1179,19 @@ WorkspaceFactoryController.prototype.readOptions_ = function() {
var startScaleValue =
document.getElementById('zoomOption_startScale_number').value;
zoom['startScale'] = typeof startScaleValue == 'string' ?
parseFloat(startScaleValue) : startScaleValue;
Number(startScaleValue) : startScaleValue;
var maxScaleValue =
document.getElementById('zoomOption_maxScale_number').value;
zoom['maxScale'] = typeof maxScaleValue == 'string' ?
parseFloat(maxScaleValue) : maxScaleValue;
Number(maxScaleValue) : maxScaleValue;
var minScaleValue =
document.getElementById('zoomOption_minScale_number').value;
zoom['minScale'] = typeof minScaleValue == 'string' ?
parseFloat(minScaleValue) : minScaleValue;
Number(minScaleValue) : minScaleValue;
var scaleSpeedValue =
document.getElementById('zoomOption_scaleSpeed_number').value;
zoom['scaleSpeed'] = typeof scaleSpeedValue == 'string' ?
parseFloat(scaleSpeedValue) : scaleSpeedValue;
Number(scaleSpeedValue) : scaleSpeedValue;
optionsObj['zoom'] = zoom;
}

View File

@@ -416,17 +416,17 @@ function getFieldsJson_(block) {
var obj = {
type: block.type,
name: block.getFieldValue('FIELDNAME'),
value: parseFloat(block.getFieldValue('VALUE'))
value: Number(block.getFieldValue('VALUE'))
};
var min = parseFloat(block.getFieldValue('MIN'));
var min = Number(block.getFieldValue('MIN'));
if (min > -Infinity) {
obj.min = min;
}
var max = parseFloat(block.getFieldValue('MAX'));
var max = Number(block.getFieldValue('MAX'));
if (max < Infinity) {
obj.max = max;
}
var precision = parseFloat(block.getFieldValue('PRECISION'));
var precision = Number(block.getFieldValue('PRECISION'));
if (precision) {
obj.precision = precision;
}

View File

@@ -89,11 +89,11 @@ Blockly.Dart['controls_for'] = function(block) {
if (Blockly.isNumber(argument0) && Blockly.isNumber(argument1) &&
Blockly.isNumber(increment)) {
// All arguments are simple numbers.
var up = parseFloat(argument0) <= parseFloat(argument1);
var up = Number(argument0) <= Number(argument1);
code = 'for (' + variable0 + ' = ' + argument0 + '; ' +
variable0 + (up ? ' <= ' : ' >= ') + argument1 + '; ' +
variable0;
var step = Math.abs(parseFloat(increment));
var step = Math.abs(Number(increment));
if (step == 1) {
code += up ? '++' : '--';
} else {

View File

@@ -33,7 +33,7 @@ Blockly.Dart.addReservedWords('Math');
Blockly.Dart['math_number'] = function(block) {
// Numeric value.
var code = parseFloat(block.getFieldValue('NUM'));
var code = Number(block.getFieldValue('NUM'));
var order;
if (code == Infinity) {
code = 'double.infinity';

View File

@@ -304,7 +304,7 @@ Blockly.JavaScript.getAdjusted = function(block, atId, opt_delta, opt_negate,
if (Blockly.isNumber(at)) {
// If the index is a naked number, adjust it right now.
at = parseFloat(at) + delta;
at = Number(at) + delta;
if (opt_negate) {
at = -at;
}

View File

@@ -354,7 +354,7 @@ Blockly.JavaScript['lists_sort'] = function(block) {
'(type, direction) {',
' var compareFuncs = {',
' "NUMERIC": function(a, b) {',
' return parseFloat(a) - parseFloat(b); },',
' return Number(a) - Number(b); },',
' "TEXT": function(a, b) {',
' return a.toString() > b.toString() ? 1 : -1; },',
' "IGNORE_CASE": function(a, b) {',

View File

@@ -90,11 +90,11 @@ Blockly.JavaScript['controls_for'] = function(block) {
if (Blockly.isNumber(argument0) && Blockly.isNumber(argument1) &&
Blockly.isNumber(increment)) {
// All arguments are simple numbers.
var up = parseFloat(argument0) <= parseFloat(argument1);
var up = Number(argument0) <= Number(argument1);
code = 'for (' + variable0 + ' = ' + argument0 + '; ' +
variable0 + (up ? ' <= ' : ' >= ') + argument1 + '; ' +
variable0;
var step = Math.abs(parseFloat(increment));
var step = Math.abs(Number(increment));
if (step == 1) {
code += up ? '++' : '--';
} else {

View File

@@ -31,7 +31,7 @@ goog.require('Blockly.JavaScript');
Blockly.JavaScript['math_number'] = function(block) {
// Numeric value.
var code = parseFloat(block.getFieldValue('NUM'));
var code = Number(block.getFieldValue('NUM'));
var order = code >= 0 ? Blockly.JavaScript.ORDER_ATOMIC :
Blockly.JavaScript.ORDER_UNARY_NEGATION;
return [code, order];

View File

@@ -316,7 +316,7 @@ Blockly.JavaScript['text_prompt_ext'] = function(block) {
var code = 'window.prompt(' + msg + ')';
var toNumber = block.getFieldValue('TYPE') == 'NUMBER';
if (toNumber) {
code = 'parseFloat(' + code + ')';
code = 'Number(' + code + ')';
}
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
};

View File

@@ -116,8 +116,8 @@ Blockly.Lua['controls_for'] = function(block) {
if (Blockly.isNumber(startVar) && Blockly.isNumber(endVar) &&
Blockly.isNumber(increment)) {
// All arguments are simple numbers.
var up = parseFloat(startVar) <= parseFloat(endVar);
var step = Math.abs(parseFloat(increment));
var up = Number(startVar) <= Number(endVar);
var step = Math.abs(Number(increment));
incValue = (up ? '' : '-') + step;
} else {
code = '';

View File

@@ -31,7 +31,7 @@ goog.require('Blockly.Lua');
Blockly.Lua['math_number'] = function(block) {
// Numeric value.
var code = parseFloat(block.getFieldValue('NUM'));
var code = Number(block.getFieldValue('NUM'));
var order = code < 0 ? Blockly.Lua.ORDER_UNARY :
Blockly.Lua.ORDER_ATOMIC;
return [code, order];
@@ -434,4 +434,4 @@ Blockly.Lua['math_atan2'] = function(block) {
Blockly.Lua.ORDER_NONE) || '0';
return ['math.deg(math.atan2(' + argument1 + ', ' + argument0 + '))',
Blockly.Lua.ORDER_HIGH];
};
};

View File

@@ -285,7 +285,7 @@ Blockly.PHP.getAdjusted = function(block, atId, opt_delta, opt_negate,
if (Blockly.isNumber(at)) {
// If the index is a naked number, adjust it right now.
at = parseFloat(at) + delta;
at = Number(at) + delta;
if (opt_negate) {
at = -at;
}

View File

@@ -89,11 +89,11 @@ Blockly.PHP['controls_for'] = function(block) {
if (Blockly.isNumber(argument0) && Blockly.isNumber(argument1) &&
Blockly.isNumber(increment)) {
// All arguments are simple numbers.
var up = parseFloat(argument0) <= parseFloat(argument1);
var up = Number(argument0) <= Number(argument1);
code = 'for (' + variable0 + ' = ' + argument0 + '; ' +
variable0 + (up ? ' <= ' : ' >= ') + argument1 + '; ' +
variable0;
var step = Math.abs(parseFloat(increment));
var step = Math.abs(Number(increment));
if (step == 1) {
code += up ? '++' : '--';
} else {

View File

@@ -31,7 +31,7 @@ goog.require('Blockly.PHP');
Blockly.PHP['math_number'] = function(block) {
// Numeric value.
var code = parseFloat(block.getFieldValue('NUM'));
var code = Number(block.getFieldValue('NUM'));
var order = code >= 0 ? Blockly.PHP.ORDER_ATOMIC :
Blockly.PHP.ORDER_UNARY_NEGATION;
if (code == Infinity) {

View File

@@ -113,9 +113,9 @@ Blockly.Python['controls_for'] = function(block) {
if (Blockly.isNumber(argument0) && Blockly.isNumber(argument1) &&
Blockly.isNumber(increment)) {
// All parameters are simple numbers.
argument0 = parseFloat(argument0);
argument1 = parseFloat(argument1);
increment = Math.abs(parseFloat(increment));
argument0 = Number(argument0);
argument1 = Number(argument1);
increment = Math.abs(Number(increment));
if (argument0 % 1 === 0 && argument1 % 1 === 0 && increment % 1 === 0) {
// All parameters are integers.
if (argument0 <= argument1) {
@@ -151,7 +151,7 @@ Blockly.Python['controls_for'] = function(block) {
var scrub = function(arg, suffix) {
if (Blockly.isNumber(arg)) {
// Simple number.
arg = parseFloat(arg);
arg = Number(arg);
} else if (arg.match(/^\w+$/)) {
// Variable.
arg = 'float(' + arg + ')';

View File

@@ -34,7 +34,7 @@ Blockly.Python.addReservedWords('math,random,Number');
Blockly.Python['math_number'] = function(block) {
// Numeric value.
var code = parseFloat(block.getFieldValue('NUM'));
var code = Number(block.getFieldValue('NUM'));
var order;
if (code == Infinity) {
code = 'float("inf")';

View File

@@ -1440,7 +1440,7 @@ function test_split() {
function listsGetSortCompare(type, direction) {
var compareFuncs = {
"NUMERIC": function(a, b) {
return parseFloat(a) - parseFloat(b); },
return Number(a) - Number(b); },
"TEXT": function(a, b) {
return a.toString() > b.toString() ? 1 : -1; },
"IGNORE_CASE": function(a, b) {

View File

@@ -166,7 +166,7 @@ Blockly.Dart['unittest_adjustindex'] = function(block) {
if (block.workspace.options.oneBasedIndex) {
if (Blockly.isNumber(index)) {
// If the index is a naked number, adjust it right now.
return [parseFloat(index) + 1, Blockly.Dart.ORDER_ATOMIC];
return [Number(index) + 1, Blockly.Dart.ORDER_ATOMIC];
} else {
// If the index is dynamic, adjust it in code.
index = index + ' + 1';

View File

@@ -170,7 +170,7 @@ Blockly.JavaScript['unittest_adjustindex'] = function(block) {
if (block.workspace.options.oneBasedIndex) {
if (Blockly.isNumber(index)) {
// If the index is a naked number, adjust it right now.
return [parseFloat(index) + 1, Blockly.JavaScript.ORDER_ATOMIC];
return [Number(index) + 1, Blockly.JavaScript.ORDER_ATOMIC];
} else {
// If the index is dynamic, adjust it in code.
index = index + ' + 1';

View File

@@ -173,7 +173,7 @@ Blockly.Lua['unittest_adjustindex'] = function(block) {
Blockly.Lua.ORDER_ADDITIVE) || '0';
if (Blockly.isNumber(index)) {
// If the index is a naked number, adjust it right now.
return [parseFloat(index) + 1, Blockly.Lua.ORDER_ATOMIC];
return [Number(index) + 1, Blockly.Lua.ORDER_ATOMIC];
}
// If the index is dynamic, adjust it in code.
return [index + ' + 1', Blockly.Lua.ORDER_ATOMIC];

View File

@@ -157,7 +157,7 @@ Blockly.PHP['unittest_adjustindex'] = function(block) {
if (block.workspace.options.oneBasedIndex) {
if (Blockly.isNumber(index)) {
// If the index is a naked number, adjust it right now.
return [parseFloat(index) + 1, Blockly.PHP.ORDER_ATOMIC];
return [Number(index) + 1, Blockly.PHP.ORDER_ATOMIC];
} else {
// If the index is dynamic, adjust it in code.
index = index + ' + 1';

View File

@@ -141,7 +141,7 @@ Blockly.Python['unittest_adjustindex'] = function(block) {
if (block.workspace.options.oneBasedIndex) {
if (Blockly.isNumber(index)) {
// If the index is a naked number, adjust it right now.
return [parseFloat(index) + 1, Blockly.Python.ORDER_ATOMIC];
return [Number(index) + 1, Blockly.Python.ORDER_ATOMIC];
} else {
// If the index is dynamic, adjust it in code.
index = index + ' + 1';

View File

@@ -24,7 +24,7 @@ suite('Angle Fields', function() {
var actualText = angleField.getText();
opt_expectedText = opt_expectedText || String(expectedValue);
assertEquals(String(actualValue), String(expectedValue));
assertEquals(parseFloat(actualValue), expectedValue);
assertEquals(Number(actualValue), expectedValue);
assertEquals(actualText, opt_expectedText);
}
function assertValueDefault(angleField) {

View File

@@ -24,7 +24,7 @@ suite('Number Fields', function() {
var actualText = numberField.getText();
opt_expectedText = opt_expectedText || String(expectedValue);
assertEquals(String(actualValue), String(expectedValue));
assertEquals(parseFloat(actualValue), expectedValue);
assertEquals(Number(actualValue), expectedValue);
assertEquals(actualText, opt_expectedText);
}
function assertValueDefault(numberField) {