Skip to content

Commit 5f98edd

Browse files
committed
fix regression in lib/clean_number (alternate)
- allow , and spaces in middle of numeric strings
1 parent 48b0358 commit 5f98edd

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/lib/clean_number.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@ var BADNUM = require('../constants/numerical').BADNUM;
1616
// precompile these regex's for speed
1717
var FRONTJUNK = /^['"%,$#\s']+/;
1818
var ENDJUNK = /['"%,$#\s']+$/;
19+
var GLOBALJUNK = /[,\s]/g;
1920

2021
/**
2122
* cleanNumber: remove common leading and trailing cruft
2223
* Always returns either a number or BADNUM.
2324
*/
2425
module.exports = function cleanNumber(v) {
2526
if(typeof v === 'string') {
26-
v = v.replace(FRONTJUNK, '').replace(ENDJUNK, '');
27+
v = v
28+
.replace(FRONTJUNK, '')
29+
.replace(ENDJUNK, '')
30+
.replace(GLOBALJUNK, '');
2731
}
2832

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

test/jasmine/tests/lib_test.js

+7-2
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%2', '2$2', {1: 2}, [1], ['1'], {}, []
15351540
].forEach(function(v) {
15361541
expect(Lib.cleanNumber(v)).toBeUndefined(v);
15371542
});

0 commit comments

Comments
 (0)