Skip to content

Commit 846f2c7

Browse files
authored
Merge pull request #1185 from plotly/clean-datum-regression-alt
fix regression in lib/clean_number (alternate)
2 parents 48b0358 + 79fe7d5 commit 846f2c7

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/lib/clean_number.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,16 @@ var isNumeric = require('fast-isnumeric');
1313

1414
var BADNUM = require('../constants/numerical').BADNUM;
1515

16-
// precompile these regex's for speed
17-
var FRONTJUNK = /^['"%,$#\s']+/;
18-
var ENDJUNK = /['"%,$#\s']+$/;
16+
// precompile for speed
17+
var JUNK = /^['"%,$#\s']+|[, ]|['"%,$#\s']+$/g;
1918

2019
/**
2120
* cleanNumber: remove common leading and trailing cruft
2221
* Always returns either a number or BADNUM.
2322
*/
2423
module.exports = function cleanNumber(v) {
2524
if(typeof v === 'string') {
26-
v = v.replace(FRONTJUNK, '').replace(ENDJUNK, '');
25+
v = v.replace(JUNK, '');
2726
}
2827

2928
if(isNumeric(v)) return Number(v);

test/jasmine/tests/lib_test.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,7 +1522,12 @@ describe('Test lib.js:', function() {
15221522
['-100.001', -100.001],
15231523
[' $4.325 #%\t', 4.325],
15241524
[' " #1" ', 1],
1525-
[' \'\n \r -9.2e7 \t\' ', -9.2e7]
1525+
[' \'\n \r -9.2e7 \t\' ', -9.2e7],
1526+
['1,690,000', 1690000],
1527+
['1 690 000', 1690000],
1528+
['2 2', 22],
1529+
['$5,162,000.00', 5162000],
1530+
[' $1,410,000.00 ', 1410000],
15261531
].forEach(function(v) {
15271532
expect(Lib.cleanNumber(v[0])).toBe(v[1], v[0]);
15281533
});
@@ -1531,7 +1536,7 @@ describe('Test lib.js:', function() {
15311536
it('should not accept other objects or cruft in the middle', function() {
15321537
[
15331538
NaN, Infinity, -Infinity, null, undefined, new Date(), '',
1534-
' ', '\t', '2 2', '2%2', '2$2', {1: 2}, [1], ['1'], {}, []
1539+
' ', '\t', '2\t2', '2%2', '2$2', {1: 2}, [1], ['1'], {}, []
15351540
].forEach(function(v) {
15361541
expect(Lib.cleanNumber(v)).toBeUndefined(v);
15371542
});

0 commit comments

Comments
 (0)