Skip to content

Commit

Permalink
Merge pull request #5637 from plotly/simplify-ensure-number
Browse files Browse the repository at this point in the history
simplify ensureNumber
  • Loading branch information
archmoj authored May 5, 2021
2 parents 32ea3c9 + e506484 commit 9a583b4
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/constants/numerical.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = {
* to avoid glitches: Make sure that even when you multiply it by the
* number of pixels on a giant screen it still works
*/
FP_SAFE: Number.MAX_VALUE / 10000,
FP_SAFE: Number.MAX_VALUE * 1e-4,

/*
* conversion of date units to milliseconds
Expand Down
6 changes: 3 additions & 3 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ var utcFormat = require('d3-time-format').utcFormat;
var isNumeric = require('fast-isnumeric');

var numConstants = require('../constants/numerical');
var FP_SAFE = numConstants.FP_SAFE;
var MAX_SAFE = numConstants.FP_SAFE;
var MIN_SAFE = -MAX_SAFE;
var BADNUM = numConstants.BADNUM;

var lib = module.exports = {};
Expand Down Expand Up @@ -166,8 +167,7 @@ lib.cleanNumber = require('./clean_number');
lib.ensureNumber = function ensureNumber(v) {
if(!isNumeric(v)) return BADNUM;
v = Number(v);
if(v < -FP_SAFE || v > FP_SAFE) return BADNUM;
return isNumeric(v) ? Number(v) : BADNUM;
return (v > MAX_SAFE || v < MIN_SAFE) ? BADNUM : v;
};

/**
Expand Down

0 comments on commit 9a583b4

Please sign in to comment.