From 6852f28cc4dbd93a323e3bac6fcff8be29e19a7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20T=C3=A9treault-Pinard?= Date: Mon, 21 Nov 2016 17:20:25 -0500 Subject: [PATCH 1/2] fix regression in lib/clean_number - junk in the 'middle' of numeric string is again accepted --- src/lib/clean_number.js | 7 +++---- test/jasmine/tests/lib_test.js | 14 ++++++++++---- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/lib/clean_number.js b/src/lib/clean_number.js index dbedfe2e2a6..2ecc8b8bfb3 100644 --- a/src/lib/clean_number.js +++ b/src/lib/clean_number.js @@ -13,9 +13,8 @@ var isNumeric = require('fast-isnumeric'); var BADNUM = require('../constants/numerical').BADNUM; -// precompile these regex's for speed -var FRONTJUNK = /^['"%,$#\s']+/; -var ENDJUNK = /['"%,$#\s']+$/; +// precompile for speed +var JUNK = /['"%,$#\s]/g; /** * cleanNumber: remove common leading and trailing cruft @@ -23,7 +22,7 @@ var ENDJUNK = /['"%,$#\s']+$/; */ module.exports = function cleanNumber(v) { if(typeof v === 'string') { - v = v.replace(FRONTJUNK, '').replace(ENDJUNK, ''); + v = v.replace(JUNK, ''); } if(isNumeric(v)) return Number(v); diff --git a/test/jasmine/tests/lib_test.js b/test/jasmine/tests/lib_test.js index ded7aa9d3df..105c0c80e92 100644 --- a/test/jasmine/tests/lib_test.js +++ b/test/jasmine/tests/lib_test.js @@ -1514,7 +1514,7 @@ describe('Test lib.js:', function() { }); }); - it('should accept number strings with arbitrary cruft on the outside', function() { + it('should accept number strings with arbitrary cruft', function() { [ ['0', 0], ['1', 1], @@ -1522,16 +1522,22 @@ describe('Test lib.js:', function() { ['-100.001', -100.001], [' $4.325 #%\t', 4.325], [' " #1" ', 1], - [' \'\n \r -9.2e7 \t\' ', -9.2e7] + [' \'\n \r -9.2e7 \t\' ', -9.2e7], + + // post https://github.com/plotly/plotly.js/issues/1183 + ['2 2', 22], + ['2%2', 22], + ['2$2', 22], + ['1,690,000', 1690000] ].forEach(function(v) { expect(Lib.cleanNumber(v[0])).toBe(v[1], v[0]); }); }); - it('should not accept other objects or cruft in the middle', function() { + it('should not accept other objects', function() { [ NaN, Infinity, -Infinity, null, undefined, new Date(), '', - ' ', '\t', '2 2', '2%2', '2$2', {1: 2}, [1], ['1'], {}, [] + ' ', '\t', , {1: 2}, [1], ['1'], {}, [] ].forEach(function(v) { expect(Lib.cleanNumber(v)).toBeUndefined(v); }); From 03419163b1f67b12ce3ed425f30e094d351b5d3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20T=C3=A9treault-Pinard?= Date: Mon, 21 Nov 2016 17:39:40 -0500 Subject: [PATCH 2/2] test: add more clean number cases --- test/jasmine/tests/lib_test.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/jasmine/tests/lib_test.js b/test/jasmine/tests/lib_test.js index 105c0c80e92..ee493ff7cbf 100644 --- a/test/jasmine/tests/lib_test.js +++ b/test/jasmine/tests/lib_test.js @@ -1528,7 +1528,9 @@ describe('Test lib.js:', function() { ['2 2', 22], ['2%2', 22], ['2$2', 22], - ['1,690,000', 1690000] + ['1,690,000', 1690000], + ['$5,162,000.00', 5162000], + [' $1,410,000.00 ', 1410000], ].forEach(function(v) { expect(Lib.cleanNumber(v[0])).toBe(v[1], v[0]); }); @@ -1537,7 +1539,7 @@ describe('Test lib.js:', function() { it('should not accept other objects', function() { [ NaN, Infinity, -Infinity, null, undefined, new Date(), '', - ' ', '\t', , {1: 2}, [1], ['1'], {}, [] + ' ', '\t', {1: 2}, [1], ['1'], {}, [] ].forEach(function(v) { expect(Lib.cleanNumber(v)).toBeUndefined(v); });