Skip to content

Commit cdeca49

Browse files
committed
Improve number parsing.
1 parent f79b9f6 commit cdeca49

7 files changed

Lines changed: 46 additions & 75 deletions

File tree

image.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
/**
2323
* @module FrameworkImage
24-
* @version 1.9.0
24+
* @version 1.9.2
2525
*/
2626

2727
'use strict';
@@ -104,15 +104,15 @@ exports.measureSVG = function(buffer) {
104104

105105
if (width === 0) {
106106
if (value.startsWith('width="')) {
107-
width = parseInt(value.match(/\d+/g));
107+
width = +value.match(/\d+/g);
108108
if (isNaN(width))
109109
width = 0;
110110
}
111111
}
112112

113113
if (height === 0) {
114114
if (value.startsWith('height="')) {
115-
height = parseInt(value.match(/\d+/g));
115+
height = +value.match(/\d+/g);
116116
if (isNaN(height))
117117
height = 0;
118118
}

index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
/**
2323
* @module Framework
24-
* @version 1.9.1
24+
* @version 1.9.2
2525
*/
2626

2727
'use strict';
@@ -227,7 +227,7 @@ function Framework() {
227227

228228
this.id = null;
229229
this.version = 1910;
230-
this.version_header = '1.9.2-1';
230+
this.version_header = '1.9.2-2';
231231

232232
var version = process.version.toString().replace('v', '').replace(/\./g, '');
233233
if (version[1] === '0')
@@ -4404,8 +4404,8 @@ Framework.prototype.responseRange = function(name, range, headers, req, res, don
44044404

44054405
var self = this;
44064406
var arr = range.replace(/bytes=/, '').split('-');
4407-
var beg = parseInt(arr[0] || '0', 10);
4408-
var end = parseInt(arr[1] || '0', 10);
4407+
var beg = +arr[0] || 0;
4408+
var end = +arr[1] || 0;
44094409
var total = self.temporary.range[name] || 0;
44104410

44114411
if (total === 0) {
@@ -4981,7 +4981,7 @@ Framework.prototype.initialize = function(http, debug, options) {
49814981

49824982
if (!port) {
49834983
if (self.config['default-port'] === 'auto') {
4984-
var envPort = parseInt(process.env.PORT || '');
4984+
var envPort = +(process.env.PORT || '');
49854985
if (!isNaN(envPort))
49864986
port = envPort;
49874987
} else
@@ -5068,7 +5068,7 @@ Framework.prototype.http = function(mode, options) {
50685068
options = {};
50695069

50705070
if (!options.port)
5071-
options.port = parseInt(process.argv[2]);
5071+
options.port = +process.argv[2];
50725072

50735073
return this.mode(require('http'), mode, options);
50745074
};

internal.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
/**
2323
* @module FrameworkInternal
24-
* @version 1.9.1
24+
* @version 1.9.2
2525
*/
2626

2727
'use strict';
@@ -897,7 +897,7 @@ function autoprefixer(value) {
897897

898898
if (name === 'opacity') {
899899

900-
var opacity = parseFloat(plus.replace('opacity', '').replace(':', '').replace(/\s/g, ''));
900+
var opacity = +plus.replace('opacity', '').replace(':', '').replace(/\s/g, '');
901901
if (isNaN(opacity))
902902
continue;
903903

mail.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
/**
2323
* @module FrameworkMail
24-
* @version 1.9.1
24+
* @version 1.9.2
2525
*/
2626

2727
'use strict'
@@ -573,7 +573,7 @@ Message.prototype._send = function(socket, options, autosend) {
573573
if (mailer.debug)
574574
console.log('<–––', line);
575575

576-
var code = parseInt(line.match(/\d+/)[0], 10);
576+
var code = +line.match(/\d+/)[0];
577577
if (code === 250 && !isAuthorization) {
578578
if ((line.indexOf('AUTH LOGIN PLAIN') !== -1 || line.indexOf('AUTH PLAIN LOGIN') !== -1) || (options.user && options.password)) {
579579
authType = 'plain';

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"name": "Andrea Sessa",
4242
"email": "andrea.sessa@gmail.com"
4343
}],
44-
"version": "1.9.2-1",
44+
"version": "1.9.2-2",
4545
"homepage": "http://www.totaljs.com",
4646
"bugs": {
4747
"url": "https://github.com/totaljs/framework/issues",

test/test-utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,10 @@ function prototypeString() {
133133
assert.ok(str.parseInt() === 0, 'string.parseInt(): ' + str);
134134

135135
str = '255.50';
136-
assert.ok(str.parseFloat() === 255.50, 'string.parseFloat(): ' + str);
136+
assert.ok(str.parseFloat() === 255.5, 'string.parseFloat(): ' + str);
137137

138138
str = ' 255,50 ';
139-
assert.ok(str.parseFloat() === 255.50, 'string.parseFloat(): ' + str);
139+
assert.ok(str.parseFloat() === 255.5, 'string.parseFloat(): ' + str);
140140

141141
str = ' ,50 ';
142142
assert.ok(str.parseFloat() === 0.50, 'string.parseFloat(): ' + str);

utils.js

Lines changed: 30 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
/**
2323
* @module FrameworkUtils
24-
* @version 1.9.1
24+
* @version 1.9.2
2525
*/
2626

2727
'use strict';
@@ -517,7 +517,7 @@ exports.request = function(url, flags, data, callback, cookies, headers, encodin
517517
}
518518

519519
request.on('response', function(response) {
520-
responseLength = parseInt(response.headers['content-length']) || 0;
520+
responseLength = +response.headers['content-length'] || 0;
521521
e.emit('begin', responseLength);
522522
});
523523

@@ -726,7 +726,7 @@ exports.download = function(url, flags, data, callback, cookies, headers, encodi
726726
}
727727

728728
request.on('response', function(response) {
729-
responseLength = parseInt(response.headers['content-length']) || 0;
729+
responseLength = +response.headers['content-length'] || 0;
730730
e.emit('begin', responseLength);
731731
});
732732

@@ -1135,17 +1135,12 @@ exports.isNullOrEmpty = function(str) {
11351135
* @return {Number}
11361136
*/
11371137
exports.parseInt = function(obj, def) {
1138-
11391138
if (obj === undefined || obj === null)
11401139
return def || 0;
1141-
11421140
var type = typeof(obj);
1143-
11441141
if (type === NUMBER)
11451142
return obj;
1146-
1147-
var str = type !== STRING ? obj.toString() : obj;
1148-
return str.parseInt(def, 10);
1143+
return (type !== STRING ? obj.toString() : obj).parseInt();
11491144
};
11501145

11511146
exports.parseBool = exports.parseBoolean = function(obj, def) {
@@ -2067,18 +2062,18 @@ Date.prototype.extend = function(date) {
20672062
if (m.indexOf(':') !== -1) {
20682063

20692064
arr = m.split(':');
2070-
tmp = parseInt(arr[0], 10);
2065+
tmp = +arr[0];
20712066
if (!isNaN(tmp))
20722067
dt.setHours(tmp);
20732068

20742069
if (arr[1]) {
2075-
tmp = parseInt(arr[1], 10);
2070+
tmp = +arr[1];
20762071
if (!isNaN(tmp))
20772072
dt.setMinutes(tmp);
20782073
}
20792074

20802075
if (arr[2]) {
2081-
tmp = parseInt(arr[2], 10);
2076+
tmp = +arr[2];
20822077
if (!isNaN(tmp))
20832078
dt.setSeconds(tmp);
20842079
}
@@ -2089,17 +2084,17 @@ Date.prototype.extend = function(date) {
20892084
if (m.indexOf('-') !== -1) {
20902085
arr = m.split('-');
20912086

2092-
tmp = parseInt(arr[0]);
2087+
tmp = +arr[0];
20932088
dt.setFullYear(tmp);
20942089

20952090
if (arr[1]) {
2096-
tmp = parseInt(arr[1], 10);
2091+
tmp = +arr[1];
20972092
if (!isNaN(tmp))
20982093
dt.setMonth(tmp - 1);
20992094
}
21002095

21012096
if (arr[2]) {
2102-
tmp = parseInt(arr[2], 10);
2097+
tmp = +arr[2];
21032098
if (!isNaN(tmp))
21042099
dt.setDate(tmp);
21052100
}
@@ -2110,17 +2105,17 @@ Date.prototype.extend = function(date) {
21102105
if (m.indexOf('.') !== -1) {
21112106
arr = m.split('.');
21122107

2113-
tmp = parseInt(arr[0], 10);
2108+
tmp = +arr[0];
21142109
dt.setDate(tmp);
21152110

21162111
if (arr[1]) {
2117-
tmp = parseInt(arr[1], 10);
2112+
tmp = +arr[1];
21182113
if (!isNaN(tmp))
21192114
dt.setMonth(tmp - 1);
21202115
}
21212116

21222117
if (arr[2]) {
2123-
tmp = parseInt(arr[2]);
2118+
tmp = +arr[2];
21242119
if (!isNaN(tmp))
21252120
dt.setFullYear(tmp);
21262121
}
@@ -2415,13 +2410,13 @@ String.prototype.parseDate = function() {
24152410
} else
24162411
time[3] = '0';
24172412

2418-
parsed.push(parseInt(date[firstDay ? 2 : 0], 10)); // year
2419-
parsed.push(parseInt(date[1], 10)); // month
2420-
parsed.push(parseInt(date[firstDay ? 0 : 2], 10)); // day
2421-
parsed.push(parseInt(time[0], 10)); // hours
2422-
parsed.push(parseInt(time[1], 10)); // minutes
2423-
parsed.push(parseInt(time[2], 10)); // seconds
2424-
parsed.push(parseInt(time[3], 10)); // miliseconds
2413+
parsed.push(+date[firstDay ? 2 : 0]); // year
2414+
parsed.push(+date[1]); // month
2415+
parsed.push(+date[firstDay ? 0 : 2]); // day
2416+
parsed.push(+time[0]); // hours
2417+
parsed.push(+time[1]); // minutes
2418+
parsed.push(+time[2]); // seconds
2419+
parsed.push(+time[3]); // miliseconds
24252420

24262421
var def = new Date();
24272422

@@ -2556,7 +2551,7 @@ String.prototype.parseConfig = function(def) {
25562551
String.prototype.format = function() {
25572552
var arg = arguments;
25582553
return this.replace(regexpSTRINGFORMAT, function(text) {
2559-
var value = arg[parseInt(text.substring(1, text.length - 1))];
2554+
var value = arg[+text.substring(1, text.length - 1)];
25602555
if (value === null || value === undefined)
25612556
value = '';
25622557
return value;
@@ -2665,13 +2660,13 @@ String.prototype.params = function(obj) {
26652660

26662661
var type = typeof(val);
26672662
if (type === STRING) {
2668-
var max = parseInt(format, 10);
2663+
var max = +format;
26692664
if (!isNaN(max))
26702665
val = val.max(max + 3, '...');
26712666

26722667
} else if (type === NUMBER || util.isDate(val)) {
26732668
if (format.isNumber())
2674-
format = parseInt(format);
2669+
format = +format;
26752670
val = val.format(format);
26762671
}
26772672
}
@@ -2721,50 +2716,26 @@ String.prototype.isEmail = function() {
27212716
return regexpMail.test(str);
27222717
};
27232718

2724-
/*
2725-
@def {Number} :: optional, default 0
2726-
return {Number}
2727-
*/
27282719
String.prototype.parseInt = function(def) {
2729-
var num = 0;
2730-
var str = this;
2731-
2732-
if (str.substring(0, 1) === '0')
2733-
num = parseInt(str.replace(/\s/g, '').substring(1), 10);
2734-
else
2735-
num = parseInt(str.replace(/\s/g, ''), 10);
2736-
2720+
var str = this.trim();
2721+
var num = +str;
27372722
if (isNaN(num))
27382723
return def || 0;
2739-
27402724
return num;
27412725
};
27422726

2743-
/*
2744-
@def {Number} :: optional, default 0
2745-
return {Number}
2746-
*/
27472727
String.prototype.parseBool = String.prototype.parseBoolean = function() {
27482728
var self = this.toLowerCase();
27492729
return self === 'true' || self === '1' || self === 'on';
27502730
};
27512731

2752-
/*
2753-
@def {Number} :: optional, default 0
2754-
return {Number}
2755-
*/
27562732
String.prototype.parseFloat = function(def) {
2757-
var num = 0;
2758-
var str = this;
2759-
2760-
if (str.substring(0, 1) === '0')
2761-
num = parseFloat(str.replace(/\s/g, '').substring(1).replace(',', '.'));
2762-
else
2763-
num = parseFloat(str.replace(/\s/g, '').replace(',', '.'));
2764-
2733+
var str = this.trim();
2734+
if (str.indexOf(',') !== -1)
2735+
str = str.replace(',', '.');
2736+
var num = +str;
27652737
if (isNaN(num))
27662738
return def || 0;
2767-
27682739
return num;
27692740
};
27702741

@@ -2868,7 +2839,7 @@ String.prototype.decrypt = function(key) {
28682839
if (index === -1)
28692840
return null;
28702841

2871-
var counter = parseInt(values.substring(0, index), 10);
2842+
var counter = +values.substring(0, index);
28722843
if (isNaN(counter))
28732844
return null;
28742845

0 commit comments

Comments
 (0)