We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 48b0358 commit 5f98eddCopy full SHA for 5f98edd
src/lib/clean_number.js
@@ -16,14 +16,18 @@ var BADNUM = require('../constants/numerical').BADNUM;
16
// precompile these regex's for speed
17
var FRONTJUNK = /^['"%,$#\s']+/;
18
var ENDJUNK = /['"%,$#\s']+$/;
19
+var GLOBALJUNK = /[,\s]/g;
20
21
/**
22
* cleanNumber: remove common leading and trailing cruft
23
* Always returns either a number or BADNUM.
24
*/
25
module.exports = function cleanNumber(v) {
26
if(typeof v === 'string') {
- v = v.replace(FRONTJUNK, '').replace(ENDJUNK, '');
27
+ v = v
28
+ .replace(FRONTJUNK, '')
29
+ .replace(ENDJUNK, '')
30
+ .replace(GLOBALJUNK, '');
31
}
32
33
if(isNumeric(v)) return Number(v);
test/jasmine/tests/lib_test.js
@@ -1522,7 +1522,12 @@ describe('Test lib.js:', function() {
1522
['-100.001', -100.001],
1523
[' $4.325 #%\t', 4.325],
1524
[' " #1" ', 1],
1525
- [' \'\n \r -9.2e7 \t\' ', -9.2e7]
+ [' \'\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],
1531
].forEach(function(v) {
1532
expect(Lib.cleanNumber(v[0])).toBe(v[1], v[0]);
1533
});
@@ -1531,7 +1536,7 @@ describe('Test lib.js:', function() {
1536
it('should not accept other objects or cruft in the middle', function() {
1537
[
1538
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'], {}, []
1535
1540
1541
expect(Lib.cleanNumber(v)).toBeUndefined(v);
1542
0 commit comments