We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 48b0358 + 79fe7d5 commit 846f2c7Copy full SHA for 846f2c7
src/lib/clean_number.js
@@ -13,17 +13,16 @@ var isNumeric = require('fast-isnumeric');
13
14
var BADNUM = require('../constants/numerical').BADNUM;
15
16
-// precompile these regex's for speed
17
-var FRONTJUNK = /^['"%,$#\s']+/;
18
-var ENDJUNK = /['"%,$#\s']+$/;
+// precompile for speed
+var JUNK = /^['"%,$#\s']+|[, ]|['"%,$#\s']+$/g;
19
20
/**
21
* cleanNumber: remove common leading and trailing cruft
22
* Always returns either a number or BADNUM.
23
*/
24
module.exports = function cleanNumber(v) {
25
if(typeof v === 'string') {
26
- v = v.replace(FRONTJUNK, '').replace(ENDJUNK, '');
+ v = v.replace(JUNK, '');
27
}
28
29
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\t2', '2%2', '2$2', {1: 2}, [1], ['1'], {}, []
1535
1540
1541
expect(Lib.cleanNumber(v)).toBeUndefined(v);
1542
0 commit comments