Skip to content

Commit 79fe7d5

Browse files
committed
turn front/end/global regex's into 1
- and allow only ' ' in middle instead of all space characters
1 parent 5f98edd commit 79fe7d5

File tree

2 files changed

+4
-9
lines changed

2 files changed

+4
-9
lines changed

src/lib/clean_number.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -13,21 +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']+$/;
19-
var GLOBALJUNK = /[,\s]/g;
16+
// precompile for speed
17+
var JUNK = /^['"%,$#\s']+|[, ]|['"%,$#\s']+$/g;
2018

2119
/**
2220
* cleanNumber: remove common leading and trailing cruft
2321
* Always returns either a number or BADNUM.
2422
*/
2523
module.exports = function cleanNumber(v) {
2624
if(typeof v === 'string') {
27-
v = v
28-
.replace(FRONTJUNK, '')
29-
.replace(ENDJUNK, '')
30-
.replace(GLOBALJUNK, '');
25+
v = v.replace(JUNK, '');
3126
}
3227

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

test/jasmine/tests/lib_test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,7 @@ describe('Test lib.js:', function() {
15361536
it('should not accept other objects or cruft in the middle', function() {
15371537
[
15381538
NaN, Infinity, -Infinity, null, undefined, new Date(), '',
1539-
' ', '\t', '2%2', '2$2', {1: 2}, [1], ['1'], {}, []
1539+
' ', '\t', '2\t2', '2%2', '2$2', {1: 2}, [1], ['1'], {}, []
15401540
].forEach(function(v) {
15411541
expect(Lib.cleanNumber(v)).toBeUndefined(v);
15421542
});

0 commit comments

Comments
 (0)